A script to put multiple vms on snapshot vmware Print

  • 84

Two things to do before you run this script.
1- Change the vcenter server name
2- Create a file "C:\temp\serversfile.txt" and put your vms in it.

#Get PowerCLI stuffs
Add-PSSnapin VMware.VimAutomation.Core
#Ignore that we don't have a proper certificate for vCenter
Set-PowerCLIConfiguration -Scope Session -InvalidCertificateAction Ignore -Confirm:$false

Connect-VIServer #### vcenter server name #####

$MinSpace = 100

Add-Type -AssemblyName System.Windows.Forms

$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Snaperisor"
$Form.TopMost = $false
$Form.Width = 800
$Form.Height = 443

$label = New-Object system.windows.Forms.Label
$label.Text = "Path of VM list:"
$label.AutoSize = $true
$label.Width = 25
$label.Height = 10
$label.location = new-object system.drawing.point(17,24)
$label.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($label)

$textBox = New-Object system.windows.Forms.TextBox
$textBox.Width = 432
$textBox.text = "C:\temp\serversfile.txt"
$textBox.Height = 20
$textBox.location = new-object system.drawing.point(192,25)
$textBox.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($textBox)

$textBox7 = New-Object system.windows.Forms.TextBox
$textBox7.Multiline = $true
$textBox7.Width = 222
$textBox7.Height = 292
$textBox7.location = new-object system.drawing.point(146,53)
$textBox7.Font = "Microsoft Sans Serif,8"
$Form.controls.Add($textBox7)

$button4 = New-Object system.windows.Forms.Button
$button4.Text = "Check Datastores"
$button4.Width = 103
$button4.Height = 58
$button4.Add_Click({
$ServerList = Get-Content $textBox.text
$textBox7.Text = $ServerList
$textBox7.Update()
$textBox10.text = ""
$textBox10.Update()
$textBox8.Text = "Checking... (this may take some time)"
$textBox8.Update()
ForEach ($VM in $ServerList)
{
$VMDatastore = Get-VM $VM | Get-Datastore | Select-Object -ExpandProperty name -First 1
$DatastoreFreespace = Get-Datastore $VMDatastore | Select FreeSpaceGB
$StrDatastoreFreeSpace = $DatastoreFreespace.FreeSpaceGB | Out-String
$StrDatastoreFreeSpace = $StrDatastoreFreeSpace.Split(".")[0]
$TextboxUpdate = "$VM datastore: $VMDatastore space = $StrDatastoreFreeSpace`GB `r`n"
$textBox10.text += $TextboxUpdate
$textBox10.Update()
If ($DatastoreFreespace.FreeSpaceGB -lt $MinSpace)
{
$textBox10.text += "$VM has less than $MinSpace`GB left on its datastore. Fix this and recheck"
$CheckResult = "Check complete with error"
Break
}
Else
{$CheckResult = "Check complete"}
}
$textBox8.Text = $CheckResult
$textBox10.Update()
$textBox8.Update()
If ($textBox8.Text -like '*error*')
{Write-Output "Datastores with less than $MinSpace`GB"}
Else {$button9.visible = $true}
$button9.Update()
})

$button4.location = new-object system.drawing.point(18,54)
$button4.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($button4)

$textBox8 = New-Object system.windows.Forms.TextBox
$textBox8.Width = 321
$textBox8.Height = 20
$textBox8.location = new-object system.drawing.point(380,55)
$textBox8.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($textBox8)

$button9 = New-Object system.windows.Forms.Button
$button9.Text = "Snap!"
$button9.Width = 103
$button9.Height = 58
$button9.visible = $false
$button9.location = new-object system.drawing.point(18,122)
$button9.Font = "Microsoft Sans Serif,10"
$button9.Add_Click({
$textBox8.Text = "Snapping... (this may take some time)"
$ServerList = Get-Content $textBox.text
$textBox10.text = ""
ForEach ($VM in $ServerList)
{
$Date = Get-Date
New-Snapshot -Name DanSnaperisor -Description "Snapshot taken by Seimaxim | $Date" -Memory:$false -Quiesce -VM $VM -Confirm:$false
$textBox10.text += "$VM snapshot taken `r`n"
$textBox10.update()
}
$textBox8.Text = "Snapshots complete"
$textBox8.update()
})
$Form.controls.Add($button9)

$textBox10 = New-Object system.windows.Forms.TextBox
$textBox10.Multiline = $true
$textBox10.Width = 360
$textBox10.Height = 257
$textBox10.location = new-object system.drawing.point(381,87)
$textBox10.Font = "Microsoft Sans Serif,6"
$Form.controls.Add($textBox10)

[void]$Form.ShowDialog()
$Form.Dispose()


Was this answer helpful?

« Back