private/Disk/Get-OSDeployDisk.ps1
|
function Get-OSDeployDisk { <# .SYNOPSIS Returns all physical disks available on the system. .OUTPUTS Microsoft.Management.Infrastructure.CimInstance .NOTES Author: David Segura Company: Recast Software Dependencies: PowerShell Modules: Storage .NET Classes: [Microsoft.Management.Infrastructure.CimSession], [Microsoft.Management.Infrastructure.CimInstance] #> [CmdletBinding()] [OutputType([Microsoft.Management.Infrastructure.CimInstance])] param ( [Alias('Disk','DiskNumber')] [uint32]$Number, [bool]$BootFromDisk, [bool]$IsBoot, [bool]$IsReadOnly, [bool]$IsSystem, [ValidateSet('1394','ATA','ATAPI','Fibre Channel','File Backed Virtual','iSCSI','MMC','MAX','Microsoft Reserved','NVMe','RAID','SAS','SATA','SCSI','SD','SSA','Storage Spaces','USB','Virtual')] [string[]]$BusType, [ValidateSet('1394','ATA','ATAPI','Fibre Channel','File Backed Virtual','iSCSI','MMC','MAX','Microsoft Reserved','NVMe','RAID','SAS','SATA','SCSI','SD','SSA','Storage Spaces','USB','Virtual')] [string[]]$BusTypeNot, [ValidateSet('SSD','HDD','SCM','Unspecified')] [string[]]$MediaType, [ValidateSet('SSD','HDD','SCM','Unspecified')] [string[]]$MediaTypeNot, [ValidateSet('GPT','MBR','RAW')] [string[]]$PartitionStyle, [ValidateSet('GPT','MBR','RAW')] [string[]]$PartitionStyleNot ) $StorageNamespace = 'ROOT/Microsoft/Windows/Storage' $CimSession = $null try { # Storage supplies the MSFT_Disk type data used by downstream Storage cmdlets; enumeration below uses the .NET CIM API. Import-Module Storage -ErrorAction Stop $CimSession = [Microsoft.Management.Infrastructure.CimSession]::Create($env:COMPUTERNAME) $GetDisk = @($CimSession.QueryInstances($StorageNamespace, 'WQL', 'SELECT * FROM MSFT_Disk')) | Sort-Object Number $GetPhysicalDisk = @($CimSession.QueryInstances($StorageNamespace, 'WQL', 'SELECT * FROM MSFT_PhysicalDisk')) | Sort-Object DeviceId } catch { Write-Error -Message "[$($MyInvocation.MyCommand.Name)] Unable to enumerate disks by using the Storage CIM provider. $($_.Exception.Message)" -ErrorAction Continue return } finally { if ($null -ne $CimSession) { $CimSession.Dispose() } } $PhysicalDiskByDeviceId = @{} foreach ($PhysicalDisk in $GetPhysicalDisk) { $PhysicalDiskByDeviceId[[string]$PhysicalDisk.DeviceId] = $PhysicalDisk } foreach ($Disk in $GetDisk) { # MSFT_Disk does not expose reliable media type on every device, so preserve the existing DeviceId-to-Number enrichment. if ($PhysicalDiskByDeviceId.ContainsKey([string]$Disk.Number)) { $Disk | Add-Member -NotePropertyName 'MediaType' -NotePropertyValue $PhysicalDiskByDeviceId[[string]$Disk.Number].MediaType -Force } $Disk | Add-Member -NotePropertyName 'SizeGB' -NotePropertyValue ([int]($Disk.Size / 1GB)) -Force } $GetDisk = $GetDisk | Where-Object {$_.IsOffline -eq $false} $GetDisk = $GetDisk | Where-Object {$_.OperationalStatus -ne 'No Media'} if ($PSBoundParameters.ContainsKey('Number')) { $GetDisk = $GetDisk | Where-Object {$_.Number -eq $Number} } if ($PSBoundParameters.ContainsKey('BootFromDisk')) {$GetDisk = $GetDisk | Where-Object {$_.BootFromDisk -eq $BootFromDisk}} if ($PSBoundParameters.ContainsKey('IsBoot')) {$GetDisk = $GetDisk | Where-Object {$_.IsBoot -eq $IsBoot}} if ($PSBoundParameters.ContainsKey('IsReadOnly')) {$GetDisk = $GetDisk | Where-Object {$_.IsReadOnly -eq $IsReadOnly}} if ($PSBoundParameters.ContainsKey('IsSystem')) {$GetDisk = $GetDisk | Where-Object {$_.IsSystem -eq $IsSystem}} if ($BusType) {$GetDisk = $GetDisk | Where-Object {$_.BusType -in $BusType}} if ($BusTypeNot) {$GetDisk = $GetDisk | Where-Object {$_.BusType -notin $BusTypeNot}} if ($MediaType) {$GetDisk = $GetDisk | Where-Object {$_.MediaType -in $MediaType}} if ($MediaTypeNot) {$GetDisk = $GetDisk | Where-Object {$_.MediaType -notin $MediaTypeNot}} if ($PartitionStyle) {$GetDisk = $GetDisk | Where-Object {$_.PartitionStyle -in $PartitionStyle}} if ($PartitionStyleNot) {$GetDisk = $GetDisk | Where-Object {$_.PartitionStyle -notin $PartitionStyleNot}} return $GetDisk } |