From d70d246e3416b4523ca2716815792cf8a90e9748 Mon Sep 17 00:00:00 2001 From: Kamen Nikolov Date: Mon, 31 Jul 2023 16:23:56 +0300 Subject: [PATCH] Added Power Actions sample actions Signed-off-by: Kamen Nikolov --- .../Example - ClusterSnapshotsReport.ps1 | 51 +++++++++++++++++++ .../Example - RemoveOldClusterSnapshots.ps1 | 17 +++++++ 2 files changed, 68 insertions(+) create mode 100644 PowerActions/Example - ClusterSnapshotsReport.ps1 create mode 100644 PowerActions/Example - RemoveOldClusterSnapshots.ps1 diff --git a/PowerActions/Example - ClusterSnapshotsReport.ps1 b/PowerActions/Example - ClusterSnapshotsReport.ps1 new file mode 100644 index 0000000..261a7c0 --- /dev/null +++ b/PowerActions/Example - ClusterSnapshotsReport.ps1 @@ -0,0 +1,51 @@ +param ( + [Parameter(Mandatory=$true)] + [VMware.VimAutomation.ViCore.Types.V1.Inventory.Cluster[]] $cluster, + [Parameter(Mandatory=$true)] + [string] $smtp, + [Parameter(Mandatory=$true)] + [string] $email) + +$vms = Get-VM -Location $cluster +$snapshots = @() +foreach ($vm in $vms) { + $snapshots += Get-Snapshot -VM $vm +} + +$header = @" + +"@ + +$snapshots | select Name,VM,Created,@{Name="Size";Expression={[math]::Round($_.SizeMB,3)}},IsCurrent | ` + ConvertTo-Html -head $header | Out-File "SnapshotReport.html" + +Send-MailMessage -from "Snapshot Reports " ` + -to $email ` + -subject "Snapshot Report" ` + -body "Cluster snapshot report" ` + -Attachment "SnapshotReport.html" ` + -smtpServer $smtp \ No newline at end of file diff --git a/PowerActions/Example - RemoveOldClusterSnapshots.ps1 b/PowerActions/Example - RemoveOldClusterSnapshots.ps1 new file mode 100644 index 0000000..698a15d --- /dev/null +++ b/PowerActions/Example - RemoveOldClusterSnapshots.ps1 @@ -0,0 +1,17 @@ +param ( + [Parameter(Mandatory=$true)] + [VMware.VimAutomation.ViCore.Types.V1.Inventory.Cluster] $cluster, + [DateTime] $date) + +if ($null -eq $date) { + $date = (Get-Date).AddDays(-7) +} + +$vms = Get-VM -Location $cluster +foreach ($vm in $vms) { + $snaphostsToBeRemoved = Get-Snapshot -VM $vm | where {$_.Created -lt $date} + if ($null -ne $snaphostsToBeRemoved) { + Write-Host "Removing snapshots: " + $snaphostsToBeRemoved + " of VM: " + $vm + Remove-Snapshot $snaphostsToBeRemoved -Confirm:$false + } +} \ No newline at end of file