0
votes

I can run my powershell script as administrator in powershell, and it yields good list of running VM's. But when I run it in TaskScheduler with highest privileges, it's showing an empty list of running VM's. We have Server 2008 R2, PowerShell V3, and I downloaded the Hyper-V module for powershell recently. I created an account on the server with Administrators privileges, and Administrators have full control for all directories that the script is copying files from/to.

Also, when I run the script through powershell, I needed to run as administrator. When I run it with the powershell prompt this is what it looks like:

C:\windows\system32> powershell -NoProfile -noninteractive -ExecutionPolicy bypass -Command "& c:\Scripts\BackupVhdShell_2_param.ps1 -single_backup_file_to_loc 'E:\' -single_backup_file_from_loc 'S:\SQL-bak.vhd'"

So that works from powreshell to start/stop vm's and copy files.

In Task Scheduler, this is how I have it set up and it yields the empty list of running VM's:

Run with highest privileges is checked. I have my login credentials saved so it can wake up the server when I'm not here or if it's not up.

In The Program/script field: %SystemRoot%\SysWow64\WindowsPowerShell\v1.0\powershell.exe

In the Add Arguments field: -NoProfile -noninteractive -ExecutionPolicy bypass -Command "& c:\Scripts\BackupVhdShell_2_param.ps1 -single_backup_file_to_loc 'E:\' -single_backup_file_from_loc 'S:\SQL-bak.vhd'"

Any thoughts? I'm not sure if TaskManager isn't finding HyperV module? Or maybe I need Runas to get it to be administrator? I'm having trouble finding info on that. This link was similar but different: http://ss64.com/nt/runas.html Same thing as this: http://peter.hahndorf.eu/blog/

This is what the majority of the script looks like. Note that I have since added logging to the file and know that this line is coming up empty when the script is run through TaskScheduler: <[array]$vmNames = @(Get-VM -Running | %{$_.elementname})>

Again, it works fine through powershell.

The script:

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"
1

1 Answers

0
votes

I finally figured it out! I changed it so I was using the other version of powershell in TaskScheduler: %SystemRoot%\system32.... Now it's finding the VM's.