Powershell 当-path为变量时,不接收gci的作业结果
这将不返回任何内容:Powershell 当-path为变量时,不接收gci的作业结果,powershell,powershell-3.0,get-childitem,powershell-jobs,Powershell,Powershell 3.0,Get Childitem,Powershell Jobs,这将不返回任何内容: $x = "C:\temp" Start-Job -ScriptBlock {get-childItem -path $x} | Wait-Job | Receive-job 但是提供不带变量的路径参数,就像这样 Start-Job -ScriptBlock {get-childItem -path C:\temp} | Wait-Job | Receive-job …返回临时文件夹的内容,在本例中为durrr.txt。这是在Windows Server 2008R2
$x = "C:\temp"
Start-Job -ScriptBlock {get-childItem -path $x} | Wait-Job | Receive-job
但是提供不带变量的路径参数,就像这样
Start-Job -ScriptBlock {get-childItem -path C:\temp} | Wait-Job | Receive-job
…返回临时文件夹的内容,在本例中为durrr.txt。这是在Windows Server 2008R2 SP1上,Powershell$host.version输出如下:
Major Minor Build Revision
----- ----- ----- --------
3 0 -1 -1
我怀疑Powershell是v3版本,于是尝试将Windows7SP1桌面从v2更新到v3,但没能重现问题。在升级的桌面上,$host.version输出现在与上面的匹配
发生什么事了
编辑/发生了什么事?
被解雇的工作似乎相当于
Start-Job -ScriptBlock {get-childItem -path $null} | Wait-Job | Receive-job
因此,gci返回了后台作业当前目录Documents文件夹的结果,该目录恰好为空。您需要将参数传递给scriptblock
$x = "C:\temp"
Start-Job -ScriptBlock {get-childItem -path $args[0]} -argumentlist $x | Wait-Job | Receive-job
在powershell V3中,您可以执行以下操作:
$x = "C:\windows"
Start-Job -ScriptBlock {get-childItem -path $using:x} | Wait-Job | Receive-job
您需要将参数传递给scriptblock
$x = "C:\temp"
Start-Job -ScriptBlock {get-childItem -path $args[0]} -argumentlist $x | Wait-Job | Receive-job
在powershell V3中,您可以执行以下操作:
$x = "C:\windows"
Start-Job -ScriptBlock {get-childItem -path $using:x} | Wait-Job | Receive-job
哈利路亚,谢谢。但是为什么Win7和2008R2上的行为不同呢?您的代码在xp、720008和2008R2上的powershell v2.0/v3.0中也不起作用。在sciptblock中传递变量的唯一方法是使用argumentlist参数输入。在v3中,我认为您可以使用
-path$using:x
,它可以正常工作。我将仔细阅读scriptblock参数。@noam请更准确地尝试ook,您会发现结果不是您的目录c:\temp。在“我的7”框中,返回%env:userprofile\documents…的目录。我试着自己投票,但谢天谢地这是不允许的。哈利路亚,谢谢。但是为什么Win7和2008R2上的行为不同呢?您的代码在xp、720008和2008R2上的powershell v2.0/v3.0中也不起作用。在sciptblock中传递变量的唯一方法是使用argumentlist参数输入。在v3中,我认为您可以使用-path$using:x
,它可以正常工作。我将仔细阅读scriptblock参数。@noam请更准确地尝试ook,您会发现结果不是您的目录c:\temp。在“我的7”框中,返回%env:userprofile\documents…的目录。我试着自己投票,但谢天谢地,这是不允许的。