Skip to content

Instantly share code, notes, and snippets.

@nenkoru
Last active November 1, 2024 14:27
Show Gist options
  • Select an option

  • Save nenkoru/182364e1c9a30b9ba53e621bcf6ba5fb to your computer and use it in GitHub Desktop.

Select an option

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
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