Hello Everyone,
Hope all are doing good!!!
I am looking for the script which generates and email the snapshot report with below categories
VM | Snapshot name | Created | Description | Created by
In our environment, we have two Vcenter servers as Prod & DR, so the snapshots should be gathered from both of the Vcenter servers (shouldn't be duplicated).
So far i am using the attached script which gives me the output as enclosed under "script_output"
Please help!!!
Thank you
Regards,
Arvin Kumar
Hello arvin1201,
I'm not quite sure what you're asking about, as the script you've attached does everything you would like it to. It gathers the snapshot information, stores it all in $filename, then uses that information to create an email and send it.
Are you looking for the script to have more functionality, or is it not working properly for you?
Edit: I noticed that the script isn't adding the 'Created By' information in the details. To do this, simply add 'Username' to this part Select-Object vm, name, Created, description
Hi Gidrakos,
I have checked with the property "username", but it has given blank in the output
There is no issue with the existing script, but it just gives the categories of "VM,Snapshot, Created, Description" but i am also looking for the category " Owner" who has created the snapshot.
No offense, but the username property (what you refer to as "Owner") is not returned by the get-snapshot commandlet. You need to iterate through the event logs for that information.
I wrote a function that does just that, so in your case, you can easily adapt it to your needs. I did modify the very end so it returns what your $ss variable was returning.
Also it's bad practice to hard code username and passwords but feel free to modify this if you need to hardcode.
#########################
#Script by Jose Rodriguez
#
#This was tailored for this post so if you need
if($cred -eq $null){
$cred = get-credential
}
$servers = ('NAME_OF_SERVER1','NAME_OF_SERVER2')
function getSnapshots($servers,$cred){
$results = @()
foreach($server in $servers){
$connect = connect-viserver -Server $server -Credential $cred
$vmsnapshots = Get-VM | Get-Snapshot
$processed = 0
foreach ($snapshot in $vmsnapshots){
Write-Progress -Activity "Getting snapshot CreatedBy info" -PercentComplete (($processed/$vmsnapshots.Length)*100)
$processed = $processed + 1
$snapevent = Get-VIEvent -Entity $snapshot.VM -Types Info -Finish $snapshot.Created -MaxSamples 1 | Where-Object {$_.FullFormattedMessage -imatch 'Task: Create virtual machine snapshot'}
if($snapevent -ne $null){
$user = $snapevent.UserName
}
else{
$user = '--Unknown--'
}
$properties = @{
VM = $snapshot.vm.name
Name = $snapshot.name
Created = ($snapshot.created | Get-Date -UFormat "%D")
CreatedBy = $user
Server = $server
}
$results += new-object psobject -Property $properties
}
Disconnect-VIServer $server -Confirm:$false
}
#$results = $results | Sort-Object -Property Created
#$return = $results | Format-Table -Property VM,Name,created,CreatedBy,Server
return $results
}
$finalresults = getSnapshots -servers $servers -cred $cred
#OBVIOSULY THIS SECTION IS SPECIFICALLY TAILORED TO MATCH THIS POSTS ORIGINAL SCRIPT. IN CASE ANYONE IS WONDERING WHERE THAT $a CAME FROM
$finalresults | Sort-Object -Property Created | select VM,Name,created,CreatedBy,Server | ConvertTo-HTML -head $a -body "<H2>VM Snapshot Report</H2>"| Out-File $filename
Thanks alot for the help!!! i will try this script and get back with the status. Just wanted to know, instead of Server can we get the Description or along with the description details.
Please suggest
For description this would do it. Replace this section with this:
$properties = @{
VM = $snapshot.vm.name
Name = $snapshot.name
Description = $snapshot.Description
Created = ($snapshot.created | Get-Date -UFormat "%D")
CreatedBy = $user
Server = $server
}
$results += new-object psobject -Property $properties
}
And this section with this:
$finalresults | Sort-Object -Property Created | select VM,Name,Description,created,CreatedBy,Server | ConvertTo-HTML -head $a -body "<H2>VM Snapshot Report</H2>"| Out-File $filename
If you found my input helpful please mark my response as Correct.
Thanks for the script again!!!. While executing the previous script getting the errors which iam enclosing
Please suggest.
At first I thought the name of your snapshots were $https://....... but I tested that and the script works if that's that's the name of your snapshots.
I can replicate the error message if I assign a variable like this: VM = "$https://google.com"
Can you message me privately so we can go into more detail? Thanks!