Powershell working stuck when using New-PsDrive
I have a little Powershell script, it creates a new background job that 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 ends ... it is stuck in a working 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 to me why? I've searched a lot on Google but can't find any explanation.
thanks for the help
source to share
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
New-Psdrive instead. I think there is a bug with Psdrive and powershell remote apps.
It is very possible that the job does not have enough context to map the drive using the New-PsDrive
cmdlet
I will also report this https://connect.microsoft.com/PowerShell/Feedback
source to share
Not that it helps you in this situation. But, if you cannot solve this problem, please report here:
https://connect.microsoft.com/PowerShell/Feedback
Lots of bugs reported, other users are telling you if they can replicate it or if they have a workaround. Then Microsoft is finally going to fix it ... maybe.
source to share
This question was asked a long time ago, but I did some troubleshooting in a similar situation, so I thought I would post here.
From what I found there seems to be a problem with creating an object PSCredential
in the assignment. If passed as a parameter to a job, it works fine. The following script illustrates this (and a 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 run this script as a job with no parameter $DeploymentCredential
, it decrypts from a file in the job and hangs:
$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 in a parameter $DeploymentCredential
(which I receive in exactly the same way), the job completes 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 remain operational indefinitely. In PowerShell 5.1 it will hang for ~ 180 seconds and then stop.
In both situations, the file writes correctly and I also added the entry at the very end of the job and it definitely reaches the last line of the MySimpleJob.ps1
script.
I'll bring this up on the UserVoice PowerShell forum since I can't find anything else yet.
source to share
Try using the parameter -init
with your start assignment:start-job -init { import-module yourmodule} -command { test-one...}
The module is .psm1, not .ps1 try using dot-source instead . C:\springfield\Citrix\CitrixDeploymentActivlanModule.ps1
set the execution policy to bypass in the script, it is not recommended to use unrestricted use of the current scope instead
source to share