Search code examples
powershellpowershell-4.0powershell-remoting

how to extend partition remotely for a virtual machine using powershell


I have attempted to write a Powershell script to remotely extend a partition of a virtual machine. I have added 1gb to the existing C drive which would make it 81GB on the VDI but I deliberately did not extended it as I want to do it by this script.

Any ideas of what I am doing wrong as I would like to run it on a group machines which a list resides in the text file:

$servers = Get-Content 'C:\MININT\Computers.txt'
$GlobalResults = @()
$ConvertToGB = ( 1024 * 1024 * 1024 )
ForEach ($server in $servers)
{
    $disk = get-wmiobject win32_LogicalDisk -ComputerName $server -Filter "DeviceID='C:'" | Select-object size,Freespace    
$server + "," + ($disk.size / $ConvertToGB) + "," + ($disk.Freespace / $ConvertToGB)    
$MaxSize =  (Get-PartitionSupportedSize -DriveLetter c).sizeMax
    Resize-Partition -DriveLetter c -Size $MaxSize
}

Any assist would be greatly appreciated as I am a beginner on this.

Kind regards


Solution

  • you can perform remote actions with invoke-command (http://www.computerperformance.co.uk/powershell/powershell_invoke.htm) where you have to prepend "using:" when you want to use local variables on that remote machine (and calculate to GB by simply "dividing to GB")

    as I cannot test right now - the following is how it "should work" (as in: not tested but I hope you get the idea)

    $servers = Get-Content 'C:\MININT\Computers.txt'
    ForEach ($server in $servers)
    {      
        $MaxSize = (invoke-command -ComputerName $server -ScriptBlock { Get-PartitionSupportedSize -DriveLetter c }).sizeMax
        invoke-command -ComputerName $server -ScriptBlock {Resize-Partition -DriveLetter c -Size $using:MaxSize}
    }