2014-02-21 2 views
0

powershell에서 내 powershell 스크립트를 관리자로 실행할 수 있으며 실행중인 VM의 목록이 좋습니다. 하지만 TaskScheduler에서 가장 높은 권한으로 실행할 때 빈 VM 목록을 보여줍니다. 우리에게는 Server 2008 R2, PowerShell V3이 있으며, 최근에 powershell 용 Hyper-V 모듈을 다운로드했습니다. 나는 관리자 권한을 가진 서버에 계정을 만들었고 관리자는 스크립트가 파일을 복사하는 모든 디렉토리를 완전히 제어 할 수 있습니다.TaskScheduler에서 PowerShell 스크립트를 실행하여 실행중인 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에서 VM의 파일을 시작/중지하고 파일을 복사 할 수 있습니다. 나는 그것이 설정 얼마나 작업 스케줄러에서

, 이것은이며 VM의 실행의 빈리스트 산출 : 가장 높은 수준의 권한을 가진

실행이 선택되어 있습니다. 내 로그인 정보가 저장되어있어 내가 여기에 없거나 일어나지 않을 때 서버를 깨울 수 있습니다. 프로그램/스크립트 필드에

: % SYSTEMROOT % \ SysWOW64와 \의 WindowsPowerShell \ 추가 인수 필드에 버전 1.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 -Running | % {$ _. 요소 이름}) >

다시 말해서 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" 

답변

0

나는 마침내 그것을 알아 냈어! PowerShell의 다른 버전을 TaskScheduler에서 사용하기 위해 그것을 변경했습니다 : % SystemRoot % \ system32 .... 이제 VM을 찾습니다.

관련 문제