Skip to content

Commit f94ecce

Browse files
committed
Added Expand-SCVirtualMachineOSDisk function.
1 parent c691ffb commit f94ecce

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#requires -module VirtualMachineManager
2+
3+
<#PSScriptInfo
4+
5+
.Version
6+
1.0
7+
.Guid
8+
1badb9c5-8b26-4184-ab23-9e2de92306e7
9+
.Author
10+
Thomas Malkewitz @dotps1
11+
.Tags
12+
WindowsStore, Registry
13+
.ProjectUri
14+
https://github.com/dotps1/PSFunctions
15+
.ReleaseNotes
16+
Initial Release.
17+
.ExternalModuleDependancies
18+
VirtualMachineManager
19+
20+
#>
21+
22+
<#
23+
24+
.SYNOPSIS
25+
Expands the OS Drive of a Virtual Machine.
26+
.DESCRIPTION
27+
Using information from Virtual Machine Manager and CIM, this cmdlet adds space to the virtual machines vhdx file and expands the partion.
28+
.INPUTS
29+
System.String
30+
System.Int
31+
.OUTPUTS
32+
System.Management.Automation.PSCustomObject
33+
.PARAMETER Name
34+
System.String
35+
The name of the Virtual Machine to perform the expansion against.
36+
.PARAMETER AmmountToAddGB
37+
System.Int
38+
The ammount of disk space to add in GB.
39+
Default Value: 10
40+
.PARAMETER MinimumFreeSpaceGB
41+
System.Int
42+
The ammount of minimum free space on the disk required before the operation is performed.
43+
Default Value: 10
44+
.NOTES
45+
If the system recovery partion is located on the disk after the OS partition, it will be removed.
46+
.EXAMPLE
47+
PS C:\> Expand-SCVirtualMachineOSDisk -Name MyVirtualMachine
48+
49+
Name: MyVirtualMachine
50+
OSDriveLetter: C
51+
OSPartitionPreviousSizeGB: 40
52+
OSPartitionNewSizeGB: 50
53+
.EXAMPLE
54+
PS C:\> "MyVirtualMachine" | Expand-SCVirtualMachineOSDisk -AmmountToAddGB 25
55+
56+
Name: MyVirtualMachine
57+
OSDriveLetter: C:
58+
OSPartitionPreviousSizeGB: 25
59+
OSPartitionNewSizeGB: 50
60+
.LINK
61+
https://dotps1.github.io
62+
63+
#>
64+
65+
66+
[CmdletBinding(
67+
ConfirmImpact = "High"
68+
)]
69+
[OutputType(
70+
[System.Management.Automation.PSCustomObject]
71+
)]
72+
73+
param (
74+
[Parameter(
75+
Mandatory = $true,
76+
ValueFromPipeline = $true,
77+
ValueFromPipelineByPropertyName = $true
78+
)]
79+
[Alias(
80+
'ComputerName'
81+
)]
82+
[String[]]
83+
$Name,
84+
85+
[Parameter()]
86+
[Int]
87+
$AmountToAddGB = 10,
88+
89+
[Parameter()]
90+
[Int]
91+
$MinumumFreeSpaceGB = 10
92+
)
93+
94+
process {
95+
foreach ($nameValue in $Name) {
96+
$vm = $null
97+
$vm = Get-SCVirtualMachine -Name $nameValue
98+
if ($null -eq $vm) {
99+
Write-Warning -Message "Virtual Machine '$($vm.Name)' does not exist in VMM, please ensure the host and the guest are in the VMM database."
100+
} else {
101+
$osHardDisk = Get-SCVirtualHardDisk -VM $vm | Where-Object { $_.ID -eq $vm.OSDiskID }
102+
$osDiskDrive = Get-SCVirtualDiskDrive -VM $vm | Where-Object { $_.VirtualHardDiskId -eq $vm.OSDiskID }
103+
if ($null -ne $osHardDisk -and $null -ne $osDiskDrive) {
104+
$osHardDiskFreeSpace = [Math]::Round( ($osHardDisk.MaximumSize - $osHardDisk.Size) / 1GB )
105+
106+
# If the freespace is less then the minimum free space specified, add space to existing OSDrive.
107+
if ($osHardDiskFreeSpace -lt $MinumumFreeSpaceGB) {
108+
try {
109+
Expand-SCVirtualDiskDrive -VirtualDiskDrive $osDiskDrive -VirtualHardDiskSizeGB ( [Math]::Round( ($osHardDisk.MaximumSize / 1GB) ) + $AmountToAddGB ) -ErrorAction Stop | Out-Null
110+
} catch {
111+
$PSCmdlet.ThrowTerminatingError(
112+
$_
113+
)
114+
115+
continue
116+
}
117+
} else {
118+
Write-Warning "Virtual Machine '$($vm.Name)' has $osHardDiskFreeSpace avaiable."
119+
120+
continue
121+
}
122+
123+
if ($vm.Generation -ne 2) {
124+
Write-Warning -Message "Virtual Machine '$($vm.Name)' is not generation 2, and the volume will need to be manually expanded."
125+
} else {
126+
# Expand the partition via CIM.
127+
try {
128+
$cimSession = New-CimSession -ComputerName $vm
129+
} catch {
130+
$PSCmdlet.ThrowTerminatingError(
131+
$_
132+
)
133+
134+
continue
135+
}
136+
137+
$osDriveLetter = (Get-CimInstance -CimSession $cimSession -ClassName Win32_OperatingSystem).SystemDirectory.SubString( 0,1 )
138+
$osPartition = Get-Partition -CimSession $cimSession -DriveLetter $osDriveLetter
139+
$recoveryPartition = Get-Partition -CimSession $cimSession | Where-Object { $_.Type -eq 'Recovery' }
140+
if ($null -ne $recoveryPartition -and $recoveryPartition.PartitionNumber -gt $osPartition.PartitionNumber) {
141+
if ($recoveryPartition.Size -lt 1GB) {
142+
Write-Warning "Removing Recovery partition from '$($vm.Name)' to allow for the partition expansion."
143+
Remove-Partition -CimSession $cimSession -InputObject $recoveryPartition -Confirm:$false
144+
}
145+
}
146+
147+
$partitionSizeMax = (Get-PartitionSupportedSize -CimSession $cimSession -DriveLetter $osDriveLetter).SizeMax
148+
Resize-Partition -CimSession $cimSession -DriveLetter $osDriveLetter -Size $partitionSizeMax
149+
$osPartitionNewSize = Get-Partition -CimSession $cimSession -DriveLetter $osDriveLetter | Select-Object -ExpandProperty Size
150+
151+
Remove-CimSession -CimSession $cimSession
152+
153+
[PSCustomObject]@{
154+
Name = $vm.Name
155+
OSDriveLetter = $osDriveLetter
156+
OSPartiontType = $osPartition.Type
157+
OSPartitionPreviousSizeGB = ( [Math]::Round(( $osPartition.Size / 1GB )))
158+
OSPartitionNewSizeGB = ( [Math]::Round(( $osPartitionNewSize / 1GB )))
159+
} | Write-Output
160+
}
161+
}
162+
}
163+
}
164+
}

0 commit comments

Comments
 (0)