Identify the correct disk to extend on OS
param(
[string]$drivename #ex: C or D
$percentage = 1.1
)
try{
$moddrivename = $drivename + ":"
$extendnumber = [math]::Round(((Get-WmiObject -Class Win32_LogicalDisk | where DeviceID -like $moddrivename | select -ExpandProperty Size) /1TB)*$percentage)
if ($extendnumber -ge 2) {
return "2TBDisk"
}
$searchstring = '\\' +$env:computername+ '\root\cimv2:Win32_LogicalDisk.DeviceID="' +$drivename+ ':"'
$disknumberlong = Get-WmiObject -Class Win32_LogicalDiskToPartition | where Dependent -Like $searchstring | select -ExpandProperty Antecedent
$disknumberlong -match 'Disk #(\d+)' | out-null
$disknumber = $matches[0].split('#')[1]
return $disknumber
} catch {
throw
}
Extending VM disk
param(
[String]$serverfqdn,
[String]$disknumber, #passed from the previous script
[double]$percentage=1.1 #percentage at which the disk should be extended by
)
Get-Module -Name VMware* -ListAvailable | Import-Module #call PowerCLI module
$vcusername = ''
$vcpassword = ''
$hostname = $serverfqdn.split(".")[0]
if ($hostname -like 'some naming convention') {
$vcservers = @("vc1","vc2")
foreach ($vc in $vcservers) {
Connect-VIServer -Server $vc -User $vcusername -Password $vcpassword
if ($vm = Get-VM $hostname) {
$correctvc = $vc
break
}
else {
Disconnect-VIServer -Server $vc -Confirm:$false
continue
}
}
} elseif ($hostname -like 'some naming convention 2') {
$vcservers = @("vc3","vc4")
foreach ($vc in $vcservers) {
Connect-VIServer -Server $vc -User $vcusername -Password $vcpassword
if ($vm = Get-VM $hostname) {
$correctvc = $vc
break
}
else {
Disconnect-VIServer -Server $vc -Confirm:$false
continue
}
}
}
$SCSI = '0:'+$disknumber
#heres the tricky part, pass each disk on the VM to get-scsicontroller which returns it's respective busnumber and unit number. Then get the disk name by filtering the result using the $SCSI variable.
$diskname = Get-VM $hostname | Get-HardDisk | Select @{N='VM';E={$_.Parent.Name}},Name,@{N='Node';E={'{0}:{1}' -f ((Get-ScsiController -HardDisk $_).ExtensionData.BusNumber),$_.ExtensionData.UnitNumber}} | where Node-like $SCSI | select Name -ExpandProperty Name
$disksize = Get-HardDisk -VM $hostname | where {$_.Name -eq $diskname} | select -ExpandProperty CapacityGB
$newdisksize = $disksize*$percentage
Write-Host $newdisksize
Get-HardDisk -VM $hostname | where {$_.Name -eq $diskname} | Set-HardDisk -CapacityGB $newdisksize -confirm:$false
Disconnect-VIServer -Server $correctvc -Confirm:$false
Extending disk on OS
param(
[string]$drivename,
[double]$percentage=0.1
)
try{
"RESCAN" | diskpart
$moddrivename = $drivename + ":"
$extendnumber = [math]::Round(((Get-WmiObject -Class Win32_LogicalDisk | where DeviceID -like $moddrivename | select -ExpandProperty Size) /1MB)*$percentage)
write-host $extendnumber
$searchstring = '\\' +$env:computername+ '\root\cimv2:Win32_LogicalDisk.DeviceID="' +$drivename+ ':"'
$disknumberlong = Get-WmiObject -Class Win32_LogicalDiskToPartition | where Dependent -Like $searchstring | select -ExpandProperty Antecedent
$disknumberlong -match 'Disk #(\d)'
$disknumber = $matches[0].split('#')[1]
$string1 = "select disk " + $disknumber
$string2 = "select volume " + $drivename
$string3 = "extend size=" + $extendnumber
$commands = @(
$string1,
$string2,
$string3
)
$commands | diskpart
Write-Host "Disk extension on OS layer completed"
exit 0
} catch {
Write-Host "Disk extension on OS layer failed"
exit -1
}
No comments:
Post a Comment