我有一个如下所示的powershell脚本。
try {
$ErrorActionPreference = "Stop";
$password = convertTo-SecureString -String password@123 -AsPlainText - Force
$credential = New-Object -Type System.Management.Automation.PSCredential - ArgumentList "MyDomain\MyUsername", $password
If (Test-Path J:) {
Remove-PSDrive -Name J
New-PSDrive -Name "J" -PSProvider FileSystem - Root "\\networkmachine\sharepath\" -Persist -Crendential $credential
}
else {
New-PSDrive -Name "J" -PSProvider FileSystem - Root "\\networkmachine\sharepath\" -Persist -Crendential
}
Copy-Item "J:\mytest.txt -Destination C:\Rafiq\"
}
catch {
Write-Host "Error Occured"
$_ | out-file "C:\Rafiq\touch1.txt
}
当我手动运行上述脚本时,它可以正常工作,但是当我使用Windows Task Scheduler运行时,却出现了以下错误。
New-PSDrive : Attempt to access invalid address
At C:\Rafiq\CopyFile.ps******
+ New-PsDrive -Name "j" -PSProvider FileSystem -Root "\\networkmachine\sharepath\" -Persist ...
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (J:PSDriveInfo) [New-PSDrive], Win32Exception
+ FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand
对我来说很奇怪,为什么通过Windows Task Scheduler访问时出现错误? 有人可以帮我吗?