如何使用 powershell 为虚拟机远程扩展分区

how to extend partition remotely for a virtual machine using powershell

我试图编写一个 Powershell 脚本来远程扩展虚拟机的分区。我已将 1gb 添加到现有的 C 驱动器,这将使它在 VDI 上达到 81GB,但我故意没有扩展它,因为我想通过此脚本进行扩展。

关于我做错了什么的任何想法,我想 运行 它在一个列表驻留在文本文件中的组机器上:

$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
}

任何帮助将不胜感激,因为我是这方面的初学者。

亲切的问候

您可以使用 invoke-command (http://www.computerperformance.co.uk/powershell/powershell_invoke.htm) 执行远程操作,当您想要在该远程计算机上使用本地变量时,您必须在前面加上 "using:"(并计算为 GB只是 "dividing to GB")

因为我现在无法测试 - 以下是它的方式 "should work"(如:未测试,但我希望你明白)

$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}
}

都可以在远程机器上完成:

$Svr = Get-Content 'C:\MININT\Computers.txt'

$ScriptBlock = {
$MaxSize = (Get-PartitionSupportedSize -DriveLetter C).Sizemax

Resize-Partition -DriveLetter C -Size $MaxSize
}

Invoke-Command -ComputerName $Svr -ScriptBlock $ScriptBlock