VMware Cloud Community
cjfink
Contributor
Contributor
Jump to solution

Add user to Snapshot report

I have a weekly snapshot report run that uses the following script:

$snaps = @()

$currsnaps = get-vm | Get-Snapshot | Select VM, Name, Created, SizeGB

$css = "<style>BODY{font-family: Arial; font-size: 10pt;}

TABLE{border: 1px solid black; border-collapse: collapse;}

TH{border: 1px solid black; background: #dddddd; padding: 5px;}

TD{border: 1px solid black; padding: 5px;}</style>

"

foreach ($snap in $currsnaps){

  $user = ($snap.VM | Get-ViEvent | ?{$_.CreatedTime -match $snap.Created} | ?{$_.FullFormattedMessage -match "snapshot"}).Username

  If ($snap.Name -ne "__GX_BACKUP__"){

  $builder = New-Object System.Object

  $builder | Add-Member -Type NoteProperty -Name VM -Value $snap.VM

  $builder | Add-Member -Type NoteProperty -Name SnapName -Value $snap.Name

  $builder | Add-Member -Type NoteProperty -Name TimeStamp -Value $snap.Created

  $builder | Add-Member -Type NoteProperty -Name SizeGB -Value $([math]::floor($snap.SizeGB))

  $snaps += $builder

  }

}

I've tried adding the following code to include the user that created the snapshot.

$builder | Add-Member -Type NoteProperty -Name CreatedBy -Value $user.UserName

$builder | Add-Member -Type NoteProperty -Name CreatedBy -Value $user

I've also added .AddSeconds(-5) to the $snap.Created, along with .AddMinutes(5).  When I add -MaxSamples 5 the script errors stating a parameter cannot be found that matches MaxSamples.

When I try to add the above code the user name does not show up in the final report.

However, when I run this script, the output displays the domain\username every time. 

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){Write-Host ( "VM: "+ $snap.VM + ". Snapshot '" + $snap + "' created on " + $snap.Created.DateTime + " by " + $snapevent.UserName +".")}

else {Write-Host ("VM: "+ $snap.VM + ". Snapshot '" + $snap + "' created on " + $snap.Created.DateTime + ". This event is not in vCenter events database")}}

How can I integrate the code that works from the 2nd script into the 1st script?

0 Kudos
1 Solution

Accepted Solutions
cjfink
Contributor
Contributor
Jump to solution

Finally got this working with the following script.  I added the highlighted code from the other script that was displaying the user name and then added a column to the report using the $snapevent.UserName.

Add-PSsnapin VMware.VimAutomation.Core

#set-PowerCLIConfiguration -InvalidCertificationAction Ignore

Connect-ViServer

$email_to =

$email_from = ""

$email_server = ""

$email_subject = "VMWare Snapshot Report"

$snaps = @()

$currsnaps = get-vm | Get-Snapshot | Select VM, Name, Created, SizeGB, UserName

$css = "<style>BODY{font-family: Arial; font-size: 10pt;}

TABLE{border: 1px solid black; border-collapse: collapse;}

