I am writing a PowerCLI script to automate the creation of VMs based on a CSV file and I am wondering how I could go about increasing the Hard Disk size based on the CSV file compared to the Template. I cannot remove the Hard Disk 1 altogether to re-create the new Hard Disk at the appropriate size, as to maintain the content obtained from the Template itself.
I.E.
The Template comes with Hard Disk 1 (40GB) and the CSV file is saying that it should be 50GB total, how do I reference Hard Disk 1 in an IF statement to increase/change the Hard Disk 1 size:
IF Hard Disk 1 -ne or -lt $row.HardDisk 1
Set-HardDisk to $row.HardDisk
Would I need to setup the Hard Disk 1 as a variable of some sort like $HD1 = Get-HardDisk -VM $row.Name | -CapacityGB and then use that within the IF statement? Fairly new at this and not sure how I can go about this or would that would be written out
You could try to capture that situation with a Try-Catch construct.
Something like this.
Try{
$hd1 = Get-HardDisk -VM $row.Name -Name 'Hard Disk 1' -ErrorAction Stop
}
Catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.VimException]{
Write-Host "Hard Disk 1 " -ForegroundColor Yellow -NoNewline; Write-Host "does not exist - creating now" -ForegroundColor Green$hd1 = New-HardDisk -VM $row.Name -CapacityGB $row.C_System -Confirm:$false
}if($hd1.CapacityGB -lt $row.C_System){
Write-Host "Hard Disk 1 " -ForegroundColor Yellow -NoNewline; Write-Host "requires additional space - adjusting now" -ForegroundColor Green
Set-HardDisk -HardDisk $hd1 -CapacityGB $row.C_System -Confirm:$false
}
else{
Write-host "Hard Disk 1 " -foreground Yellow -NoNewline; write-host "is present with adequate space" -ForegroundColor Green
Write-Host ""
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Something like this should do the trick.
$vm = New-VM -Name $row.Name -Template $row.Templae # rest of the parameters
$hd = Get-HardDisk -VM $vm -Name 'Hard Disk 1'
if($hd.CapacityGB -lt $row.CapacityGB){
Set-HardDisk -HardDisk $hd -CapacityGB $row.CapacityGB
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you very much for the response - I am playing around with the logic of the script and trying to determine how to have the script also verify first that the drive doesn't exist and if it doesn't, proceed to creating it with the parameters set in the CSV file.
Am I close with this?:
$hd1 = Get-HardDisk -VM $row.Name -Name 'Hard Disk 1'
$hd2 = Get-HardDisk -VM $row.Name -Name 'Hard Disk 2'
$hd3 = Get-HardDisk -VM $row.Name -Name 'Hard Disk 3'
IF ($hd1 = "")
Write-Host "$hd1 does not exist - Creating $hd1"
New-HardDisk -VM $row.Name -CapacityGB $row.C_System -Confirm:$false
IF ($hd1.CapacityGB -lt $row.C_System)
Set-HardDisk -HardDisk $hd1 -CapacityGB $row.C_System
IF ($hd2 = "")
Write-Host "$hd2 does not exist - Creating $hd2"
New-HardDisk -VM $row.Name -CapacityGB $row.D_Homevg -Confirm:$false
IF ($hd2.CapacityGB -lt $row.D_Homevg)
Set-HardDisk -HardDisk $hd2 -CapacityGB $row.D_Homevg
IF ($hd3 = "")
Write-Host "$hd3 does not exist - Creating $hd3"
New-HardDisk -VM $row.Name -CapacityGB $row.App_eHealth -Confirm:$false
IF ($hd3.CapacityGB -lt $row.App_eHealth)
Set-HardDisk -HardDisk $hd3 -CapacityGB $row.App_eHealth
It returns these errors:
Get-HardDisk : 2016/12/22 2:11:39 PM Get-HardDisk HardDisk with name
'Hard Disk 2' was not found using the specified filter(s).
Set-HardDisk : Cannot bind parameter 'HardDisk'. Cannot convert the "" value
of type "System.String" to type
That test should be
IF ($hd1 -eq $null)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Okay thank you for the reply - I think I have it behaving mostly how I intended.
I am having an issue with 'cleaning up' the output though, because 'Hard Disk 2' and 'Hard Disk 3' don't necessarily exist until the script creates them, is there a way to have the line setting the $hd2 and $hd3 variables not result in an error and just quietly attribute the Get-HardDisk cmdlet to them? Would piping out to Out-Null work for this?
Currently this section is written as:
$hd1 = Get-HardDisk -VM $row.Name -Name 'Hard Disk 1'
$hd2 = Get-HardDisk -VM $row.Name -Name 'Hard Disk 2'
$hd3 = Get-HardDisk -VM $row.Name -Name 'Hard Disk 3'
IF ($hd1 -eq $null)
Write-Host "Hard Disk 1 " -ForegroundColor Yellow -NoNewline; Write-Host "does not exist - creating now" -ForegroundColor Green
New-HardDisk -VM $row.Name -CapacityGB $row.C_System -Confirm:$false
IF ($hd1.CapacityGB -lt $row.C_System)
Write-Host "Hard Disk 1 " -ForegroundColor Yellow -NoNewline; Write-Host "requires additional space - adjusting now" -ForegroundColor Green
Set-HardDisk -HardDisk $hd1 -CapacityGB $row.C_System -Confirm:$false
ELSE {
Write-host "Hard Disk 1 " -foreground Yellow -NoNewline; write-host "is present with adequate space" -ForegroundColor Green
Write-Host ""
IF ($hd2 -eq $null)
Write-Host "Hard Disk 2 " -ForegroundColor Yellow -NoNewline; Write-Host "does not exist - creating now" -ForegroundColor Green
New-HardDisk -VM $row.Name -CapacityGB $row.D_Homevg -Confirm:$false
IF ($hd2.CapacityGB -lt $row.D_Homevg)
Write-Host "Hard Disk 2 " -ForegroundColor Yellow -NoNewline; Write-Host "requires additional space - adjusting now" -ForegroundColor Green
Set-HardDisk -HardDisk $hd2 -CapacityGB $row.D_Homevg -Confirm:$false
ELSE
Write-host "Hard Disk 2 " -foreground yellow -NoNewline; write-host "is present with adequate space" -ForegroundColor Green
Write-Host ""
IF ($hd3 -eq $null)
Write-Host "Hard Disk 3 " -ForegroundColor Yellow -NoNewline; Write-Host "does not exist - creating now" -ForegroundColor Green
New-HardDisk -VM $row.Name -CapacityGB $row.App_eHealth -Confirm:$false
IF ($hd3.CapacityGB -lt $row.App_eHealth)
Write-Host "Hard Disk 3 " -ForegroundColor Yellow -NoNewline; Write-Host "requires additional space - adjusting now" -ForegroundColor Green
Set-HardDisk -HardDisk $hd3 -CapacityGB $row.App_eHealth -Confirm:$false
ELSE {
Write-host "Hard Disk 3 " -ForegroundColor Yellow -NoNewline; Write-host "is present with adequate space" -ForegroundColor Green
Write-Host""
When I execute the script, I see this error at the variable setting and before it successfully proceeds to creating the Hard Disk 2 and Hard Disk 3 and I am wondering if I have things in the incorrect order:
Get-HardDisk : 2016/12/29 8:06:42 AM Get-HardDisk HardDisk with name
'Hard Disk 2' was not found using the specified filter(s).
Get-HardDisk : 2016/12/29 8:06:42 AM Get-HardDisk HardDisk with name
'Hard Disk 3' was not found using the specified filter(s).
This errors occurs both when the script hits the variable assignments ($hd2 = Get-HardDisk -VM $row.Name -Name 'Hard Disk 2' and $hd3 = Get-HardDisk -VM $row.Name -Name 'Hard Disk 3') and the IF statement checking that $hd2 and $hd3 -eq null. Is there a way I can clean up that logic or have it quietly check for the Hard Disks?
Thank you for all of your help
You could try to capture that situation with a Try-Catch construct.
Something like this.
Try{
$hd1 = Get-HardDisk -VM $row.Name -Name 'Hard Disk 1' -ErrorAction Stop
}
Catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.VimException]{
Write-Host "Hard Disk 1 " -ForegroundColor Yellow -NoNewline; Write-Host "does not exist - creating now" -ForegroundColor Green$hd1 = New-HardDisk -VM $row.Name -CapacityGB $row.C_System -Confirm:$false
}if($hd1.CapacityGB -lt $row.C_System){
Write-Host "Hard Disk 1 " -ForegroundColor Yellow -NoNewline; Write-Host "requires additional space - adjusting now" -ForegroundColor Green
Set-HardDisk -HardDisk $hd1 -CapacityGB $row.C_System -Confirm:$false
}
else{
Write-host "Hard Disk 1 " -foreground Yellow -NoNewline; write-host "is present with adequate space" -ForegroundColor Green
Write-Host ""
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That executed perfectly! Thank you so much for your assistance with this