Powershell 如何从已执行脚本访问执行脚本中的变量
我有一个正在运行的PS1脚本(start webserver.PS1)作为“web服务器”运行,它正在侦听http调用,该调用执行在对“web服务器”的调用中执行的脚本(例如:script2.PS1)。 我正在用Start Job执行脚本。如何在已执行脚本(script2.ps1)中访问start-webserver.ps1中的变量 Start-WebServer.ps1 ScriptToExecute.ps1 最后我得到了一个“output.txt”,内容如下:Powershell 如何从已执行脚本访问执行脚本中的变量,powershell,Powershell,我有一个正在运行的PS1脚本(start webserver.PS1)作为“web服务器”运行,它正在侦听http调用,该调用执行在对“web服务器”的调用中执行的脚本(例如:script2.PS1)。 我正在用Start Job执行脚本。如何在已执行脚本(script2.ps1)中访问start-webserver.ps1中的变量 Start-WebServer.ps1 ScriptToExecute.ps1 最后我得到了一个“output.txt”,内容如下: 脚本执行完毕 空行您需要在Sc
脚本执行完毕
空行您需要在ScriptToExecute中定义您正在接收的参数 默认情况下,它在$args变量中设置 在您的情况下,只需使用$args[0]就足够了 i、 e: 如果还希望接收$AllObject,则需要将Start WebServer修改为:
$job = Start-Job -Name "$identifier" -FilePath "Path\To\ScriptToExecute.ps1" -InputObject $allObjects -ArgumentList @($propertiesHash,$allObjects)
然后这样做:
'Script executed!' | Out-File -Path ".\output.txt" -Encoding UTF8 -Append
$args[0] | Get-Member | Out-File -Path ".\output.txt" -Encoding UTF8 -Append
$args[1] | Get-Member | Out-File -Path ".\output.txt" -Encoding UTF8 -Append
更好的方法是在ScriptToExecute中指定参数
i、 e:
'Script executed!' | Out-File -Path ".\output.txt" -Encoding UTF8 -Append
$args[0] | Get-Member | Out-File -Path ".\output.txt" -Encoding UTF8 -Append
$job = Start-Job -Name "$identifier" -FilePath "Path\To\ScriptToExecute.ps1" -InputObject $allObjects -ArgumentList @($propertiesHash,$allObjects)
'Script executed!' | Out-File -Path ".\output.txt" -Encoding UTF8 -Append
$args[0] | Get-Member | Out-File -Path ".\output.txt" -Encoding UTF8 -Append
$args[1] | Get-Member | Out-File -Path ".\output.txt" -Encoding UTF8 -Append
param($propertiesHash, $allObjects)
'Script executed!' | Out-File -Path ".\output.txt" -Encoding UTF8 -Append
$propertiesHash | Get-Member | Out-File -Path ".\output.txt" -Encoding UTF8 -Append
$allObjects | Get-Member | Out-File -Path ".\output.txt" -Encoding UTF8 -Append