我可以在powershell中作为管理员运行我的powershell脚本,它提供了运行VM的很好的列表。但是当我以最高的权限在TaskScheduler中运行它时,它显示的是运行VM的空列表。我们有Server 2008 R2,PowerShell V3,最近我下载了powershell的Hyper模块。我使用管理员权限在服务器上创建了一个帐户,管理员可以完全控制脚本正在将文件从/复制到的所有目录。
另外,当我通过powershell运行脚本时,我需要作为管理员运行。当我使用powershell提示符运行它时,如下所示:
C:\windows\system32> powershell -NoProfile -noninteractive -ExecutionPolicy旁路-Command "& c:\Scripts\BackupVhdShell_2_param.ps1 -single_backup_file_to_loc 'E:\‘-single_backup_file_from_loc’S:\SQLBak.vhd‘“
因此,这可以从电源地狱启动/停止vm和复制文件。
在中,我这样设置它,并生成运行VM的空列表:
以最高权限运行将被选中。我保存了我的登录凭据,这样它就可以在我不在的时候或者没有打开的时候唤醒服务器。
在Program/script字段中:%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运行时,这一行将为空: )
再一次,通过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"发布于 2014-02-21 20:34:26
我终于想出来了!我更改了它,以便在TaskScheduler中使用其他版本的powershell:%SystemRoot%\system32 32.现在它找到了VM的。
https://stackoverflow.com/questions/21944340
复制相似问题