我可以运行我的PowerShell脚本在PowerShell的管理员,并且它产生运行虚拟机的好名单。但是当我以最高权限在TaskScheduler中运行它时,它显示了一个正在运行虚拟机的空列表。我们有Server 2008 R2,PowerShell V3和我最近为PowerShell下载了Hyper-V模块。我使用管理员权限在服务器上创建了一个帐户,并且管理员可以完全控制脚本将文件从/复制到的所有目录。PowerShell脚本从运行的TaskScheduler运行产生的空数组VM的
此外,当我通过PowerShell中运行该脚本,我需要以管理员身份运行。当我用PowerShell中运行它提示这是什么样子:
C:\ Windows \ System32下>的PowerShell -NoProfile -nonInteractive -ExecutionPolicy旁路-Command“& C:\脚本\ BackupVhdShell_2_param.ps1 -single_backup_file_to_loc“E :\” -single_backup_file_from_loc 'S:\ SQL-bak.vhd'”
所以,从powreshell工程启动/停止虚拟机和复制文件。
在任务计划程序,这就是我把它设置和它产生运行虚拟机的空列表:
使用最高权限运行进行检查。我保存了我的登录凭据,以便在我不在时或者未启动时唤醒服务器。
在程序/脚本领域中:%SystemRoot%\ SYSWOW64 \ WindowsPowerShell \ V1.0 \ powershell.exe
在添加参数字段:-NoProfile -nonInteractive -ExecutionPolicy旁路-Command“& C:\ Scripts \ BackupVhdShell_2_param.ps1 -single_backup_file_to_loc'E:\'-single_backup_file_from_loc'S:\ SQL-bak.vhd'“
有什么想法?我不确定TaskManager是否找不到HyperV模块?或者,也许我需要Runas让它成为管理员?我在查找相关信息时遇到了问题。此链接类似但不同:http://ss64.com/nt/runas.html与此相同:http://peter.hahndorf.eu/blog/
这是脚本的大部分内容。请注意,因为我已经添加记录到文件,并知道这行就要到了空当脚本通过的TaskScheduler运行:< [数组] $ vmNames = @(GET-VM - 运行|%{$ _的ElementName}) >
再次,它工作正常通过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"