none
Powershell script run with TaskScheduler not stopping VM's RRS feed

  • Question

  • 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

    Friday, February 21, 2014 4:28 PM

Answers

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


    Michele Cleary

    • Marked as answer by MichCl Friday, February 21, 2014 8:35 PM
    Friday, February 21, 2014 8:35 PM

All replies

  • Write all output to a log file and review the log.


    ¯\_(ツ)_/¯

    Friday, February 21, 2014 5:40 PM
  • Good idea.  I'm not used to the land of powerShell.  It looks like when I run through TaskScheduler the list of running VM's comes up empty.  

    Michele Cleary

    Friday, February 21, 2014 7:15 PM
  • Any ideas?  I have run with highest privileges checked in taskScheduler and I'm an administrators.  I saved the task with my AD info. It's acting like it did when I wasn't running as administrator in powerShell.  I wonder if it's finding the hyperV commands ok.  Not sure how to find that.

    Michele Cleary

    Friday, February 21, 2014 7:36 PM
  • I was looking at this Runas and he mentions using Runas -Verb, but I'm not finding good info on how to use Runas to run as administrator and if I can use that in TaskScheduler.

    Michele Cleary

    Friday, February 21, 2014 8:00 PM
  • I finally figured it out!  I changed it so I was using the 32 bit version of powershell in TaskScheduler:  %SystemRoot%\system32\....  Now it's finding the VM's!


    Michele Cleary

    • Marked as answer by MichCl Friday, February 21, 2014 8:35 PM
    Friday, February 21, 2014 8:35 PM
  • I finally figured it out!  I changed it so I was using the 32 bit version of powershell in TaskScheduler:  %SystemRoot%\system32\....  Now it's finding the VM's!


    Michele Cleary

    Just FYI - system32 is x64. SysWOW64 is x86.

    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Friday, February 21, 2014 8:39 PM
  • Good to know.  Thanks!

    Michele Cleary

    Friday, February 21, 2014 8:50 PM