关于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

posted @ 2023-02-22 20:37  talentzemin  阅读(45)  评论(0编辑  收藏  举报