Invoke-Command and Get-PartitionSupportedSize

Scenario: I increase the size of a VHD in VMWare, and I need to expand the partition on that volume to use the newly added VHD space. I’m trying to write a function to do this.

If I run Get-PartitionSupportedSize locally on the server, it returns the size of the volume. If I run invoke-command to do it remotely, it returns nothing.

Here’s the relevant portion of my function.

$DriveScript = “(Get-PartitionSupportedSize -DriveLetter $DriveLetter)”
$Drive = Invoke-Command -ComputerName $ComputerName -ScriptBlock {$DriveScript}

$SetSize = [int64]$Drive.sizeMax

$DriveScript = “Resize-Partition -DriveLetter $DriveLetter -Size $($SetSize/1GB)GB”
#Invoke-Command -ComputerName $ComputerName -ScriptBlock {$DriveScript}

I think the main problem is that the remote computer won’t know about $DriveLetter so you’ll need to use $Using to pass that variable through Invoke-Command. However, to do that you can’t declare your ScriptBlock as a String because you’ll get an error.

$DriveLetter = 'c'

Invoke-Command -ComputerName $computer -ScriptBlock {
    Get-PartitionSupportedSize -DriveLetter $Using:DriveLetter


You could also you cim session.

$session = New-CimSession -ComputerName $computername
Get-PartitionSupportedSize -CimSession $session -DriveLetter $driveletter

CimSession totally did it!
Thank you.

In case anyone is interested in my final function.

Function Resize-DiskPartition


        Write-Verbose "Expanding drive $DriveLetter on $ComputerName"
        $Session = New-CimSession -ComputerName $ComputerName

        Write-Verbose "Retrieving maximum partition size"
        $Drive = Get-PartitionSupportedSize -CimSession $Session -DriveLetter $driveletter
        $SetSize = [int64]$Drive.sizeMax
        Write-Verbose "Expanding partition to $($SetSize/1GB) GB"
        Resize-Partition -DriveLetter $DriveLetter -Size $SetSize -CimSession $Session

    End {
        Remove-CimSession $Session
        Write-Verbose "Disk expansion complete"
} #End function Resize-DiskPartition