下面的行在 Powershell 提示符下工作正常,但在计划任务中失败。
$pass = gc C:\secrets\shhh.txt | convertTo-secureString
$Cred = new-object -typeName System.management.automation.psCredential -argumentlist "domain\domainUser",$pass
$path = "\\server\share\folder"
$j = start-job {gci -path $args[0]} -cred $Cred -argumentList $path | wait-job | receive-job
$body = $j | out-string
$error | out-file C:\temp\err.txt
send-mailMessage -to me@domain.tld -from server@domain.tld -subject halp -smtpserver mailserver.domain.tld -body $body
在 c:\temp\err.txt 中,Windows 2008R2 计划任务留下了一个面包屑:
[localhost] The background process exited abnormally.
+ CategoryInfo : OpenError: (localhost:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : 2101,PSSessionStateBroken
...这将我们带到了这个 Microsoft 错误报告。该报告提到了使用 localhost 环回远程处理的解决方法,这听起来有点脏。我应该去那里吗?
有更好的解决方案吗?也许使用 *session cmdlet 或调用命令之一?计划任务的Start in
值已设置,但后台进程可能以某种不好的方式使用了一些变量,像这样?
–version 2
使用或使用-command "& {bigKlugeyScriptblock}"
语法调用 powershell.exe 还没有运气。
编辑:我试图避免创建一个新的域帐户来运行该任务。该任务不能domainUser
从 from运行$cred
,因为该帐户不应该具有 localhost 的权限。