Powershell作业在使用New-PsDrive时失效

时间:2014-09-16 15:48:52

标签: windows powershell background-process powershell-v3.0

我有一个小的Powershell脚本,它会创建新的后台作业,其中包含New-PsDrive和Copy-Item。

Start-Job -ScriptBlock {

$shareadress = "\\172.22.0.100\c$"
$username = "Springfield\Administrator"
$pwd = "MyPassword"

$password = ConvertTo-SecureString -AsPlainText -Force -String $pwd
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$password

New-PSDrive TEMPO -PSProvider filesystem -Root $shareadress -Credential $credentials -Scope global
Copy-Item "C:\test.txt" -Destination "TEMPO:\test.txt"

Remove-PSDrive TEMPO

}

Get-Job | Wait-Job
Get-Job | Receive-Job

Remove-Job -State Completed

当我执行它时,我得到了它,它永远不会结束......它被困在了跑步状态:

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
28     Job28           BackgroundJob   Running       True            localhost            ...                      

当我执行没有作业时,正常

$shareadress = "\\172.22.0.100\c$"
$username = "Springfield\Administrator"
$pwd = "MyPassword"

$password = ConvertTo-SecureString -AsPlainText -Force -String $pwd
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$password

New-PSDrive TEMPO -PSProvider filesystem -Root $shareadress -Credential $credentials -Scope global
Copy-Item "C:\test.txt" -Destination "TEMPO:\test.txt"

Remove-PSDrive TEMPO

有人可以解释我为什么吗?我在Google上搜索了很多,但无法找到任何解释..

感谢您的帮助

4 个答案:

答案 0 :(得分:1)

在这种情况下,这对你没有任何帮助。但是,如果你不能解决它,请在这里发布信息:

https://connect.microsoft.com/PowerShell/Feedback

报告了大量错误,其他用户会通知您是否可以复制错误或是否有解决方法。然后微软最终解决它......也许吧。

答案 1 :(得分:1)

这就是我如何解决它:

Start-Job -ScriptBlock {

$destination = $letter+"\test.txt"
$letter = ls function:[d-z]: -n | ?{ !(test-path $_) } | random
$shareadress = "\\172.22.0.100\c$"
$username = "Springfield\Administrator"
$password = "MyPassword"

Net use $letter $shareadress /user:$username $pwd | Out-Null

Copy-Item "C:\test.txt" -Destination $destination

Net use $letter /delete | Out-Null

}

Get-Job | Wait-Job
Get-Job | Receive-Job

Remove-Job -State Completed

我使用Net-use代替New-Psdrive。 我认为Remote Psdrive和powershell工作存在一个错误。

完全有可能该作业没有足够的上下文来映射具有New-PsDrive cmdlet的驱动器

我还会在https://connect.microsoft.com/PowerShell/Feedback

上报告

答案 2 :(得分:1)

很久以前就问过这个问题,但是我已经针对类似的情况做了一些故障排除,所以我想在这里发帖。

从我发现的情况来看,在作业中创建Activity对象似乎是一个问题。如果它作为参数传递给作业,它似乎工作正常。以下脚本说明了这一点(以及解决方法)。

C:\脚本\ MySimpleJob.ps1:

PSCredential

如果我将此脚本作为没有Param( [string] $computer, [pscredential] $DeploymentCredential ) if($DeploymentCredential -eq $null) { $securePass = Get-Content "C:\scripts\passwords\dijital.txt" | ConvertTo-SecureString $DeploymentCredential = New-Object PsCredential 'foobar\dijital', $SecurePass } New-PSDrive -Name TargetServer -PSProvider FileSystem -Root ('\\' + $computer + '\c$') -Credential $deploymentCredential | Out-Null $testinglog = 'TargetServer:\temp\TEST.txt' if(Test-Path -Path $testinglog) { Remove-Item -Path $testinglog -Force } New-Item $testinglog -ItemType 'File' -Force ((Get-Date -Format "yyyy-MM-dd\THH\:mm:ss:fff: ") + 'I can write to this file!') | Out-File -FilePath $testinglog 参数的作业启动,则会从作业中的文件解密并挂起:

$DeploymentCredential

但是,如果我传递$job = Start-Job -ScriptBlock { C:\scripts\MySimpleJob.ps1 -Computer $args[0] }` -ArgumentList @( 'MYTESTSERVER') Wait-Job -Job $job ($job.PSEndTime - $job.PSBeginTime).TotalSeconds 参数(我以完全相同的方式获得),则作业将在不到1秒的时间内正常完成:

$DeploymentCredential

在PowerShell 4.0中,作业将无限期地保持在运行状态。在PowerShell 5.1中,它将挂起约180秒然后终止。

在这两种情况下,文件都被正确写入,我还将日志记录添加到作业的最后,它肯定已到达$SecurePass = Get-Content "C:\scripts\passwords\dijital.txt" | ConvertTo-SecureString $DeploymentCredential = New-Object PsCredential 'foobar\dijital', $SecurePass $job = Start-Job -ScriptBlock { C:\scripts\MySimpleJob.ps1 -Computer $args[0] -deploymentCredential $args[1] }` -ArgumentList @( 'MYTESTSERVER' , $DeploymentCredential) Wait-Job -Job $job ($job.PSEndTime - $job.PSBeginTime).TotalSeconds 脚本的最后一行。

我会在UserVoice PowerShell论坛上提出这个问题,因为我找不到任何东西。

答案 3 :(得分:0)

尝试将参数-init与起始作业一起使用:start-job -init { import-module yourmodule} -command { test-one...}

模块是.psm1而不是.ps1尝试使用点源. C:\springfield\Citrix\CitrixDeploymentActivlanModule.ps1

将执行策略设置为在脚本中绕过并不是一个好主意,而是尝试使用不受限制的当前范围