PowerShell脚本从TaskScheduler运行,生成运行VM的空数组';s

PowerShell脚本从TaskScheduler运行,生成运行VM的空数组';s,powershell,scheduled-tasks,powershell-3.0,hyper-v,Powershell,Scheduled Tasks,Powershell 3.0,Hyper V,我可以在powershell中以管理员身份运行我的powershell脚本,它会生成运行VM的良好列表。但当我以最高权限在TaskScheduler中运行它时,它会显示一个运行VM的空列表。我们有Server2008R2,PowerShellV3,我最近下载了PowerShell的Hyper-V模块。我在服务器上创建了一个具有管理员权限的帐户,管理员可以完全控制脚本将文件从/复制到的所有目录 另外,当我通过powershell运行脚本时,我需要以管理员身份运行。当我使用powershell提示符

我可以在powershell中以管理员身份运行我的powershell脚本,它会生成运行VM的良好列表。但当我以最高权限在TaskScheduler中运行它时,它会显示一个运行VM的空列表。我们有Server2008R2,PowerShellV3,我最近下载了PowerShell的Hyper-V模块。我在服务器上创建了一个具有管理员权限的帐户,管理员可以完全控制脚本将文件从/复制到的所有目录

另外,当我通过powershell运行脚本时,我需要以管理员身份运行。当我使用powershell提示符运行它时,它看起来是这样的:

C:\windows\system32>powershell-NoProfile-noninteractive-ExecutionPolicy bypass-Command“&C:\Scripts\BackupVhdShell\u 2\u param.ps1-single\u backup\u file\u to\u loc'E:\'-single\u backup\u file\u from\u loc'S:\SQL bak.vhd'”

因此,它可以从powreshell启动/停止vm并复制文件

在Task Scheduler中,我是这样设置的,它会生成运行VM的空列表:

选中“以最高权限运行”。我保存了登录凭据,以便在我不在或服务器未启动时唤醒服务器

在程序/脚本字段中:%SystemRoot%\SysWow64\WindowsPowerShell\v1.0\powershell.exe

在“添加参数”字段中:-NoProfile-noninteractive-ExecutionPolicy bypass-Command“&c:\Scripts\BackupVhdShell\u 2\u param.ps1-single\u backup\u file\u to\u loc'E:\'-single\u backup\u file\u from\u loc'S:\SQL bak.vhd'”

有什么想法吗?我不确定TaskManager是否没有找到HyperV模块?或者我需要Runas来让它成为管理员?我很难找到这方面的信息。此链接类似但不同:与此相同:

这就是大部分脚本的外观。请注意,我已经在文件中添加了日志记录,并且知道当脚本通过TaskScheduler运行时,这一行是空的:

同样,它可以通过powershell正常工作

剧本:

param($single_backup_file_to_loc, $single_backup_file_from_loc)

function StopVMsInOrder ([array][String]$vmNames){ 
#this function will stop VM's in list, sequentially 

   Write-Host "Processing virtual machines in order"
   foreach ($name in $vmNames) {
       Write-Host "Analyzing $name"
       Try {
             #Write-Host "...Saving $name"
             #Save-VM -VM $name -wait -Force
             Write-Host "..shutdown $name" #name)"
             Invoke-VMShutdown -VM $name -Force #$vm.name
       } #try
       Catch {
          Write-Host "Failed to get virtual machine $name"
       } #catch
   }#foreach



} #function StopVMsInOrder

function StartVMsInOrder ([array][String]$vmNames){ 
#this function will start VM's in list, sequentially as opposed to all at once

   Write-Host "Processing virtual machines in order"
   foreach ($name in $vmNames) {
       Write-Host "Analyzing $name"
       Try {
             Write-Host "..Starting $name"
             Start-VM -VM $name -wait
       } #try
       Catch {
          Write-Host "Failed to get virtual machine $name"
       } #catch
   }#foreach

} #function StartVMsInOrder

function CopyFileToFolder ([string]$Source,[string]$destination){  
   # get filename  
  ...
}  

#################start of script##############

import-module Hyperv

#get list of running vm's
[array]$vmNames = @(Get-VM -Running | %{$_.elementname})  

Write-Host "To: $single_backup_file_to_loc"
Write-Host "From: $single_backup_file_from_loc"

#call function to stop vm's
StopVMsInOrder $vmNames      

if($single_backup_file_to_loc -ne " ")
{
   #someone passed in a parameter for one-off use of script
   [array]$destFileArray = @($single_backup_file_to_loc)
   [array]$sourceFileArray = @($single_backup_file_from_loc)
}else
{
   Write-Host "To Loc not Defined as param"
   #get set up for what need to backup vhd's
   #where back it up to
}

$i=0
for ($i = $sourceFileArray.GetLowerBound(0); $i -le $sourceFileArray.GetUpperBound(0); $i++) { 
        $tempSource =  $sourceFileArray[$i]
        $tempDest = $destFileArray[$i]
        CopyFileToFolder $tempSource $tempDest
        Write-Host "i: $i"
}

Write-Host "Done with vhd backup"

#call function to start vm's
StartVMsInOrder $vmNames  

Write-Host "Done with vm start"

我终于明白了!我更改了它,以便在TaskScheduler中使用其他版本的powershell:%SystemRoot%\system32。。。。现在它正在寻找虚拟机