Hi All

I recently had to write a Power Shell script to shutdown Hyper-V guests on Failover Cluster nodes.

So here goes, create a text based file with the extension .ps1. Mine is named “BackupVMs.ps1”

The contents of the file should be something like this:

[cmdletbinding()]

# Stop all VOIP HyperV Guests on Local Server – I’m defining the two node names here. Increase it to as many as you want

Param([string[]]$vmhost = ‘QCSCL02’, [string[]]$vmhost1 = ‘QCSCL01’)

 

# This line will only shutdown the guests with the word “BACKUP” in the VM description’s Notes field

#$runningVM = Get-VM -ComputerName $vmhost| where state -eq ‘running’| where notes -contains ‘BACKUP’

# This line will send the shutdown command to all guests

$runningVM = Get-VM -ComputerName $vmhost| where state -eq ‘running’

foreach ($cn in $runningVM)

{Stop-VM $cn.name}

 

[cmdletbinding()]

# Stop all VOIP HyperV Guests on Remote Server

# This line will only shutdown the guests with the word “BACKUP” in the VM description’s Notes field

#$runningVM = Get-VM -ComputerName $vmhost1| where state -eq ‘running’| where notes -contains ‘BACKUP’

# This line will send the shutdown command to all guests

$runningVM = Get-VM -ComputerName $vmhost1| where state -eq ‘running’

foreach ($cn in $runningVM)

{Stop-VM –computername $vmhost1 $cn.name}

 

 

# Sleep for 1 minute(s) – This is not really necessary, but makes me feel better

Start-Sleep -s 60

 

 

# Delete Folders, and contents, older than 61 days – CHANGE DAYS AND SET PATH

$limit = (Get-Date).AddDays(-61)

$path = “C:\Backup”

# Delete files older than the $limit.

Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

# Delete any empty directories left behind after deleting the old files.

Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse

 

 

# Create Backup Folder by Date

$date = Get-Date

$date = $date.ToString(“yyyy-MM-dd”)

New-Item -ItemType directory -Path “$path\$date”

 

 

# Copy to remote storrage – CHANGE PATH

Copy-Item -Path “C:\ClusterStorage\Volume2\HyperV” -Destination “$path\$date” –Recurse

 

# Start all HyperV Guests on Local Server

# This line will only startup guests with the word “BACKUP” in the VM description’s Notes field

#$VMtoStart = Get-VM -ComputerName $vmhost| where notes -contains ‘BACKUP’

# This line will startup all guests

$VMtoStart = Get-VM -ComputerName $vmhost

foreach ($cn in $VMtoStart)

{Start-VM $cn.name}

 

 

# Start all HyperV Guests on Remote Server

# This line will only start-up guests with the word “BACKUP” in the VM description’s Notes field

#$VMtoStart = Get-VM -ComputerName $vmhost1| where notes -contains ‘BACKUP’

# This line will start-up all guests

$VMtoStart = Get-VM -ComputerName $vmhost1| where notes -contains ‘BACKUP’

foreach ($cn in $VMtoStart)

{Start-VM –computername $vmhost1 $cn.name}

#End of file

 

That is it, let me know if something is not clear or if I can be off assistance.