Hi,
I am new to powershell, with the help of some forums i was able to make a script which helps to expand the C Drive of the VM's in a vcenter. But i am not sure how to handle the error's in it. For example, if one of the hostname given in the .CSV file is not valid or not able to connect i need to get the error as "Unable to connect the server".. I heard it can be done through try and catch but doesn't know how to use it..Also i do believe this can be handled in less number of lines... any help on these also will be appreciated
The code i used is given below..
$vcc = "VCName"
Connect-VIServer $vcc
Import-Csv "Servers.csv" | % {
$vm_list = get-vm -name $_.Name
foreach($vm in $vm_list) {
$Drive_Size =""
$New_SizeGB =""
$New_SizeKB =""
Write-Host "------------------------------"
Write-Host "$VM"
ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name -EA SilentlyContinue)) {
if($harddisk.name -eq "Hard disk 1"){
$Drive_Size = $HardDisk.CapacityGB
Write-Host "Size Before Expansion : $Drive_Size"
$New_SizeGB = [decimal]::round($HardDisk.CapacityGB) + 10
$New_SizeKB = ($New_SizeGB * 1024 * 1024)
$disk1 = Get-VM $VM.Name | Get-HardDisk | Where {$_.Name -eq "Hard disk 1"}
Set-HardDisk -harddisk $disk1 -CapacityKB $New_SizeKB -Confirm:$false | out-null
}
}
ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
if($harddisk.name -eq "Hard disk 1"){
$DriveAft = $HardDisk.CapacityGB
Write-Host "Size After Expansion : $DriveAft "
}
}
}
}