Invoke-Command and Get-PartitionSupportedSize

Welcome Forums General PowerShell Q&A Invoke-Command and Get-PartitionSupportedSize

This topic contains 4 replies, has 3 voices, and was last updated by

1 year, 9 months ago.

  • Author
  • #69691

    Points: 4
    Rank: Member

    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}

  • #69712

    Points: 41
    Rank: Member

    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
  • #69742

    Points: 0
    Rank: Member

    You could also you cim session.

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

    Points: 4
    Rank: Member

    CimSession totally did it!
    Thank you.

  • #69784

    Points: 4
    Rank: Member

    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

The topic ‘Invoke-Command and Get-PartitionSupportedSize’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort