I've created a script to help deploy a large amount of VMs using a CSV file.
It's setup to basically look for an existing OSCustomizationSpec (listed in the csv) and then clone it and use that for deploying the new VMs with specific IP, DNS, etc. After each deployment of a VM, the temp cloned OSCustomization is deleted and then subsequently recreated for each deployed VM.
The issue is, if the script fails for whatever reason or is interrupted, the temp OSCustomization is there and causes an issue resulting in the script failure the next time it's ran.
So I'm trying to do a search for the Temp one, and if exists, delete it, and then proceed with the script.
$OSCusSpec = Get-OSCustomizationSpec -Name 'TempCust'
If ($OSCusSpec -eq $True)
{
Remove-OSCustomizationSpec "TempCust" -Confirm:$false | Out-Null
Write-Host " Cleanup of old OSCustomizationSpec Complete. " -ForegroundColor Green
}
Else
{
Write-Host " No existing Temp OSCustomizationSpec Cleanup Necessary. " -ForegroundColor Yellow
}
I've tried all different ways of trying to accomplish this, but I can't seem to get anything to work. the -eq $True doesn't seem to return any value, because even if that TempCust is there, the script still returns the Else statement. I've tried using different variations of that and $null, and just can't figure out how the best way to get this working.
Thanks for any assistance or guidance. I really appreciate it because I've been banging my head
Otherwise, a try-catch construct could help.
Something like this
Get-OSCustomizationSpec -Name 'TempCust' -ErrorAction Stop |
Remove-OSCustomizationSpec -Confirm:$false
Write-Host " Cleanup of old OSCustomizationSpec Complete. " -ForegroundColor Green
}
catch{
Write-Host " No existing Temp OSCustomizationSpec Cleanup Necessary. " -ForegroundColor Yellow
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
As an alternative solution, why don't you clone the OSCustomizationSpec to a NonPersistent one.
These are not stored on the server, but are kept in memory of your current PowerCLI session.
Something like this
New-OSCustomizationSpec -Name 'NewCust' -Type NonPersistent
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Otherwise, a try-catch construct could help.
Something like this
Get-OSCustomizationSpec -Name 'TempCust' -ErrorAction Stop |
Remove-OSCustomizationSpec -Confirm:$false
Write-Host " Cleanup of old OSCustomizationSpec Complete. " -ForegroundColor Green
}
catch{
Write-Host " No existing Temp OSCustomizationSpec Cleanup Necessary. " -ForegroundColor Yellow
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you for the reply. I do create a non-persistent one with the following code:
$OSCust = Get-OSCustomizationSpec -Name $vm.OSCustomizationSpec (this is found in the csv)
Get-OSCustomizationSpec -Name $OSCust | New-OSCustomizationSpec -Name "TempCust" -Type NonPersistent
Then the try-catch construct should work
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This actually worked perfect. I have a Try/Catch setup for the main contents of the script, and I was trying to avoid doing a nested Try/Catch (because I'm new to this level of scripting and read how that can cause headaches if you don't know what you're doing or trying to troubleshoot issues). But, it's easy enough to just add this above the bulk of the script and it did work well.
Thanks, LucD.