PowerCLI – increase C drive on Windows server

I needed to make an automated script for configuring the VM’s hard disk 1 to a size of 100 GB, specifically targeting the C partition on a Windows server. Please refer to the usage instructions below for the script, which includes relevant parameters.

Usage:

.\increase-partition.ps1 -vcenter vcsa.local -username * -password * -server “server_fqdn”

Script

#requires -modules VMware.VimAutomation.Core

param (
    [CmdletBinding()]
    [Parameter(Mandatory = $true)]
    [string]$VCenter,
     
    [Parameter(Mandatory = $true)]
    [string]$Username,
     
    [Parameter(Mandatory = $true)]
    [string]$Password,
     
    [Parameter(Mandatory = $true)]
    [string]$server
)
 
Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue
Set-PowerCLIConfiguration -ProxyPolicy NoProxy -DefaultVIServerMode multiple -Scope User -InvalidCertificateAction ignore -Confirm:$false | Out-Null
 
# Connection to vCenter
try {
    Connect-VIServer -Server $VCenter -User $Username -Password $Password -ErrorAction Stop | Out-Null
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidLogin] {
    Write-Host "Cannot connect to $VCenter with provided credentials" -ForegroundColor Red
    Continue
}
catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViServerConnectionException] {
    Write-Host "Cannot connect to $VCenter - check IP/FQDN" -ForegroundColor Red
    Continue
}
catch {
    Write-Host "Cannot connect to $VCenter - Unknown error" -ForegroundColor Red
    Continue
}

Get-HardDisk -server $vcenter -VM $computer | Where-Object {$_.Name -eq "Hard Disk 1"} | Set-HardDisk -CapacityGB "100" -confirm:$false

Disconnect-VIserver -Server $vcu -Confirm:$false
 

Loading