VMware Cloud Community
MGandhi
Contributor
Contributor
Jump to solution

Snapshot PS script ..Need help with report

I am trying to achieve that when products are deployed on VM's, Powershell Snapshot script will execute and take snapshots, provide report in html (output) and then execute another Script which will be execute against VI3 to commit all snapshots.

I need help with reporting, I want report to show VM name + Snapshot summary Details, Below script only shows Snapshot summary. Also if there is a script to commit all snapshots on defined VM.

**********************************************

  1. Variables

$VCServerName = "VIServer"

$CustomFieldName = "Snapshots"

$ManagedObjectType = "VMName"

$today=(Get-Date -Format 'yyyy-MM-dd_HH.mm.ss-tt')

$nsn="PowerShell-$today"

$a = ""

  1. Script

Connect-VIServer $VCServerName -Port 443 -Protocol HTTPS

Get-VM $ManagedObjectType | New-Snapshot -Name $nsn -Description "Product Update Snapshot"

Get-Snapshot $ManagedObjectType | SELECT name, ID, Description, VM, Created | ConvertTo-HTML -head $a | Out-File C:\Report.htm

Invoke-Expression C:\Report.htm

Disconnect-VIServer $VCServerName -Confirm:$False

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You have to select the last one.

Something like this for example

Get-Snapshot -VM $vm | Sort Created | Select -Last 1 | Remove-Snapshot -Confirm:$false

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

Try a foreach loop like this

$VCServerName = "VIServer"
$CustomFieldName = "Snapshots"
$ManagedObjectType = "VMName"
$today=(Get-Date -Format 'yyyy-MM-dd_HH.mm.ss-tt')
$nsn="PowerShell-$today"

$a = "<style>"
$a = $a + "BODY{background-color:peachpuff;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "</style>"

Connect-VIServer $VCServerName -Port 443 -Protocol HTTPS
$report = @()
foreach($vm in Get-VM){
 	New-Snapshot -VM $vm -Name $nsn -Description "Product Update Snapshot"
	$report += Get-Snapshot -VM $vm | SELECT VM, name, ID, Description, Created
} 
$report | ConvertTo-HTML -head $a | Out-File C:\Report.htm
Invoke-Expression C:\Report.htm

Disconnect-VIServer $VCServerName -Confirm:$False

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
MGandhi
Contributor
Contributor
Jump to solution

LucD,

I am begginer in Powershell. The modified script by you creates a snapshot for all the vm's in vi3. I need help so if "$ManagedObjectType = "VMName"" VMName is defined then it only executes against that VM not all the Vm's.

Thanks for you help.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sorry, but I don't really understand what you want to do.

Do you want to have the snapshot information only for the VM whose name you defined in $ManagedObjectType ?

Or do you only want to create a snapshot for the VM that is defined in $ManagedObjectType ?

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
MGandhi
Contributor
Contributor
Jump to solution

LucD,

I want to create a snapshot for the VM that is defined in the $ManagedObjectType, Then get the Snapshot information for the VM defined in $ManagedObjectType and output it to report.htm (I want to see some information about $ManagedObjectType + Snapshot information in the report for audit purpose)

Basic script steps:

1) Connect to VC

2) Execute Snapshot against $ManagedObjectType

3) Dump Report.htm file in which $ManagedObjectType information (name, ID, Description) and Snapshot informaiton is (name, ID, Description, VM, Created ) are listed for review/audit purpose.

4) Invoke-Expression C:\Report.htm

Hoping this clarifies what i am trying to do..Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, I think I got it.

$VCServerName = "VIServer"
$CustomFieldName = "Snapshots"
$ManagedObjectType = "VMName"
$today=(Get-Date -Format 'yyyy-MM-dd_HH.mm.ss-tt')
$nsn="PowerShell-$today"

$a = "<style>"
$a = $a + "BODY{background-color:peachpuff;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "</style>"

Connect-VIServer $VCServerName -Port 443 -Protocol HTTPS

$vm = Get-VM -Name $ManagedObjectType
New-Snapshot -VM $vm -Name $nsn -Description "Product Update Snapshot"
Get-Snapshot -VM $vm | SELECT VM, @{N="Description";E={$vm.Description}}, @{N="ID";E={$vm.ID}},
   name, ID, Description, Created | ConvertTo-HTML -head $a | Out-File C:\Report.htm
Invoke-Expression C:\Report.htm

Disconnect-VIServer $VCServerName -Confirm:$False

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
MGandhi
Contributor
Contributor
Jump to solution

Thanks LucD for quick help.

I was wondering if you know of a way to query last snapshot on particular VM and commit it or delete it via 2 seperate script, Any suggestions would be great help. I need to create a 2 seperate scripts that can be run against particular VM, first one that can "Commit last snapshot" (Commit Snapshot) and second one to "go back to last snapshot" (Revert snapshot).

I looked at below which deletes all snapshots....

Get-Snapshot -VM $vm | Remove-Snapshot -Confirm:$false

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to select the last one.

Something like this for example

Get-Snapshot -VM $vm | Sort Created | Select -Last 1 | Remove-Snapshot -Confirm:$false

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos