Quantcast
Channel: Hyper-V forum
Viewing all articles
Browse latest Browse all 8743

Script to check and start VM working, but not as ScheduledJob / Scheduled Task

$
0
0

Good Day All,

I’ve seen several threads on the subject, and I feel like I’ve exhausted the recommended help offered in them.

What I have is a Windows 7 VM that likes to drop into a “Saved” state, sometimes once a month or so. I don’t know why, and considering the purpose of the machine, it’s not that big of a deal. I thought it would be easier to just write a script that checks the VM, and starts it if it’s not. This is what I’ve ended with:

Import-Module Hyper-V
$VM = Get-VMIntegrationService -VMName HVAC-Win7 -Name Heartbeat
if($VM.PrimaryStatusDescription -ne "OK"){start-vm HVAC-Win7}

Once it works, I intend to add some additional things to log that the script does to a log file, but I'm more concerned with getting it working for the time being.

This works from PowerShell as is (above), but not as a Scheduled Task or as a Scheduled Job. Here’s the syntax I used to create the Scheduled Job:

Register-ScheduledJob –Name "Keep HVAC Alive" -ScriptBlock { Import-Module Hyper-V ; $VM = Get-VMIntegrationService -VMName HVAC-Win7 -Name Heartbeat ; if($VM.PrimaryStatusDescription -ne "OK") { start-vm HVAC-Win7 } }

And

Register-ScheduledJob –Name "Keep HVAC Alive" –FilePath C:\Scripts\HVAC\TestVMStatus.ps1

I’ve created the Tasks per best practices (as far as I’m aware), the action is powershell.exe, and the argument was the path to the script.

Both the Task and the Job are configured to run even if the user is not logged on, and as highest privileges. The account used to run it has ‘logon as a batch job’ rights (no error is presented when the account is selected and the password is provided).

When the Job is configured to run as SYSTEM, it runs, but nothing happens. When it’s configured to run as me, it just hangs. When it’s configured to run as a Hyper-V Mgmt account I created and gave rights to, it runs, but does not start the VM. The logs don’t say much, just that they started and terminated as one would expect.

Any other ideas? Thank you in advance for your help!



Viewing all articles
Browse latest Browse all 8743

Trending Articles