Invoke-Command and Get-PartitionSupportedSize

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Darwin Reiswig Darwin Reiswig 3 weeks, 3 days ago.

  • Author
    Posts
  • #69691
    Profile photo of Darwin Reiswig
    Darwin Reiswig
    Participant

    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
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    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

    You could also you cim session.

    $session = New-CimSession -ComputerName $computername
    Get-PartitionSupportedSize -CimSession $session -DriveLetter $driveletter
    
  • #69775
    Profile photo of Darwin Reiswig
    Darwin Reiswig
    Participant

    CimSession totally did it!
    Thank you.

  • #69784
    Profile photo of Darwin Reiswig
    Darwin Reiswig
    Participant

    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
    

You must be logged in to reply to this topic.