Automation

 View Only
  • 1.  Add user to Snapshot report

    Posted May 31, 2017 04:36 PM

    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?



  • 2.  RE: Add user to Snapshot report

    Posted Jun 07, 2017 10:05 AM


  • 3.  RE: Add user to Snapshot report

    Posted Jun 07, 2017 12:28 PM

    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

      }

    }



  • 4.  RE: Add user to Snapshot report

    Posted Jun 09, 2017 01:38 PM

    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



  • 5.  RE: Add user to Snapshot report

    Posted Jun 09, 2017 01:44 PM

    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.



  • 6.  RE: Add user to Snapshot report

    Posted Jun 14, 2017 06:48 PM

    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.



  • 7.  RE: Add user to Snapshot report
    Best Answer

    Posted Jun 28, 2017 03:03 PM

    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)

    }