write-host "Message.Status: Test Message Status";
我设法通过一个独立的过程来运行它:
powershell.exe -Command { write-host "Message.Status: Test Message Status"; }
问题是我想传递参数到脚本,以便我可以实现这样的事情:
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)"; }
但是-ArgumentList在这里不起作用
代码执行在subprocess中开始在哪里?
batch file,Vista x64,如果和括号
在Linux上改变C中的真实进程名称
SHGetSpecialFolderPath,如何从32位应用程序访问64位CSIDL
Runtime.getRuntime()。exec()的奇怪行为
我得到:
powershell.exe : -ArgumentList : The term '-ArgumentList' is not recognized as the name of a cmdlet,function,script file,or operable
我需要在不同的进程中运行PowerShell脚本文件的某些部分,并且由于PowerShell脚本已上传到外部系统,因此我无法使用其他文件。
在一个batch file里面,我怎么知道一个进程是否正在运行?
PyInstaller将不会安装Python 3.6.0a4和x64 Windows
如何将一个std :: string环境块传递给CreateProcess?
64位Windows文件打开
-Command参数需要一个-Command块,您可以在其中使用param()块定义参数。 然后使用-args参数传递参数。 你唯一的错误就是在你定义scriptblock 之前把-args after -command 。
所以这是如何工作的:
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","here" | Out-Null
输出:
I am in main process 17900 I am in process 10284 Message.Status: I am here
您可以使用-File参数并按脚本的路径进行操作。 以下任何未命名的参数都将作为脚本参数传递。 像下面的东西应该做的
powershell -File "C:ScriptFolderScriptwithParameters.ps1" "ParameterOneValu" "valuetwo"
好吧,如果你完全需要另一个进程而不是另一个文件,那么你最好的选择可能是.NET运行空间。 基本上你的代码包装在一个scriptblock中
$SB = { *Your Code* }
然后像下面那样设置一个运行空间,确保使用“UseNewThread”作为线程选项。 注意$ 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()
如果您需要从运行空间获得任何数据,那么您可能需要对其进行调整,但有几种方法可以实现这一点(如果需要帮助,请发表评论)。 如果您需要同步执行而不是异步,则将.BeginInvoke()更改为.Invoke()
所以应该让你开始,但是这将需要一些移动的部分。 首先我们定义一个新的函数:
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 }
这个功能将启动新的过程。 它使用start-process cmdlet, -Argumentlist是我们的参数应用于powershell.exe您可以删除-noExit ,使完成时新的进程关闭或添加其他PowerShell标志,并在start-process标志获取窗口和行为调整到您的要求。
接下来我们定义我们的脚本块:
$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)"; }
在这里,我们在块的开始部分定义了一些参数,它们有一个位置和名称,例如位置0中的任何参数都将在变量$arg1 。块中的其余代码全部在新进程中执行。
现在我们已经定义了脚本块和函数在一个新的进程中运行它,我们所要做的就是调用它:
Run-InNewProcess $script -Arg1 '"WHAT WHAT"' -Arg2 '"In the But"'
把这个代码全部复制到你的ISE中,然后你就可以看到它。
Start-Job将为其脚本块创建一个过程,并且将参数传递给它是很简单的。
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)"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。