Adeel ASIF
Adeel ASIF

Reputation: 3544

Powershell Job Stuck when using New-PsDrive

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

Upvotes: 3

Views: 2857

Answers (4)

Ieuan
Ieuan

Reputation: 148

This question was asked a long time ago but I have done some troubleshooting on a similar situation so thought I'd post here.

From what I've found, it seems to be an issue with creating the PSCredential object within the job. If it's passed as a parameter to the job, it seems to work fine. The following script illustrates this (and the workaround).

C:\scripts\MySimpleJob.ps1:

Param(
    [string] $computer,
    [pscredential] $DeploymentCredential
)

if($DeploymentCredential -eq $null)
{
    $securePass = Get-Content "C:\scripts\passwords\dijital.txt" | ConvertTo-SecureString
    $DeploymentCredential = New-Object PsCredential 'foobar\dijital', $SecurePass
}

New-PSDrive -Name TargetServer -PSProvider FileSystem -Root ('\\' + $computer + '\c$') -Credential $deploymentCredential | Out-Null

$testinglog = 'TargetServer:\temp\TEST.txt'
if(Test-Path -Path $testinglog)
{
    Remove-Item -Path $testinglog -Force
}

New-Item $testinglog -ItemType 'File' -Force
((Get-Date -Format "yyyy-MM-dd\THH\:mm:ss:fff: ") + 'I can write to this file!') | Out-File -FilePath $testinglog

If I launch this script as a job without the $DeploymentCredential parameter, it is decrypted from a file within the job and will hang:

$job = Start-Job -ScriptBlock {
                        C:\scripts\MySimpleJob.ps1 -Computer $args[0]
                       }`
          -ArgumentList @( 'MYTESTSERVER')
Wait-Job -Job $job
($job.PSEndTime - $job.PSBeginTime).TotalSeconds

However, if I pass the $DeploymentCredential parameter (which I obtain in exactly the same way), the job will complete normally in less than 1 second:

$SecurePass = Get-Content "C:\scripts\passwords\dijital.txt" | ConvertTo-SecureString
$DeploymentCredential = New-Object PsCredential 'foobar\dijital', $SecurePass

$job = Start-Job -ScriptBlock {
                        C:\scripts\MySimpleJob.ps1 -Computer $args[0] -deploymentCredential $args[1]
                       }`
          -ArgumentList @( 'MYTESTSERVER' , $DeploymentCredential)
Wait-Job -Job $job
($job.PSEndTime - $job.PSBeginTime).TotalSeconds

In PowerShell 4.0 the job will stay in the running state indefinitely. In PowerShell 5.1, it will hang for ~180 seconds and then terminate.

In both situation the file is written to correctly and I've also added logging to the very end of the job and it is definitely reaching the last line of the MySimpleJob.ps1 script.

I will raise this on the UserVoice PowerShell forum as I can't find anything raised there yet.

Upvotes: 2

Adeel ASIF
Adeel ASIF

Reputation: 3544

This is how i resolved it :

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 used Net-use instead of New-Psdrive. I think there is a bug with Remote Psdrive and powershell jobs.

It's entirely possible that the job doesn't have sufficient context to map a drive with New-PsDrive cmdlet

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

Upvotes: 2

xBr0k3n
xBr0k3n

Reputation: 415

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.

Upvotes: 1

walid toumi
walid toumi

Reputation: 2282

Try to use the parameter -init with your start-job: start-job -init { import-module yourmodule} -command { test-one...}

The module is .psm1 and not .ps1 try instead to use dot-source . C:\springfield\Citrix\CitrixDeploymentActivlanModule.ps1

set execution policy to bypass in script is not a good idea try instead to use unrestricted with the current scope

Upvotes: 0

Related Questions