[英]Powershell Start-Process to start Powershell session and pass local variables
有没有一种方法可以使用Powershell Start-Process cmdlet启动新的Powershell会话并传递带有局部变量的脚本块(其中一次将是数组)?
例:
$Array = @(1,2,3,4)
$String = "This is string number"
$Scriptblock = {$Array | ForEach-Object {Write-Host $String $_}}
Start-Process Powershell -ArgumentList "$Scriptblock"
谢谢。
我很确定没有直接方法将变量从一个PowerShell会话传递到另一个。 最好的办法就是采取一些解决方法,例如在-ArgumentList中传递的代码中声明变量,然后在调用会话中插入值。 如何将变量插值到-ArgumentList中的声明中取决于变量的类型。 对于数组和字符串,您可以执行以下操作:
$command = '<contents of your scriptblock without the curly braces>'
Start-Process powershell -ArgumentList ("`$Array = echo $Array; `$String = '$String';" + $command)
您可以将脚本块的内容包装到一个函数中,然后从ArgumentList调用该函数,然后将变量作为参数传递给该函数, 就像我在本文中所做的那样 。
$ScriptBlock = {
function Test([string]$someParameter)
{
# Use $someParameter to do something...
}
}
# Run the script block and pass in parameters.
$myString = "Hello"
Start-Process -FilePath PowerShell -ArgumentList "-Command & {$ScriptBlock Test('$myString')}"
通过将数组与“ /”连接起来以创建字符串,然后将脚本块输入具有适当参数的另一个.ps1脚本中,然后在第二个脚本中将连接的字符串拆分回数组,我可以使用此方法
Start-Process Powershell -ArgumentList "&C:\script.ps1 $JoinedArray $String"
丑陋,但这是我可以使用它的唯一方法。 感谢所有的答复。
PowerShell.exe的命令行选项说,使用脚本块时,您应该能够通过添加-args来传递参数:
PowerShell.exe -Command { - | <script-block> [-args <arg-array>] | <string> [<CommandParameters>] }
但是,当我尝试这样做时,出现以下错误:
-args:术语“ -args”不被视为cmdlet,函数,脚本文件或可运行程序的名称。 检查名称的拼写,或者是否包含路径,请验证路径是否正确,然后重试。
我添加了$MyInvocation | fl
$MyInvocation | fl
转到脚本块以查看正在发生的情况,并且-args似乎只是附加到脚本块中反序列化的命令中(由于-args不是有效命令,因此出现错误)。 我也尝试过使用GetNewClosure()和$ Using:VariableName,但是它们仅在脚本块被调用时才起作用(与之相反,我们使用它来序列化/反序列化命令)。
通过将它包装在如deadlydog's answer之类的函数中,我能够使它工作。
$var = "this is a test"
$scriptblock = {
$MyInvocation | fl #Show deserialized commands
function AdminTasks($message){
write-host "hello world: $message"
}
}
Start-Process powershell -ArgumentList '-noexit','-nologo','-noprofile','-NonInteractive','-Command',$scriptblock,"AdminTasks('$var')" -Verb runAs #-WindowStyle Hidden
#Output:
MyCommand :
$MyInvocation | fl #Show deserialized commands
function AdminTasks($message){
write-host hello world: $message
}
AdminTasks('this is a test')
BoundParameters : {}
UnboundArguments : {}
ScriptLineNumber : 0
OffsetInLine : 0
HistoryId : 1
ScriptName :
Line :
PositionMessage :
PSScriptRoot :
PSCommandPath :
InvocationName :
PipelineLength : 2
PipelinePosition : 1
ExpectingInput : False
CommandOrigin : Runspace
DisplayScriptPosition :
hello world: this is a test
将其包装在脚本块中并使用$args[0]
或$args[1]
也可以,但是要注意,如果在反序列化并使用`$ var0或$ var1时出现问题,您需要将$ var0或$ var1括起来。 $以防止$ sb被“”替换,因为该变量在调用者的作用域中不存在:
$var0 = "hello"
$var1 = "world"
$scriptblock = {
$MyInvocation | fl #Show deserialized commands
$sb = {
write-host $args[0] $args[1]
}
}
Start-Process powershell -ArgumentList '-noexit','-nologo','-noprofile','-NonInteractive','-Command',$scriptblock,"& `$sb $var0 $var1"
如果要传递可序列化但不是字符串的对象,我写了一个解决方案: 是否可以通过启动过程将可序列化的对象传递给PowerShell脚本?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.