Author Posts

April 27, 2017 at 2:21 pm

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}

April 27, 2017 at 9:46 pm

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

}

April 28, 2017 at 10:49 am

You could also you cim session.

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

April 28, 2017 at 2:42 pm

CimSession totally did it!
Thank you.

April 28, 2017 at 4:06 pm

In case anyone is interested in my final function.

Function Resize-DiskPartition
{
    

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory,Position=1)]
            [string]$ComputerName,
        [Parameter(Mandatory,Position=2)]
            [string]$DriveLetter
    )

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

    Process{
        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