首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell脚本从TaskScheduler运行,生成运行VM的空数组

PowerShell脚本从TaskScheduler运行,生成运行VM的空数组
EN

Stack Overflow用户
提问于 2014-02-21 20:17:50
回答 1查看 1.7K关注 0票数 0

我可以在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,它工作得很好。

剧本:

代码语言:javascript
复制
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"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-21 20:34:26

我终于想出来了!我更改了它,以便在TaskScheduler中使用其他版本的powershell:%SystemRoot%\system32 32.现在它找到了VM的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21944340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档