VMware Cloud Community
sford19
Contributor
Contributor

Snapshot description changes based on user

 Below is my script. It is not working and need some help. It is going on every VM and placing the user who created the snapshot in the description but I can not figure out how to make it only do each VM once.  I want to run this weekly so I can find the people who did not follow our snapshot policy. some snapshots I keep for a bit and do not want the user to be put it on description.  It is putting domain/user and I am trying to look at the description and see if the domain/ is there.

 

Add-pssnapin VMware.VimAutomation.Core

$viserver = "viserver"

$secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force

$mycreds = New-Object System.Management.Automation.PSCredential ("user", $secpasswd)

Connect-VIServer $viserver -Credential $mycreds

foreach ($snap in Get-VM | Get-Snapshot)

{$snapevent = Get-VIEvent -Entity $snap.VM -Types Info -Finish $snap.Created -MaxSamples 1 | Where-Object {$_.FullFormattedMessage -imatch 'Task: Create virtual machine snapshot'}

if ($snapevent -ne $null){

    $description = ConvertTo-Csv  $snap.Description 

    if ($description -notcontains "domain"){

        $username = $snapevent.UserName 

        $description = $snap.Description

        Write-Host $description

        Set-Snapshot $snap -Description "$username  $description"

        Write-Host ( "VM: "+ $snap.VM + ". Snapshot '" + $snap + "' created on " + $snap.Created.DateTime + " by " + $snapevent.UserName +".")}

    else {

        Continue

}

}}

#

Reply
0 Kudos
1 Reply
Prakas
Enthusiast
Enthusiast

I guess you are getting multiple entries of 'domain\username' in your snapshot description and you want to fix this.

If so, update the line 10 in your script with below and see if it helps:-

    if ($description -notlike "*domain*"){

Reply
0 Kudos