Last active
November 1, 2024 14:27
-
-
Save nenkoru/182364e1c9a30b9ba53e621bcf6ba5fb to your computer and use it in GitHub Desktop.
One-liner to get a JSON list of all iSCSI disks that are not attached to the host machine and are not attached to any of the Hyper-V VMs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Get-PhysicalDisk | Where-Object { $_.FriendlyName -like "*iSCSI*" -and -not ((Get-VM | Get-VMHardDiskDrive | Select-Object -ExpandProperty DiskNumber -ErrorAction SilentlyContinue) -contains $_.DeviceID) } | Where-Object { $driveLetters = Get-Volume | Select-Object -ExpandProperty DriveLetter -ErrorAction SilentlyContinue; $diskIndices = $driveLetters | ForEach-Object { $driveLetter = $_; Get-CimInstance CIM_LogicalDiskBasedOnPartition | Where-Object { $_.Dependent -like "*DeviceID = `"$driveLetter`:`"*"} | ForEach-Object { if ($_.Antecedent -match "Disk #(\d+)") { $matches[1] } } }; $diskIndices -notcontains $_.DeviceID } | ConvertTo-JSON |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment