How can I use `Set-Disk` inside a PowerShell pipeline? - powershell

I am using the following PowerShell pipeline to reset a USB drive to my needs:
Clear-Disk -Number <NUMBER_OF_USB_DRIVE> -RemoveData -RemoveOEM -Confirm:$false -PassThru | New-Partition -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -FileSystem FAT32
After Clear-Disk, I'd like to insert Set-Disk -PartitionStyle MBR. The cmdlet accepts input objects from the pipeline, so I can use it and pipe objects to it. But it does not return any results and does not have a -PassThru parameter, so I can process objects from it further in my pipeline. It looks like Set-Disk can only be used at the end of a pipeline.
Are there other tricks available, so that I can use Set-Disk inside a pipeline?
My current workaround looks like that:
Clear-Disk -Number <NUMBER_OF_USB_DRIVE> -RemoveData -RemoveOEM -Confirm:$false -PassThru | %{
Set-Disk -InputObject $_ -PartitionStyle MBR
New-Partition -InputObject $_ -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -FileSystem FAT32
}
But I don't like it much, because technically a Foreach-Object should not really be necessary to process exactly one object.

Initialize-Disk would follow your requirements as it also supports -PassThru. As Clear-Disk is supposed to:
Cleans a disk by removing all partition information and un-initializing it, erasing all data on the disk.
Using Initialize-Disk should be possible. Second example for it is:
Example 2: Initialize a disk using the MBR partition style
Initialize-Disk -Number 1 -PartitionStyle MBR
It doesn't look like you're currently doing more with Set-Disk.

Related

Powershell - Disk Partition and Format - Conditions

I am building some servers using Terraform and on these servers, I am assigning multiple volumes, for example:
F:\ 100GB
G:\ 50GB
H:\ 200GB
When the server is built, I see all the volumes in diskmgmt but all uninitialized so I have created a Cloudinit script with the following as a starter, which initialises the disks and is all good
Get-Disk | Where-Object {$_.PartitionStyle -eq 'Raw'} | Initialize-Disk -PartitionStyle MBR
If I then run this powershell, it formats the disks and assigns the drive letter
New-Partition -DiskNumber 1 -DriveLetter 'F' -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "Data F" -Confirm:$false
New-Partition -DiskNumber 2 -DriveLetter 'G' -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "Data G" -Confirm:$false
New-Partition -DiskNumber 3 -DriveLetter 'H' -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "Data H" -Confirm:$false
The issue I have is that every server build assigns a different Disk number against a volume. So the first build might have this, in which case my drive letters and volumes are all good
Disk 1 - 100GB
Disk 2 - 50GB
Disk 3 - 200GB
The next build would have these disk numbers assigned which then means the drive letters and names are incorrect
Disk 1 - 50GB
Disk 2 - 200GB
Disk 3 - 100GB
I think I need to have the Powershell with some logic to do something like
Get-Disk | Where-Object {$_.'Total Size' -eq '100 GB'} | New-Partition -DriveLetter 'F' -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "Data F" -Confirm:$false
I run this but nothing happens, no error, just seems to run and suggest it may have done something but hasn't. I think the issue might be with my rookie Powershell.
Any tips ?
Thanks

How to do this in PowerShell?

I have a dps script that does this
create vdisk file="C:\Image.vhd" maximum=10000 type=expandable
attach vdisk
create partition primary
format fs=ntfs quick
rescan
assign letter=I
How can I do this in PS 5.1?
You would have to install Hyper-V and the associated PowerShell Module and then you could create the disk:
New-VHD -Path "C:\Image.vhd" -Dynamic -SizeBytes 10485760000
Mount, or attach, the disk then initialize it (assuming there is only one extra disk, if there are more than one disk you'd need to get the number using get-disk).
Mount-VHD -Path "C:\Image.vhd"
Initialize-Disk -Number 1 -PartitionStyle GPT
Then create the partition, set the drive letter and quickly format with NTFS
New-Partition -DiskNumber 1 -DriveLetter I | Format-Volume -FileSystem NTFS -Quick

Take a disk offline from powershell

normally to take a disk offline I use the windows disk partition manager. now i would need to do it with a power shell using the drive letter.
I tried with the command:
Set-Partition -DriveLetter E -IsOffline $True
i get this error from powershell:
Set-Partition : The volume still has access path to it.
Any suggestions?
I solved it like this:
set-disk (get-partition -DriveLetter E | get-disk | select number -ExpandProperty number) -isOffline $true

Remove Multiple Snapshots at once in PowerCLI

I am trying to delete old snapshots after patching using PowerCLI. The code i am using now is:
Get-VM | Get-Snapshot | Remove-Snapshot -confirm$false
It works great...but it only removes one at a time, and i would like it to do 2-3 at a time. Is this possible?
Thanks in advance!
This code will remove multiple snapshots from all virtual machines:
Get-VM | Get-Snapshot | % { Remove-Snapshot $_ -Confirm:$false }
I would recommend selecting a single virtual machine and testing first:
$VM = Get-VM -Name 'My Virtual Machine'
$VM | Get-Snapshot | % { Remove-Snapshot $_ -Confirm:$false }
Tested to work on PowerCLI 6.5.
I would recommend taking a look at the 'RunAsync' parameter. This will create the task and then move onto the next task without waiting for the prior task to complete.
Example:
Get-VM | Get-Snapshot | Remove-Snapshot -RunAsync -Confirm:$false

Powershell: Initialise and mount VHD disks

Powershell: Initialise and mount VHD disks
Using Powershell and VBoxManage (VirtualBox), I can create and mount a VHD disk, as follows:
$vhdpath="c:\myvhd.vhd"
vboxmanage createmedium disk --filename $vhdpath --sizebyte 200 --format VHD --variant Fixed
$vhd = Mount-DiskImage -PassThru $vhdpath -StorageType VHD
if(-not $vhd) {
Write-Host "Error mounting VHD"
exit
}
$vhd=Get-DiskImage -ImagePath $vhd.ImagePath
Initialize-Disk -Number $vhd.Number -PartitionStyle MBR
$partition = New-Partition -AssignDriveLetter -UseMaximumSize -DiskNumber $vhd.Number
$volume = Format-Volume -FileSystem FAT32 -Confirm:$false -Force -Partition $partition
Of course it is possible to replace vboxmanage ... with Hyper-V equivalent commands.
Two questions:
When the VHD disk is visible to PS, so is to the OS and, before the PS script formats it, an annoying Windows pop-up appears proposing to format the disk. How can I get rid of it?
In general, it is possible to mount a VHD disk as a removable disk. Is it possible to do so with PS too?
I am primarily (but not necessarily) looking for commands that do not require Hyper-V being installed.