Skip to content

Instantly share code, notes, and snippets.

@joeypiccola
Last active May 3, 2017 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeypiccola/50a65c32937d654c0e12e73c910bdd4e to your computer and use it in GitHub Desktop.
Save joeypiccola/50a65c32937d654c0e12e73c910bdd4e to your computer and use it in GitHub Desktop.
code snippit for ps org
foreach ($vCenter in $vCenters) {
foreach ($template in $templates) {
# try to convert the current template to a VM
try {
Write-Verbose "Trying to convert template $($template.name) to a VM."
Set-Template -Template $template -ToVM -ErrorAction Stop
Write-Verbose "Successfully converted template $($template.name) to a VM."
}
catch {
Write-Verbose "Failed to convert template $($template.name) to a VM with the following exception."
Write-Warning $_.Exception.Message
Write-Verbose "Continuing on with next available $myOrgViServer template, if any."
continue
}
# try and snapshot the current VM
try {
Write-Verbose "Trying to snapshot VM ($template.name)."
New-Snapshot -VM $template.name -Name $snapName -ErrorAction Stop
Write-Verbose "Successfully snapshot VM ($template.name)."
}
catch {
Write-Verbose "Failed to snapshot VM $($template.name) with the following exception."
Write-Warning $_.Exception.Message
Write-Verbose "Continuing on with next available $myOrgViServer template, if any."
}
# try to start the current VM
try {
Write-Verbose "Trying to power-on VM $($template.name)"
Get-VM -Name $template.name -ErrorAction Stop | Start-VM -ErrorAction Stop
Write-Verbose "Successfully powered-on VM ($($template.name)"
}
catch {
Write-Verbose "Failed to power-on VM $($template.name) with the following exception."
Write-Warning $_.Exception.Message
Write-Verbose "Continuing on with next available $myOrgViServer template, if any."
continue
}
# wait until the VM is up and tools are good
Wait-SystemToolStatusOK -SystemName $template.name -LoopSleepTime 10
# try to invoke the WURegScript
try {
Write-Verbose "Trying to invoke WURegScript on VM $($template.name)"
Invoke-VMScript -GuestCredential -ScriptText $WURegScript -ScriptType Powershell -VM $template.name -ErrorAction Stop
Write-Verbose "Successfully invoked WURegScript on VM $($template.name)"
}
catch {
Write-Verbose "Failed to invoke WURegScript on $($template.name) with the following exception."
Write-Warning $_.Exception.Message
Write-Verbose "Continuing on with next available $myOrgViServer template, if any."
continue
}
# wait until the VM is up and tools are good
Wait-SystemToolStatusOK -SystemName $template.name -LoopSleepTime 10
# try to invoke the WUPatchScript
try {
Write-Verbose "Trying to invoke WUPatchScript on VM $($template.name)"
Invoke-VMScript -GuestCredential -ScriptText $WUPatchScript -ScriptType Powershell -VM $template.name -ErrorAction Stop
Write-Verbose "Successfully invoked WUPatchScript on VM $($template.name)"
}
catch {
Write-Verbose "Failed to invoke WUPatchScript on $($template.name) with the following exception."
Write-Warning $_.Exception.Message
Write-Verbose "Continuing on with next available $myOrgViServer template, if any."
continue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment