Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell script run with TaskScheduler not stopping VM's

$
0
0

I have a script that stops and starts the VM's, and copies files and it works ok when I run it with powershell, but when I run it with TaskScheduler, it's not stopping/starting the VM's (but does copy files).  We have server 2008 R2.  I updated powershell to version 3 and downloaded and installed the Hypver-V powershell module from Microsoft.

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:

Run with highest priviledges 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?

This is what the majority of the script looks like:

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"



Michele Cleary


Viewing all articles
Browse latest Browse all 15028

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>