0

I am trying to move files from a DMZ server to a domain server via powershell I can get it to work from an interactive session however if I try to run it under a scheduled task the copy fails. I added transcript start command to try and see what might be happening but I am not sure how to troubleshoot it. Are you not allowed to run PSDrive within a Scheduled Task?

Start-Transcript -path C:\temp\pslog.txt -append -NoClobber -IncludeInvocationHeader 

$password = convertto-securestring -String 'somepassword' -AsPlainText -Force
#$pass = Get-Content C:\ITScripts\password.txt | ConvertTo-SecureString

$cred = new-object -typename System.Management.Automation.PSCredential('username@domain', $password)
#$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "username@domain",$pass
 
New-PSDrive -Name Z -PSProvider filesystem -Root "\\DOMAINSERVER\FOLDERNAME" -Credential $creds
Get-ChildItem "LOCALPATH" -Filter *.txt | Move-Item -Destination "Z:\Inbound" -Force -PassThru

Remove-PSDrive Z

Stop-Transcript

So if I run this from the desktop or a powershell session it works ok when I run it from a scheduled task I get the following output.


Transcript started, output file is C:\temp\pslog.txt

New-PSDrive : The specified drive root "\DOMAINSERVER\FOLDERNAME" either does not exist, or it is not a folder. At C:\ITScripts\CopyFilesBetweenServer.ps1:11 char:1

  • New-PSDrive -Name Z -PSProvider filesystem -Root "\DOMAINSERVER\FOLDERNAME ...
  •   + CategoryInfo          : ReadError: (Z:PSDriveInfo) [New-PSDrive], IOException
      + FullyQualifiedErrorId : DriveRootError,Microsoft.PowerShell.Commands.NewPSDriveCommand
    

New-PSDrive : The specified drive root "\DOMAINSERVER\FOLDERNAME" either does not exist, or it is not a folder. At C:\ITScripts\CopyFilesBetweenServer.ps1:11 char:1

  • New-PSDrive -Name Z -PSProvider filesystem -Root "\DOMAINSERVER\FOLDERNAME ...
  •   + CategoryInfo          : ReadError: (Z:PSDriveInfo) [New-PSDrive], IOException
      + FullyQualifiedErrorId : DriveRootError,Microsoft.PowerShell.Commands.NewPSDriveCommand
    
    

Remove-PSDrive : Cannot find drive. A drive with the name 'Z' does not exist. At C:\ITScripts\CopyFilesBetweenServer.ps1:14 char:1

  • Remove-PSDrive Z
  •   + CategoryInfo          : ObjectNotFound: (Z:String) [Remove-PSDrive], DriveNotFoundException
      + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemovePSDriveCommand
    

Remove-PSDrive : Cannot find drive. A drive with the name 'Z' does not exist. At C:\ITScripts\CopyFilesBetweenServer.ps1:14 char:1

  • Remove-PSDrive Z
  •   + CategoryInfo          : ObjectNotFound: (Z:String) [Remove-PSDrive], DriveNotFoundException
      + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemovePSDriveCommand
    
    

Thanks for any help you can provide.

  • 1
    Notice that some of your unc paths start with a single slash...those don't work. The ones with 2 slashes work. – Mike Shepard Oct 13 '20 at 16:19
  • 1
    Yeah that is just on the transcript output and is my fault as I was removing the real names. The code snippet is \\ – WelshPretender Oct 14 '20 at 08:27
  • Is the scheduled task running as an account that has access to the network paths in question? – Mike Shepard Oct 14 '20 at 13:39
  • No the server is not domain joined so I have used the -Credential switch pass through a domain account that has access to the mapped drive. The scheduled task at the moment is running with a local admin account. – WelshPretender Oct 14 '20 at 17:10
  • If you dump the credentials to disk from PowerShell, using Export-CliXml under the account which is running the scheduled task, and then import the credentials using Import-CliXML does that work? The credentials would only be readable on that machine, using that account, so you can't just copy/paste the credential file. – RobbieCrash Oct 19 '20 at 22:07

0 Answers0