Hi,
We have 500 MB as recovery partition. We would like to remove that partition from all of our VMs. Is there any powershell script which can do that.
Thread reported so moderators know it should be moved to the area for PowerCLI.
Assuming you are talking about a Windows Guest OS, you could try the Remove-Partition cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, you are correct. This is required from all Win 10 VDIs.
This is what I wrote so far but does not work yet
$File = Get-Filename
$csv = Import-Csv "$file"
foreach($item in $csv)
{
Get-VM -Name $($item.name) | Stop-VM -confirm:$false
Remove-Partition -DiskNumber ((Get-Disk).Number) -PartitionNumber ((Get-Partition -DiskNumber ((Get-Disk).Number) | Where-Object {$_.Type -eq 'Recovery'}).PartitionNumber) -PassThru -Confirm:$false | Start-VM
}
}
Not sure what the Get-FileName is supposed to do.
Is the Get-Partition actually returning a Recovery partition?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry, pasted only half of the script.
Function Get-FileName($InitialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "CSV (*.csv) | *.csv"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.FileName
}
And is Get-Partition returning a Recovery partition?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This gives the recovery partition details. Need to use this in the remove-partition line
Get-Partition | Where-Object -FilterScript {$_.Type -eq 'Recovery'}
And this does not work?
Get-Partition | Where-Object -FilterScript {$_.Type -eq 'Recovery'} |
Remove-Partition
Note that this has to be executed from an elevated PS prompt
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I used these two lines but it did not remove it. It is shutting down the VM but does not remove
Get-VM -Name $($item.name) | Stop-VM -confirm:$false
Get-Partition | Where-Object -FilterScript {$_.Type -eq 'Recovery'} | Remove-Partition -PassThru -Confirm:$false | Start-VM
I'm confused, you stop the VM, and then want the Guest OS to remove a partition?
Are you running the Get-Partition and Remove-Partition from an elevated PS prompt inside the Guest OS?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry for not being clear.
I will have a csv file with VM names. The script will power off the VMs and then go one by one and remove the partition and then power them on. does it make sense?
Did you actually read my last reply?
You have to run those cmdlets inside the Guest OS running on that VM.
And that is not possible when you stop the VM
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Oh I missed that. My bad.
I will test using SCCM to push this script to run remotely when the VM is running
I tested the scripts, it works.
BTW, there is no Type of "Recovery", it is "Unknown", the "Unknown" is the "Recovery Partition from Windows Disk manager.
PS C:\WINDOWS\system32> Get-Partition
DiskPath: \\?\scsi#disk&ven_vmware&prod_virtual_disk#5&35015fb0&0&000000#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
PartitionNumber DriveLetter Offset Size Type
--------------- ----------- ------ ---- ----
1 1048576 350 MB IFS
2 C 368050176 74.22 GB IFS
3 80056680448 450 MB Unknown
PS C:\WINDOWS\system32> Get-Partition | Where-Object -FilterScript {$_.Type -eq 'Unknown'}
DiskPath: \\?\scsi#disk&ven_vmware&prod_virtual_disk#5&35015fb0&0&000000#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
PartitionNumber DriveLetter Offset Size Type
--------------- ----------- ------ ---- ----
3 80056680448 450 MB Unknown
S C:\WINDOWS\system32> Get-Partition | Where-Object -FilterScript {$_.Type -eq 'Unknown'} | Remove-Partition -PassThru -Confirm:$false
DiskPath: \\?\scsi#disk&ven_vmware&prod_virtual_disk#5&35015fb0&0&000000#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
PartitionNumber DriveLetter Offset Size Type
--------------- ----------- ------ ---- ----
3 80056680448 450 MB Unknown
PS C:\WINDOWS\system32> Get-Partition | Where-Object -FilterScript {$_.Type -eq 'Unknown'}
PS C:\WINDOWS\system32>
I did the same thing from my other Windows 2019 server, ONLY within disk manager or disk part, it is called "Recovery", from Get-Partition it is "Unknown" under the Type column
You do not need to Power of a VM in order to do it, it can be done online without impact anything
Disk manager after I deleted the "Unknown" partition using
Get-Partition | Where-Object -FilterScript {$_.Type -eq 'Unknown'}| Remove-Partition -PassThru -Confirm:$false | ft -AutoSize
The same can be done within diskpart as well.
The following is the screen log I did on one of the Windows server 2019 using diskpart
#############################################################################################
C:\>diskpart
Microsoft DiskPart version 10.0.17763.1911
Copyright (C) Microsoft Corporation.
On computer: egncn6
DISKPART> list disk
Disk ### Status Size Free Dyn Gpt
-------- ------------- ------- ------- --- ---
Disk 0 Online 170 GB 20 GB
Disk 1 Online 100 GB 1024 KB
DISKPART> select disk
The arguments specified for this command are not valid.
For more information on the command type: HELP SELECT DISK
There is no disk selected.
DISKPART> select disk 0
Disk 0 is now the selected disk.
DISKPART> list disk
Disk ### Status Size Free Dyn Gpt
-------- ------------- ------- ------- --- ---
* Disk 0 Online 170 GB 20 GB
Disk 1 Online 100 GB 1024 KB
DISKPART> list partition
Partition ### Type Size Offset
------------- ---------------- ------- -------
Partition 1 Primary 500 MB 1024 KB
Partition 2 Primary 149 GB 501 MB
Partition 3 Recovery 501 MB 149 GB
DISKPART> select partition 3
Partition 3 is now the selected partition.
DISKPART> delete partition override
DiskPart successfully deleted the selected partition.
DISKPART> exit
Leaving DiskPart...
Please verify the label of the Partition because some time has the "Unknown" label name and the system can not found the name if you are using "Recovery" when you want remove it