VMware Cloud Community
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Who created the Snapshot

Any idea how I can relatemy Get-Snapshot information to the Get-VIEvent information to work out who created a snapshot ?

Thanks

Alan

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
Tags (1)
0 Kudos
76 Replies
bseymour
Contributor
Contributor
Jump to solution

In fact what I was hoping to do with this was to run it as a scheduled task that would email users of their snapshots daily.

0 Kudos
bseymour
Contributor
Contributor
Jump to solution

I downloaded the script again, modified the script to 1 day:

104 foreach ($snap in (Get-VM | Get-Snapshot | Where {$_.Created -lt ((Get-Date).AddDays(-1))})){

It seems so have found things now. The only error I get now has to do with the emailing.

Exception calling "Add" with "1" argument(s): "Value cannot be null.
Parameter name: item"
At C:\scripts\SnapshotEmail.ps1:86 char:13
+     $msg.To.Add <<<< ($Mailto)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
Exception calling "Send" with "1" argument(s): "A recipient must be specified."
At C:\scripts\SnapshotEmail.ps1:99 char:12
+     $smtp.Send <<<< ($msg)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks as if the $MailTo variable is empty when the script tries to add it to the recipients of the email.

That could be because the script couldn't find the owner of the snapshot or the Find-User function is not returning the email address of the snapshot creator.


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

0 Kudos
Trondesson
Contributor
Contributor
Jump to solution

Hello folks.

I get the same exceptions mentioned in Post #41. But I couldn't figure out why the $MailTo variable is empty. I tried to locate the error an it seems to, that the function Get-SnapshotExtra doesn't return a prober value for $SnapshotInfo.

I'm very new to PowerCLI. Could somebody help me to figure out, wether the function Get-SnapshotExtra works or not? What do I have to do?

Best Regards.

Trondesson

0 Kudos
Trondesson
Contributor
Contributor
Jump to solution

Hi.

I tried to run the script step by step in a PowerCLI to see what's going wrong.

I got an exception in the Get-SnapshotExtra($snap)-function.

PowerCLI C:\Scripts> $key

UHRZ-Ubuntu-E01&22.04.2015 15:51:54

PowerCLI C:\Scripts> $_ | Add-Member -MemberType NoteProperty -Name Creator -Value $report[$key].User

Add-Member : Das Argument kann nicht an den Parameter "InputObject" gebunden werden, da es NULL ist.

In Zeile:1 Zeichen:6

+ $_ | Add-Member -MemberType NoteProperty -Name Creator -Value $report[$key].User

+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Add-Member], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand

PowerCLI C:\Scripts> $report[$key].User

PowerCLI C:\Scripts>

I guess there is somethin wrong witth $report[$key].User which has no value.

Can someone help me with that?

0 Kudos
chaston
Contributor
Contributor
Jump to solution

LucD,


When I run this script in my vCenter 5.5 environment on a VM that had a snapshot created today, I don't get the 'created' by value returned. I see the username in the VI event within the VM's event tab but the field is not being output into the spreadsheet. I have even ran it in sections to make sure things are getting populated and it seems like everything is there but the user entry.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suggest you try the final script Alan posted on the subject, see his PowerCLI: SnapReminder post.

Even if you don't need the email, the function to find the snapshot information in the events is better


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

0 Kudos
afhamsani
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

Is the script you shared is able to retrieve the creator of snapshot even when this doesnt even listed in task\event in particular vm?

Sometimes i observed in the event\task it cant capture who was the creator of the snapshot.

Any reason of this? is it because the snapshot was created more than time it permissable to list in task\event?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The UserName is sometimes present in the event.
Afaik it depends on who or what created the snapshot (Web Client, vSphere Client, CLI, PowerCLI...) if the UserName is there or not.
But to my knowledge there is no clear statement/document that explains what are the exact conditions to have the UserName in the Event or not.


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

0 Kudos
dlgaines
Contributor
Contributor
Jump to solution

I can run the script without getting any errors but i do not get an email with the reminder to delete. I've confirmed the vcenter server and the SMTP server are correct. I have a snapshot that was created a few days ago and it's checking for snapshots created in the last 14 days so it should see it.The script runs and completes but I see no results. Any suggestions?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can the script resolve your AD account from the account you use in vSphere to create the snapshot?


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

0 Kudos
dlgaines
Contributor
Contributor
Jump to solution

I don't know how to check that but I know that the AD account I use to login to vcenter has my email address associated with it.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you run the script in the ISE or in VSC, you could check the content of the variable $mailto.

That should hold your email address.

Another possible issue might be that your SMTP server only accepts connections from specific servers.

Can you check with a simple Send-MailMessage if the mail arrives in your mailbox?


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

0 Kudos
dlgaines
Contributor
Contributor
Jump to solution

I ran "get-variable -name $mailto" and it returned "Cannot validate argument on parameter 'Name'. The argument is null." I'm assuming there is a way I can view the content from within the running script to get the actual email of the snapshot creator but I don't know how.

I was able to do a simple send-mailmessage using the same smtp server and I get the email.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The name should mailto, not $mailto


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

0 Kudos
dlgaines
Contributor
Contributor
Jump to solution

Here's what i get with that.

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

So there is no content after your ran the script?

That probably explains why the email doesn't arrive


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

0 Kudos
dlgaines
Contributor
Contributor
Jump to solution

Do you know how I can check why it would have no value?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

First I would need to see the exact script you are using (there are quite a bit of variations in this thread).

Can you attach the .ps1 file (take out or mask all the confidential info that might be in there).


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

0 Kudos
dlgaines
Contributor
Contributor
Jump to solution

It's an exact copy and paste from this site http://www.virtu-al.net/featured-scripts/snapreminder/

The only values that have changed are the SMTP, MailFrom, and VISrv on lines 5-7 to match what we have in our environment.

0 Kudos