Powershell 在脚本中通过新shell传递arg
由于库内存泄漏,我正在尝试调用新的shell。当我调用shell时,我需要传递一个参数(实际代码将传递2个参数)。在新shell中执行代码块后,它需要返回一个值。我编写了一些测试代码来重现错误:Powershell 在脚本中通过新shell传递arg,powershell,powershell-3.0,Powershell,Powershell 3.0,由于库内存泄漏,我正在尝试调用新的shell。当我调用shell时,我需要传递一个参数(实际代码将传递2个参数)。在新shell中执行代码块后,它需要返回一个值。我编写了一些测试代码来重现错误: Function GetLastName { Param ($firstName) $lastName = Powershell -firstName $firstName { Param ([string]$firstName) $lastName =
Function GetLastName
{
Param ($firstName)
$lastName = Powershell -firstName $firstName {
Param ([string]$firstName)
$lastName = ''
if ($firstName = 'John')
{
$lastName = 'Doe'
Write-Host "Hello $firstName, your last name is registered as $lastName"
}
Write-Host "Last name not found"
Write-Output $lastName
}
Write-Output $lastName
}
Function Main
{
$firstName = 'John'
$lastName = GetLastName $firstName
Write-Host "Your name is $firstName $lastName"
}
Main
我得到的错误
Powershell : -firstName : The term '-firstName' is not recognized as the name of
a cmdlet, function, script file, or operable
At C:\Scripts\Tests\test1.ps1:5 char:15
+ $lastName = Powershell -firstName $firstName {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (-firstName : Th...e, or operable :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
program. Check the spelling of the name, or if a path was included, verify that
the path is correct and try again.
At line:1 char:1
+ -firstName John -encodedCommand DQAKAAkACQAJAFAAYQByAGEAbQAgACgAWwBzAHQAcgBpAG4A ...
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (-firstName:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Powershell:-firstName:术语'-firstName'不被识别为的名称
cmdlet、函数、脚本文件或可操作的
在C:\Scripts\Tests\test1.ps1:5 char:15
+$lastName=Powershell-firstName$firstName{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:NotSpecified:(-firstName:Th…e,或可操作:String)[],RemoteException
+FullyQualifiedErrorId:NativeCommandError
检查名称的拼写,或者如果包含路径,请验证
路径正确,请重试。
第1行字符:1
+-名字John-编码命令DQAKAKAQAJAFAAYQBYAGEABQAGAGAGAGCGAWWBZAHQACGBPAG4A。。。
+ ~~~~~~~~~~
+CategoryInfo:ObjectNotFound:(-firstName:String)[],CommandNotFoundException
+FullyQualifiedErrorId:CommandNotFoundException
有人能帮我怎么做吗?将代码拆分为两个单独的脚本,并将其中一个用作第二个脚本的启动程序。类似如下:
# launcher.ps1
powershell.exe -File 'C:\path\to\worker.ps1' -FirstName $firstName
但是,请注意,从调用方的角度来看,新进程的主机输出(
Write host
)被合并到其常规输出(Write output
)中。将您的代码拆分为两个单独的脚本,并将其中一个脚本用作第二个脚本的启动程序。类似这样的情况:
# launcher.ps1
powershell.exe -File 'C:\path\to\worker.ps1' -FirstName $firstName
但是,请注意,从调用方的角度来看,新进程的主机输出(
Write host
)被合并到其常规输出(Write output
)中。从powershell中调用powershell.exe
执行脚本块的语法有点不同:
powershell.exe -command { scriptblock content here } -args "arguments","go","here"
因此,在您的脚本中,应该是:
$lastName = powershell -Command {
Param ([string]$firstName)
$lastName = ''
if ($firstName = 'John')
{
$lastName = 'Doe'
Write-Host "Hello $firstName, your last name is registered as $lastName"
} else {
Write-Host "Last name not found"
}
Write-Output $lastName
} -args $firstName
从powershell中调用
powershell.exe执行脚本块的语法有点不同:
powershell.exe -command { scriptblock content here } -args "arguments","go","here"
因此,在您的脚本中,应该是:
$lastName = powershell -Command {
Param ([string]$firstName)
$lastName = ''
if ($firstName = 'John')
{
$lastName = 'Doe'
Write-Host "Hello $firstName, your last name is registered as $lastName"
} else {
Write-Host "Last name not found"
}
Write-Output $lastName
} -args $firstName
使用。它还为主机流提供了适当的分离。看起来很复杂。你能在回答部分给出一个例子吗?请停止在你的问题主题中添加语言标记。使用。它还为主机流提供了适当的分离。看起来很复杂。你能在回答部分给出一个例子吗?请停止添加语言标记您的问题主题中的语言标记。嗯,我希望将代码保留在一个文件中,但这似乎是最好的选择。开始作业正在我创建的表中添加不需要的属性。嗯,我希望将代码保留在一个文件中,但这似乎是最好的选择。开始作业正在t中添加不需要的属性我可以创建。哦,让我测试一下。谢谢你的回复。哦,让我测试一下。谢谢你的回复。