Author Posts

September 17, 2014 at 3:05 am

Hello,

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

September 17, 2014 at 9:28 pm

It's entirely possible that the job doesn't have sufficient context to map a drive. Would it be possible to do this without a mapped drive? E.g., just provide UNCs in the Copy-Item command?

September 18, 2014 at 12:03 am

Thanks for your answer

Yes it's possible, but how to pass credentials to Copy-Item cmdlet? I know there is a parameter

-Credential

but it didn't work, because it's not supported.

Anyway, i resolve it by using

Net-use

instead of

New-Psdrive

, and it works.

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

September 18, 2014 at 12:13 am

Thanks for your answer, i can't use without mapping a drive, because i can't pass Credential parameter to Copy-Item cmdlet, it doesn't support it.

Anyway, i resolve it by using Net-use instead of New-Psdrive, and it works

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

I think there is a bug with Remote Psdrive and powershell jobs.

I'll also report it on https://connect.microsoft.com/PowerShell/Feedback

September 18, 2014 at 2:04 pm

Adeel,

You can pass the credential to the job.

Tim Curwick
MadWithPowerShell.com

$Credential = Get-Credential

Start-Job -ScriptBlock {
 
$shareadress = "\\172.22.0.100\c$"
Copy-Item "C:\test.txt" -Destination $shareadress
 
} -Credential $Credential
 
Get-Job | Wait-Job
Get-Job | Receive-Job
Remove-Job -State Completed