Take a disk offline from powershell - 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

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

Powershell Mount-VHD weird behavior when issued remotely

If i run the below code on the hyper-v hypervisor (locally), it runs successfully and i get the correct Drive-Letter.
$DriveLetter = (Mount-VHD "F:\Template\win2016Template.vhdx" -PassThru |
Get-Disk | Get-Partition | Get-Volume).DriveLetter
output: G
but if i modify it to run from any normal domain joined workstation using domain admin privileges i got incorrect Drive-Letter
$DriveLetter = (Mount-VHD -ComputerName RUF-HV01 "F:\Template\win2016Template.vhdx" -PassThru | Get-
Disk | Get-Partition | Get-Volume).DriveLetter
output: T
I have no idea where "T" is coming from. The correct drive letter is G not T.
Any ideas?
Thanks,

Using Powershell to make USB Bootable Drive

I am trying to automate someone taking a ISO and making that into a bootable USB. I need help with that last command in the script which is xcopy. How can I copy all the files from the user imputed $ISO to the USB drive X:? I am having issues using "$ISO" and ":" in a path. The last command needs work cause currently it errors out.
Write-Host "Please mount your ISO and insert your USB stick"
$ISO = Read-Host -Prompt 'Please enter your mounted ISO drive letter'
Write-Host "Your mounted drive letter is seleted as $ISO"
Get-Disk
$USBDisk = Read-Host -Prompt 'Please enter your USB Disk Number from above'
Get-Disk $USBDisk | Clear-Disk -RemoveData
New-Partition -DiskNumber $USBDisk -Size 30000 MB -IsActive -DriveLetter X | Format-Volume -FileSystem FAT32 -NewFileSystemLabel Win10
xcopy $ISO:\* X:\ /S
Try:
xcopy ${ISO}:\* X:\ /S
This is an alternate method of referencing a variable and is useful when you need a trailing colon and don't want to confuse the parser with looking for a variable namespace.

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.