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.