TH{border: 1px solid black; background: #dddddd; padding: 5px;}

TD{border: 1px solid black; padding: 5px;}</style>

"

foreach ($snap in $currsnaps){

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

# $user = ($snap.VM | Get-ViEvent | ?{$_.CreatedTime -match $snap.Created} | ?{$_.FullFormattedMessage -match "snapshot"}).Username

  If ($snap.Name -ne "__GX_BACKUP__"){

  $builder = New-Object System.Object

  $builder | Add-Member -Type NoteProperty -Name VM -Value $snap.VM

  $builder | Add-Member -Type NoteProperty -Name SnapName -Value $snap.Name

$builder | Add-Member -Type NoteProperty -Name User -Value $snapevent.UserName

  $builder | Add-Member -Type NoteProperty -Name TimeStamp -Value $snap.Created

  $builder | Add-Member -Type NoteProperty -Name SizeGB -Value $([math]::floor($snap.SizeGB))

  $snaps += $builder

  }

}

if($snaps){

  Send-MailMessage -To $email_to -From $email_from -SmtpServer $email_server -Subject $email_subject -BodyAsHtml ($snaps | ConvertTo-Html -Head $css | Out-String)

}

View solution in original post

0 Kudos
6 Replies
Sudarshan_Bhart
Enthusiast
Enthusiast
Jump to solution

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$snaps = @()

$currsnaps = get-vm | Get-Snapshot | Select VM, Name, Created, SizeGB

$css = "<style>BODY{font-family: Arial; font-size: 10pt;}

TABLE{border: 1px solid black; border-collapse: collapse;}

TH{border: 1px solid black; background: #dddddd; padding: 5px;}

TD{border: 1px solid black; padding: 5px;}</style>

"

foreach ($snap in $currsnaps){

  $user = ($snap.VM | Get-ViEvent | ?{$_.CreatedTime -match $snap.Created} | ?{$_.FullFormattedMessage -match "snapshot"}).Username

  If ($snap.Name -ne "__GX_BACKUP__"){

      $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){

              $snapUser = $snapevent.UserName

          }

          else {

              $snapUser = 'na'

          }

         

      $builder = New-Object System.Object

      $builder | Add-Member -Type NoteProperty -Name VM -Value $snap.VM

      $builder | Add-Member -Type NoteProperty -Name SnapName -Value $snap.Name

      $builder | Add-Member -Type NoteProperty -Name TimeStamp -Value $snap.Created

      $builder | Add-Member -Type NoteProperty -Name SizeGB -Value $([math]::floor($snap.SizeGB))

      $builder | Add-Member -Type NoteProperty -Name User -Value $snapUser

      $snaps += $builder

  }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

cjfink
Contributor
Contributor
Jump to solution

Luc,

Thank you for your response!  The initial run of your code doesn't return anything.  So I checked to make sure there were still snapshots in the environment and there is.  I'll see if I can tweek the code any more to get it to work.

Thank you again,

Chris

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The lack of user info is probably due to the Get-VIEvent not finding any related events.

You might try moving the Start value further back in time.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

cjfink
Contributor
Contributor
Jump to solution

Good idea.  I haven't been able to circle back around to this yet, but hope to over the next couple of days.  Will let you know how it goes.

0 Kudos
cjfink
Contributor
Contributor
Jump to solution

Finally got this working with the following script.  I added the highlighted code from the other script that was displaying the user name and then added a column to the report using the $snapevent.UserName.

Add-PSsnapin VMware.VimAutomation.Core

#set-PowerCLIConfiguration -InvalidCertificationAction Ignore

Connect-ViServer

$email_to =

$email_from = ""

$email_server = ""

$email_subject = "VMWare Snapshot Report"

$snaps = @()

$currsnaps = get-vm | Get-Snapshot | Select VM, Name, Created, SizeGB, UserName

$css = "<style>BODY{font-family: Arial; font-size: 10pt;}

TABLE{border: 1px solid black; border-collapse: collapse;}

TH{border: 1px solid black; background: #dddddd; padding: 5px;}

TD{border: 1px solid black; padding: 5px;}</style>

"

foreach ($snap in $currsnaps){

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

# $user = ($snap.VM | Get-ViEvent | ?{$_.CreatedTime -match $snap.Created} | ?{$_.FullFormattedMessage -match "snapshot"}).Username

  If ($snap.Name -ne "__GX_BACKUP__"){

  $builder = New-Object System.Object

  $builder | Add-Member -Type NoteProperty -Name VM -Value $snap.VM

  $builder | Add-Member -Type NoteProperty -Name SnapName -Value $snap.Name

$builder | Add-Member -Type NoteProperty -Name User -Value $snapevent.UserName

  $builder | Add-Member -Type NoteProperty -Name TimeStamp -Value $snap.Created

  $builder | Add-Member -Type NoteProperty -Name SizeGB -Value $([math]::floor($snap.SizeGB))

  $snaps += $builder

  }

}

if($snaps){

  Send-MailMessage -To $email_to -From $email_from -SmtpServer $email_server -Subject $email_subject -BodyAsHtml ($snaps | ConvertTo-Html -Head $css | Out-String)

}

0 Kudos