[英]How to run PowerShell script in a different process and pass arguments to it?
Let's say I have a script: 假设我有一个脚本:
write-host "Message.Status: Test Message Status";
I managed to run it in a separate process by doing: 我设法通过以下方式在一个单独的过程中运行它:
powershell.exe -Command
{ write-host "Message.Status: Test Message Status"; }
The problem is I want to pass parameters to the script so that I can achieve something like this: 问题是我想将参数传递给脚本,以便我可以实现这样的事情:
write-host "I am in main process"
powershell.exe -Command -ArgumentList "I","am","here"
{
write-host "I am in another process"
write-host "Message.Status: $($one) $($two) $($three)";
}
However -ArgumentList
doesn't work here 但是-ArgumentList
在这里不起作用
I get: 我明白了:
powershell.exe : -ArgumentList : The term '-ArgumentList' is not recognized as the name of a cmdlet, function, script file, or operable
I need to run some part of PowerShell script file in a different process and I cannot use another file due to the fact that PowerShell script is uploaded to external system. 我需要在不同的进程中运行PowerShell脚本文件的某些部分,由于PowerShell脚本上传到外部系统,我无法使用其他文件。
The -Command
parameter is expecting a scriptblock
in which you can define your parameters using a Param()
block. -Command
参数需要一个scriptblock
,您可以在其中使用Param()
块定义参数。 Then use the -args
parameter to pass in the arguments. 然后使用-args
参数传入参数。 Your only mistake was to put the -args
after -command
before you defined the scriptblock. 您唯一的错误是在定义scriptblock 之前将-args
放在-command
之后 。
So this is how it works: 这就是它的工作原理:
write-host "I am in main process $($pid)"
powershell.exe -Command {
Param(
$one,
$two,
$three
)
write-host "I am in process $($pid)"
write-host "Message.Status: $($one) $($two) $($three)";
} -args "I", "am", "here" | Out-Null
Output: 输出:
I am in main process 17900
I am in process 10284
Message.Status: I am here
You can use the -File parameter and follow it by the path to script. 您可以使用-File参数并按脚本路径进行操作。 Any unnamed arguments which follows will be passed as script parameters. 后面的任何未命名参数将作为脚本参数传递。 Something like below should do 下面应该做的事情
powershell -File "C:\ScriptFolder\ScriptwithParameters.ps1" "ParameterOneValu" "valuetwo"
Ok so if you need another process entirely but not another file then your best bet is probably .NET runspaces. 好的,如果你需要另一个进程而不是另一个文件,那么你最好的选择可能就是.NET运行空间。 Basically wrap your code in a scriptblock 基本上将您的代码包装在一个scriptblock中
$SB = {
*Your Code*
}
Then set up a runspace like below, making sure to use the "UseNewThread" as the thread option. 然后设置如下所示的运行空间,确保使用“UseNewThread”作为线程选项。 Note that $arg is whatever your argument to be passed to the script is 请注意,$ arg是您传递给脚本的参数
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "UseNewThread"
$newRunspace.Open()
$psCmd = [PowerShell]::Create().AddScript($SB).AddArgument($arg)
$psCmd.Runspace = $newRunspace
$data = $psCmd.BeginInvoke()
You'll likely need to tweak this if you need to get any data back from the runspace once it is complete but there are a few ways to do that(leave a comment if you need assistance). 如果您需要在完成后从运行空间获取任何数据,则可能需要调整此项,但有几种方法可以执行此操作(如果需要帮助,请留言)。 If you need synchronous execution rather than async then change .BeginInvoke()
to .Invoke()
如果需要同步执行而不是异步,则将.BeginInvoke()
更改为.Invoke()
So should get you started, But it will require a few moving parts. 所以应该让你开始,但它需要一些活动部分。 First we define a new function: 首先我们定义一个新函数:
function Run-InNewProcess{
param([String] $code)
$code = "function Run{ $code }; Run $args"
$encoded = [Convert]::ToBase64String( [Text.Encoding]::Unicode.GetBytes($code))
start-process PowerShell.exe -argumentlist '-noExit','-encodedCommand',$encoded
}
This function will be what starts the new process. 此功能将启动新流程。 It uses the start-process
cmdlet, The -Argumentlist
is our arguments applied to the powershell.exe
You can remove -noExit
to make the new process close on completion or add other powershell flags, and flags on Start-Process
to get the windows and behaviours tweaked to your requirements. 它使用start-process
cmdlet, -Argumentlist
是我们应用于powershell.exe
参数你可以删除-noExit
以使新进程在完成时关闭或添加其他powershell标志,并在Start-Process
上标记以获取窗口和行为调整到您的要求。
Next we define our script block: 接下来我们定义脚本块:
$script = {
[CmdletBinding()]
Param (
[Parameter(Position=0)]
[string]$Arg1,
[Parameter(Position=1)]
[string]$Arg2)
write-host "I am in another process"
write-host "Message.Status: $($Arg1) $($Arg2)";
}
Here we define some parameters in the opening part of the block, They have a position and name, so for example any argument in position 0 will be in the variable $arg1
The rest of the code in the block is all executed in the new process. 这里我们在块的开始部分定义一些参数,它们有一个位置和名称,所以例如位置0中的任何参数都在变量$arg1
。块中的其余代码都在新进程中执行。
Now we have defined the script block and the function to run it in a new process, All we have to do is call it: 现在我们已经定义了脚本块和在新进程中运行它的函数,我们所要做的就是调用它:
Run-InNewProcess $script -Arg1 '"WHAT WHAT"' -Arg2 '"In the But"'
Copy past this code all in to your ISE and you will see it in action. 将此代码全部复制到您的ISE中,您将看到它正在运行中。
Start-Job will create a process for its scriptblock, and it's straightforward to pass arguments to it. Start-Job将为其scriptblock创建一个进程,并且可以直接向其传递参数。
Write-Host "Process count before starting job: $((Get-Process |? { $_.ProcessName -imatch "powershell" }).Count)"
$job = Start-Job `
-ArgumentList "My argument!" `
-ScriptBlock {
param($arg)
Start-Sleep -Seconds 5;
Write-Host "All Done! Argument: $arg"
}
while ($job.State -ne "Completed")
{
Write-Host "Process count during job: $((Get-Process |? { $_.ProcessName -imatch "powershell" }).Count)"
Start-Sleep -Seconds 1
}
Receive-Job $job -AutoRemoveJob -Wait
Write-Host "Process count after job: $((Get-Process |? { $_.ProcessName -imatch "powershell" }).Count)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.