关于PowerShell job中使用New-PSDrive 的 Bug
背景
Start-Job -ScriptBlock {
$shareadress = "\\127.0.0.1\E$"
$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
Copy-Item "C:\test.txt" -Destination "TEMPO:\test.txt"
}
结果
job的状态始终处于Running状态
解决方案
使用net use
代替New-PSDrive
命令
Ref
https://community.spiceworks.com/topic/585560-background-job-stuck-when-using-remote-new-psdrive
https://stackoverflow.com/questions/25873217/powershell-job-stuck-when-using-new-psdrive
https://github.com/PowerShell/PowerShell/issues/2884