5

The Powershell job is blocked when using New-PsDrive

 2 years ago
source link: https://www.codesd.com/item/the-powershell-job-is-blocked-when-using-new-psdrive.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The Powershell job is blocked when using New-PsDrive

advertisements

I've a little Powershell script, it creates New Background Job, who contains New-PsDrive and 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

When i execute it, i got this, and it never end... it's stuck in Running State :

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

When i execute it without Job, it works :

$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

Can someone explain me why? I searched a lot on Google, but can't find any explanation..

Thanks for your help


Not that this is any help to you in this situation. But, if you cant resolve it post the info here:

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

Plenty of bugs get reported, other users let you know if they can replicate it or if they have a workaround. Then Microsoft eventually gets around to fixing it....maybe.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK