Powershell VMWare脚本不从计划任务运行

背景/目标

我正在尝试创build一个计划任务,将:

  • 在指定的时间运行(一次)
  • 运行一个PowerShell脚本
    • 将其全部输出logging为转录本
    • 使用encryption的存储凭据连接到我的vCenter服务器
    • closures某个文件夹中所有虚拟机的客户操作系统

问题

该脚本完全从控制台执行,但甚至不会开始执行任务。

代码

#Stop an error from occurring when a transcript is already stopped $ErrorActionPreference="SilentlyContinue" Stop-Transcript | out-null #Reset the error level before starting the transcript $ErrorActionPreference="Continue" Start-Transcript -path C:\temp\Shutdown_NonProductionVMs.log -append #Add the VMWare Snap-in Add-PSSnapin -Name VMWare.VimAutomation.Core #Get the Credentials $creds = Get-VICredentialStoreItem -file C:\temp\pscreds.creds #Connect to the server using the credentials file Connect-VIServer -Server $creds.host -User $creds.User -Password $creds.Password #Get all VMs within the non-production VMs folder and then shut them down #NOTE the -WhatIf tag, which will stop it from shutting down the VMs until we're sure the script is good. Get-Folder -Name "03. Non-Production VMs" | Get-VM | Shutdown-VMGuest -WhatIf #Clean Up Disconnect-VIServer -Force -Confirm:$false Remove-PSSnapin -Name VMWare.VimAutomation.Core Stop-Transcript 

我已经检查/迄今尝试和其他线索

  • 同样,当控制台以pipe理员身份打开时,此脚本在控制台中运行得非常好。
  • 我已经设置了一个“无限制”的机器级执行策略,看看是否有帮助。
  • 我以与创build凭证文件的用户相同的方式运行此任务
  • 我正在运行具有最高权限的任务。
  • 我使用存储的凭据(我的用户凭据 – 与控制台中工作的相同)运行任务。
  • 我试着评论与成绩单相关的代码,看看是否做到了; 没有骰子。
  • 任务只是继续运行。 我没有得到任何结果,并没有终止。

有任何想法吗? 在此先感谢您的帮助!

如果您在交互式“控制台”会话中启动Powershell,则最有可能启动64位版本的Powershell。 (可执行文件将驻留在Windows \ System32中。)还可以通过执行驻留在Windows \ SysWOW64中的版本从64位Windows启动32位Powershell。 确保您知道哪个版本的Task Scheduler正在启动。

请记住,每个Powershell版本都有一个完全独立的执行策略。 所以你需要设置执行策略两次,以覆盖Powershell的32位和64位版本。

您尝试加载的pipe理单元也很重要。 某些pipe理单元和COM对象只能在32位或64位环境中加载。 但是,我刚刚在安装了PowerCLI的Server 2008 R2服务器上进行了testing,并且在加载在两种环境中引用的pipe理单元时都没有问题。 所以我不认为这是这个问题。 但是这仍然是需要考虑的事情。 这可能取决于您安装的PowerCLI版本。 确保从32位和64位PSshell加载pipe理单元。

最后,你能告诉我们你的计划任务中的操作屏幕是什么意思吗? 你正在运行什么确切的命令行(W /参数)? 如果运行诸如cmd /c powershell.exe c:\yourscript.ps1 2>&1> C:\debug.txt您可能会得到更多的debugging信息

它也可能有助于从32位和64位环境中为我们提供Get-ExecutionPolicy -List的输出。

另外在你的post中你提到:

任务只是继续运行。 我没有得到任何结果,并没有终止。

这通常表示它正在等待用户input。

编辑:错别字,错别字eveyrwhere