no receive-job results for gci when -path is a variable

谁说我不能喝 提交于 2019-12-24 07:40:33

问题


This returns nothing:

$x = "C:\temp"
Start-Job -ScriptBlock {get-childItem -path $x} | Wait-Job | Receive-job

But providing the path parameter without a variable, like so...

Start-Job -ScriptBlock {get-childItem -path C:\temp} | Wait-Job | Receive-job

...returns the contents of that temp folder, durrr.txt in this case. This is on Windows Server 2008R2 SP1 with Powershell $host.version output as follows:

Major  Minor  Build  Revision
-----  -----  -----  --------
3      0      -1     -1

Suspecting Powershell's v3, I tried updating a Windows 7 SP1 desktop from v2 to v3, but no luck recreating the problem. On that upgraded desktop, $host.version output now matches the above.

What's going on?

EDIT / What was going on?

The busted job seems equivalent to

Start-Job -ScriptBlock {get-childItem -path $null} | Wait-Job | Receive-job

So gci returned results for the background job's current directory, the Documents folder, which happened to be empty.


回答1:


You need to pass argument to the scriptblock

$x = "C:\temp"
Start-Job -ScriptBlock {get-childItem -path $args[0]} -argumentlist $x  | Wait-Job | Receive-job

In powershell V3 you can do :

$x = "C:\windows"
Start-Job -ScriptBlock {get-childItem -path $using:x} | Wait-Job | Receive-job


来源:https://stackoverflow.com/questions/15161115/no-receive-job-results-for-gci-when-path-is-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!