Reputation: 115
I am creating a PowerShell script to be run as a scheduled task. It's purpose is to create a snapshot on each of our virtual machines every Monday. I've created a short script for each VM. After establishing a remote session to our local Hyper-V server, it should simply find our virtual machine and create a Snapshot. This is what it looks like when testing:
PS C:\Users\crhoden\Documents\Scripts\HyperV Snapshot Job> .\win7
Name State CPUUsage(%) MemoryAssigned(M) Uptime Status Version
---- ----- ----------- ----------------- ------ ------ -------
Windows 10 Professional Off 0 0 00:00:00 Operating normally 7.0
Windows 7 Professional Pre-Alpha Saved 0 0 00:00:00 Operating normally 7.0
Windows 8.1 Professional Pre-Alpha Off 0 0 00:00:00 Operating normally 7.0
Windows Server 2012 R2 Off 0 0 00:00:00 Operating normally 7.0
Get-VMSnapshot : Hyper-V was unable to find a virtual machine with name "Windows 7 Pro".
At C:\Users\crhoden\Documents\Scripts\HyperV Snapshot Job\Win7.ps1:6 char:1
+ Get-VMSnapshot -VMName $vmname | Where-Object {$_.CreationTime -lt (G ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-VMSnapshot], VirtualizationException
+ FullyQualifiedErrorId : ObjectNotFound,Microsoft.HyperV.PowerShell.Commands.GetVMSnapshot
Checkpoint-VM : Hyper-V was unable to find a virtual machine with name "Windows 7 Pro".
At C:\Users\crhoden\Documents\Scripts\HyperV Snapshot Job\Win7.ps1:7 char:1
+ Checkpoint-VM -Name $vmname -SnapshotName "Weekly Snapshot $((Get-Dat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Checkpoint-VM], VirtualizationException
+ FullyQualifiedErrorId : ObjectNotFound,Microsoft.HyperV.PowerShell.Commands.CheckpointVM
[hypervserver]: PS C:\Users\crhoden\Documents> get-vm
Name State CPUUsage(%) MemoryAssigned(M) Uptime Status
---- ----- ----------- ----------------- ------ ------
Windows 10 Pro Running 1 1172 1.00:49:27 Operating normally
Windows 7 Pro Running 0 1024 10:54:07 Operating normally
Windows 8 Pro Running 0 1159 16.22:02:43 Operating normally
Windows 8.1 Pro Running 1 1716 16.21:43:03 Operating normally
[hypervserver]: PS C:\Users\crhoden\Documents>
As you can probably tell, when I manually run a "get-vm", the remote machines turn up just fine. But when ran as a script, it still searches my workstation instead. Here are the contents of the script:
Enter-pssession -computername hypervserver
start-sleep -s 10
cd C:\
$vmname = 'Windows 7 Pro'
get-vm
Get-VMSnapshot -VMName $vmname | Where-Object {$_.CreationTime -lt (Get-Date).AddDays(-15) } | Remove-VMSnapshot
Checkpoint-VM -Name $vmname -SnapshotName "Weekly Snapshot $((Get-Date).toshortdatestring())"
I added the start-sleep command as an attempted fix, thinking commands were firing before the connection was established. No such luck. The kicker is if I run through this script line by line, it works just fine. Any help is appreciated. EDIT: It also works perfect when pasting in the contents of the script in their entirety.
Upvotes: 2
Views: 697
Reputation:
Enter-PSSession
is for interactive sessions only, so working interactively or pasting into a session does work, but not in a script which is by nature not interactive.
You can either
-ComputerName
parameter of Get-VMSnapShot
As you discovered by yourself the used cmdlet has to match the Hyper-Vserver version.
Upvotes: 1