{"id":1266,"date":"2023-10-25T11:45:09","date_gmt":"2023-10-25T11:45:09","guid":{"rendered":"http:\/\/ihni.uk\/?p=1266"},"modified":"2023-10-25T11:45:09","modified_gmt":"2023-10-25T11:45:09","slug":"vmware-snapshots-report","status":"publish","type":"post","link":"https:\/\/ihni.uk\/?p=1266","title":{"rendered":"VMWare Snapshots Report"},"content":{"rendered":"<p>A quick powershell script using HTML and CSS to run against your VMware Environment utilising PowerCLI to pull back a report of your snapshots.<\/p>\n<p>This can be useful for reclaiming space and making the most of your storage or to spot somewhere that you have a usage issue.<br \/>\n<!--more--><br \/>\nThis does make the assumption you are using the same credentials to log into each of your VCentres, if you are using differing ones, a service account with the correct permissions on each could be created to run this report.<\/p>\n<p>[powershell]<br \/>\n#Import the PowerCLI module<br \/>\nImport-Module VMware.PowerCLI<\/p>\n<p># Record the start time for measuring execution time<br \/>\n$startTime = Get-Date<\/p>\n<p># Get valid credentials<br \/>\n$creds = Get-Credential<br \/>\n$vcsausername = $creds.UserName<\/p>\n<p># Create an array to store VM snapshot data<br \/>\n$vmData = @()<\/p>\n<p># List of vCenter servers (modify as needed)<br \/>\n$vCenterServers = @(&quot;vcsa1.your.domain.com&quot;, &quot;vcsa2.your.domain.com&quot;)<\/p>\n<p>foreach ($vCenterServer in $vCenterServers) {<br \/>\n    # Connect to vCenter server<br \/>\n    Connect-VIServer -Server $vCenterServer -Credential $creds<\/p>\n<p>    # Retrieve VMs with snapshots<br \/>\n    $vmsWithSnapshots = Get-VM | Where-Object { $_ | Get-Snapshot -ErrorAction SilentlyContinue }<\/p>\n<p>    # Iterate through VMs with snapshots<br \/>\n    foreach ($vm in $vmsWithSnapshots) {<br \/>\n        $snapshots = $vm | Get-Snapshot<\/p>\n<p>        foreach ($snapshot in $snapshots) {<br \/>\n            # Calculate snapshot age in days<br \/>\n            $snapshotAge = (Get-Date) &#8211; $snapshot.Created<\/p>\n<p>            $vmData += [PSCustomObject]@{<br \/>\n                VDC = $vm.VMHost.Parent.Name<br \/>\n                VM = $vm.Name<br \/>\n                SnapshotName = $snapshot.Name<br \/>\n                SnapshotDescription = $snapshot.Description<br \/>\n                SnapshotSize = &quot;{0:N2} GB&quot; -f $snapshot.SizeGB<br \/>\n                SnapshotCreated = $snapshot.Created<br \/>\n                SnapshotAgeInDays = [math]::Round($snapshotAge.TotalDays, 2)<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    # Disconnect from vCenter server<br \/>\n    Disconnect-VIServer -Server $vCenterServer -Confirm:$false<br \/>\n}<\/p>\n<p># Sort the results by SnapshotAgeInDays from largest to smallest<br \/>\n$vmData = $vmData | Sort-Object -Property SnapshotAgeInDays -Descending<\/p>\n<p># Custom CSS for the table and dark mode<br \/>\n$customCSS = @&quot;<br \/>\n&lt;img src=&quot;data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7&quot; data-wp-preserve=&quot;%3Cstyle%3E%0A%20%20%20%20body%20%7B%0A%20%20%20%20%20%20%20%20background-color%3A%20%23333%3B%0A%20%20%20%20%20%20%20%20color%3A%20%23fff%3B%0A%20%20%20%20%7D%0A%20%20%20%20table%20%7B%0A%20%20%20%20%20%20%20%20width%3A%20100%25%3B%0A%20%20%20%20%20%20%20%20border-collapse%3A%20collapse%3B%0A%20%20%20%20%20%20%20%20font-size%3A%208pt%3B%20%2F*%20Set%20font%20size%20to%208%20points%20*%2F%0A%20%20%20%20%20%20%20%20background-color%3A%20%23444%3B%0A%20%20%20%20%7D%0A%20%20%20%20th%2C%20td%20%7B%0A%20%20%20%20%20%20%20%20border%3A%201px%20solid%20%23ddd%3B%0A%20%20%20%20%20%20%20%20padding%3A%208px%3B%0A%20%20%20%20%20%20%20%20text-align%3A%20left%3B%0A%20%20%20%20%7D%0A%20%20%20%20th%20%7B%0A%20%20%20%20%20%20%20%20background-color%3A%20%23666%3B%0A%20%20%20%20%7D%0A%20%20%20%20p%20%7B%0A%20%20%20%20%20%20%20%20font-size%3A%208pt%3B%20%2F*%20Set%20font%20size%20to%208%20points%20*%2F%0A%20%20%20%20%7D%0A%3C%2Fstyle%3E&quot; data-mce-resize=&quot;false&quot; data-mce-placeholder=&quot;1&quot; class=&quot;mce-object&quot; width=&quot;20&quot; height=&quot;20&quot; alt=&quot;&amp;lt;style&amp;gt;&quot; title=&quot;&amp;lt;style&amp;gt;&quot; \/&gt;<br \/>\n&quot;@<\/p>\n<p># Create an HTML report with custom CSS<br \/>\n$logoURL = &quot;https:\/\/www.logostock.com\/logo.png&quot;<br \/>\n$htmlReport = @&quot;<br \/>\n&lt;!DOCTYPE html&gt;<br \/>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n    &lt;link href=&quot;https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.5.2\/css\/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt;<br \/>\n    $customCSS<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n    &lt;div class=&quot;container&quot;&gt;<br \/>\n        &lt;img src=&quot;$logoURL&quot; alt=&quot;Logo&quot; style=&quot;max-width: 200px;&quot;&gt;<br \/>\n        &lt;h1&gt;Snapshot Report&lt;\/h1&gt;<br \/>\n        $($vmData | ConvertTo-HTML -Fragment)<br \/>\n        &lt;br&gt; &lt;!&#8211; Add a line break &#8211;&gt;<br \/>\n        &lt;p&gt;Report ran on $((Get-Date).ToString(&#8216;dd MMMM yyyy&#8217;)) by $($env:USERNAME) using $($vcsausername) to authenticate to VCSA. &lt;br&gt;<br \/>\n        Report run time was $((Get-Date) &#8211; $startTime).&lt;\/p&gt;<br \/>\n    &lt;\/div&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n&quot;@<\/p>\n<p># Output the HTML report to a file<br \/>\n$htmlReport | Out-File -FilePath &quot;c:\\temp\\SnapshotReport.html&quot;<\/p>\n<p># Display the HTML report<br \/>\nInvoke-Item &quot;c:\\temp\\SnapshotReport.html&quot;<\/p>\n<p>[\/powershell]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A quick powershell script using HTML and CSS to run against your VMware Environment utilising&hellip;<\/p>\n","protected":false},"author":1,"featured_media":988,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[19,11,14],"tags":[63,70,76,80],"class_list":["post-1266","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","category-scripts","category-vmware","tag-powercli","tag-report","tag-snapshots","tag-vmware"],"jetpack_featured_media_url":"https:\/\/ihni.uk\/wp-content\/uploads\/2020\/02\/Powershell.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/posts\/1266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1266"}],"version-history":[{"count":0,"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/posts\/1266\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/media\/988"}],"wp:attachment":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}