Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Background Job stuck when using New-PSDrive

$
0
0

Hi,

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-TypeNameSystem.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

So When i execute it as Job, 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-TypeNameSystem.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


Viewing all articles
Browse latest Browse all 6937

Trending Articles