Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 文件共享的启动作业混乱_Powershell - Fatal编程技术网

Powershell 文件共享的启动作业混乱

Powershell 文件共享的启动作业混乱,powershell,Powershell,所以,这绝对是在鞭策我。我创建了一个脚本,它根据用户对大量问题的回答将数据从一个文件共享移动到另一个文件共享。我想做的是运行一个后台作业,该作业提供在移动之前移动的所有文件的报告。因此,我添加了一些代码,这些代码绝对不会从源文件共享中收集信息。它只是从我的特定机器提供数据。我做错了什么 While ($sourcepath -eq $null) { $sourcepath= read-host "Enter source file path" } Set-Location $sourcepath

所以,这绝对是在鞭策我。我创建了一个脚本,它根据用户对大量问题的回答将数据从一个文件共享移动到另一个文件共享。我想做的是运行一个后台作业,该作业提供在移动之前移动的所有文件的报告。因此,我添加了一些代码,这些代码绝对不会从源文件共享中收集信息。它只是从我的特定机器提供数据。我做错了什么

While ($sourcepath -eq $null) {
$sourcepath= read-host "Enter source file path"
}
Set-Location $sourcepath
Start-job -Scriptblock {Get-childitem -recurse |Out-File 
c:\users\john.smith\desktop\shareonfile.txt}

作业在不同的进程中运行,具有自己的作用域。工作目录将不会被继承。为了证明这一点:

Set-Location $sourcepath

Start-Job -ScriptBlock {
    Get-Location
} | Wait-Job | Receive-Job

Get-Job | Remove-Job
无论如何,您应该避免设置位置,只需将路径传递到
Get ChildItem
。要在作业中执行此操作,请定义一个参数并按如下方式传递其值:

Start-job -Scriptblock { param($thePath)
    Get-childitem -Path $thePath -recurse | 
    Out-File c:\users\john.smith\desktop\shareonfile.txt
} -ArgumentList $sourcepath

令人惊叹的!非常感谢你。这很有道理。