finding volumes mapped to drives in Windows On EC2 Cloud
# List the disks
function Convert-SCSITargetIdToDeviceName {
param([int]$SCSITargetId)
If ($SCSITargetId -eq 0) {
return "sda1"
}
$deviceName = "xvd"
If ($SCSITargetId -gt 25) {
$deviceName += [char](0x60 + [int]($SCSITargetId / 26))
}
$deviceName += [char](0x61 + $SCSITargetId % 26)
return $deviceName
}
[string[]]$array1 = @()
[string[]]$array2 = @()
[string[]]$array3 = @()
[string[]]$array4 = @()
Get-WmiObject Win32_Volume | Select-Object Name, DeviceID | ForEach-Object {
$array1 += $_.Name
$array2 += $_.DeviceID
}
$i = 0
While ($i -ne ($array2.Count)) {
$array3 += ((Get-Volume -Path $array2[$i] | Get-Partition | Get-Disk).SerialNumber) -replace "_[^ ]*$" -replace "vol", "vol-"
$array4 += ((Get-Volume -Path $array2[$i] | Get-Partition | Get-Disk).FriendlyName)
$i ++
}
[array[]]$array = $array1, $array2, $array3, $array4
Try {
$InstanceId = Get-EC2InstanceMetadata -Category "InstanceId"
$Region = Get-EC2InstanceMetadata -Category "Region" | Select-Object -ExpandProperty SystemName
}
Catch {
Write-Host "Could not access the instance Metadata using AWS Get-EC2InstanceMetadata CMDLet.
Verify you have AWSPowershell SDK version '3.1.73.0' or greater installed and Metadata is enabled for this instance." -ForegroundColor Yellow
}
Try {
$BlockDeviceMappings = (Get-EC2Instance -Region $Region -Instance $InstanceId).Instances.BlockDeviceMappings
$VirtualDeviceMap = (Get-EC2InstanceMetadata -Category "BlockDeviceMapping").GetEnumerator() | Where-Object { $_.Key -ne "ami" }
}
Catch {
Write-Host "Could not access the AWS API, therefore, VolumeId is not available.
Verify that you provided your access keys or assigned an IAM role with adequate permissions." -ForegroundColor Yellow
}
Get-disk | ForEach-Object {
$DriveLetter = $null
$VolumeName = $null
$VirtualDevice = $null
$DeviceName = $_.FriendlyName
$DiskDrive = $_
$Disk = $_.Number
$Partitions = $_.NumberOfPartitions
$EbsVolumeID = $_.SerialNumber -replace "_[^ ]*$" -replace "vol", "vol-"
if ($Partitions -ge 1) {
$PartitionsData = Get-Partition -DiskId $_.Path
$DriveLetter = $PartitionsData.DriveLetter | Where-object { $_ -notin @("", $null) }
$VolumeName = (Get-PSDrive | Where-Object { $_.Name -in @($DriveLetter) }).Description | Where-object { $_ -notin @("", $null) }
}
If ($DiskDrive.path -like "*PROD_PVDISK*") {
$BlockDeviceName = Convert-SCSITargetIdToDeviceName((Get-WmiObject -Class Win32_Diskdrive | Where-Object { $_.DeviceID -eq ("\\.\PHYSICALDRIVE" + $DiskDrive.Number) }).SCSITargetId)
$BlockDeviceName = "/dev/" + $BlockDeviceName
$BlockDevice = $BlockDeviceMappings | Where-Object { $BlockDeviceName -like "*" + $_.DeviceName + "*" }
$EbsVolumeID = $BlockDevice.Ebs.VolumeId
$VirtualDevice = ($VirtualDeviceMap.GetEnumerator() | Where-Object { $_.Value -eq $BlockDeviceName }).Key | Select-Object -First 1
}
ElseIf ($DiskDrive.path -like "*PROD_AMAZON_EC2_NVME*") {
$BlockDeviceName = (Get-EC2InstanceMetadata -Category "BlockDeviceMapping").ephemeral((Get-WmiObject -Class Win32_Diskdrive | Where-Object { $_.DeviceID -eq ("\\.\PHYSICALDRIVE" + $DiskDrive.Number) }).SCSIPort - 2)
$BlockDevice = $null
$VirtualDevice = ($VirtualDeviceMap.GetEnumerator() | Where-Object { $_.Value -eq $BlockDeviceName }).Key | Select-Object -First 1
}
ElseIf ($DiskDrive.path -like "*PROD_AMAZON*") {
if ($DriveLetter -match '[^a-zA-Z0-9]') {
$i = 0
While ($i -ne ($array3.Count)) {
if ($array[2][$i] -eq $EbsVolumeID) {
$DriveLetter = $array[0][$i]
$DeviceName = $array[3][$i]
}
$i ++
}
}
$BlockDevice = ""
$BlockDeviceName = ($BlockDeviceMappings | Where-Object { $_.ebs.VolumeId -eq $EbsVolumeID }).DeviceName
}
ElseIf ($DiskDrive.path -like "*NETAPP*") {
if ($DriveLetter -match '[^a-zA-Z0-9]') {
$i = 0
While ($i -ne ($array3.Count)) {
if ($array[2][$i] -eq $EbsVolumeID) {
$DriveLetter = $array[0][$i]
$DeviceName = $array[3][$i]
}
$i ++
}
}
$EbsVolumeID = "FSxN Volume"
$BlockDevice = ""
$BlockDeviceName = ($BlockDeviceMappings | Where-Object { $_.ebs.VolumeId -eq $EbsVolumeID }).DeviceName
}
Else {
$BlockDeviceName = $null
$BlockDevice = $null
}
New-Object PSObject -Property @{
Disk = $Disk;
Partitions = $Partitions;
DriveLetter = If ($DriveLetter -eq $null) { "N/A" } Else { $DriveLetter };
EbsVolumeId = If ($EbsVolumeID -eq $null) { "N/A" } Else { $EbsVolumeID };
Device = If ($BlockDeviceName -eq $null) { "N/A" } Else { $BlockDeviceName };
VirtualDevice = If ($VirtualDevice -eq $null) { "N/A" } Else { $VirtualDevice };
VolumeName = If ($VolumeName -eq $null) { "N/A" } Else { $VolumeName };
DeviceName = If ($DeviceName -eq $null) { "N/A" } Else { $DeviceName };
}
} | Sort-Object Disk | Format-Table -AutoSize -Property Disk, Partitions, DriveLetter, EbsVolumeId, Device, VirtualDevice, DeviceName, VolumeName
function Convert-SCSITargetIdToDeviceName {
param([int]$SCSITargetId)
If ($SCSITargetId -eq 0) {
return "sda1"
}
$deviceName = "xvd"
If ($SCSITargetId -gt 25) {
$deviceName += [char](0x60 + [int]($SCSITargetId / 26))
}
$deviceName += [char](0x61 + $SCSITargetId % 26)
return $deviceName
}
[string[]]$array1 = @()
[string[]]$array2 = @()
[string[]]$array3 = @()
[string[]]$array4 = @()
Get-WmiObject Win32_Volume | Select-Object Name, DeviceID | ForEach-Object {
$array1 += $_.Name
$array2 += $_.DeviceID
}
$i = 0
While ($i -ne ($array2.Count)) {
$array3 += ((Get-Volume -Path $array2[$i] | Get-Partition | Get-Disk).SerialNumber) -replace "_[^ ]*$" -replace "vol", "vol-"
$array4 += ((Get-Volume -Path $array2[$i] | Get-Partition | Get-Disk).FriendlyName)
$i ++
}
[array[]]$array = $array1, $array2, $array3, $array4
Try {
$InstanceId = Get-EC2InstanceMetadata -Category "InstanceId"
$Region = Get-EC2InstanceMetadata -Category "Region" | Select-Object -ExpandProperty SystemName
}
Catch {
Write-Host "Could not access the instance Metadata using AWS Get-EC2InstanceMetadata CMDLet.
Verify you have AWSPowershell SDK version '3.1.73.0' or greater installed and Metadata is enabled for this instance." -ForegroundColor Yellow
}
Try {
$BlockDeviceMappings = (Get-EC2Instance -Region $Region -Instance $InstanceId).Instances.BlockDeviceMappings
$VirtualDeviceMap = (Get-EC2InstanceMetadata -Category "BlockDeviceMapping").GetEnumerator() | Where-Object { $_.Key -ne "ami" }
}
Catch {
Write-Host "Could not access the AWS API, therefore, VolumeId is not available.
Verify that you provided your access keys or assigned an IAM role with adequate permissions." -ForegroundColor Yellow
}
Get-disk | ForEach-Object {
$DriveLetter = $null
$VolumeName = $null
$VirtualDevice = $null
$DeviceName = $_.FriendlyName
$DiskDrive = $_
$Disk = $_.Number
$Partitions = $_.NumberOfPartitions
$EbsVolumeID = $_.SerialNumber -replace "_[^ ]*$" -replace "vol", "vol-"
if ($Partitions -ge 1) {
$PartitionsData = Get-Partition -DiskId $_.Path
$DriveLetter = $PartitionsData.DriveLetter | Where-object { $_ -notin @("", $null) }
$VolumeName = (Get-PSDrive | Where-Object { $_.Name -in @($DriveLetter) }).Description | Where-object { $_ -notin @("", $null) }
}
If ($DiskDrive.path -like "*PROD_PVDISK*") {
$BlockDeviceName = Convert-SCSITargetIdToDeviceName((Get-WmiObject -Class Win32_Diskdrive | Where-Object { $_.DeviceID -eq ("\\.\PHYSICALDRIVE" + $DiskDrive.Number) }).SCSITargetId)
$BlockDeviceName = "/dev/" + $BlockDeviceName
$BlockDevice = $BlockDeviceMappings | Where-Object { $BlockDeviceName -like "*" + $_.DeviceName + "*" }
$EbsVolumeID = $BlockDevice.Ebs.VolumeId
$VirtualDevice = ($VirtualDeviceMap.GetEnumerator() | Where-Object { $_.Value -eq $BlockDeviceName }).Key | Select-Object -First 1
}
ElseIf ($DiskDrive.path -like "*PROD_AMAZON_EC2_NVME*") {
$BlockDeviceName = (Get-EC2InstanceMetadata -Category "BlockDeviceMapping").ephemeral((Get-WmiObject -Class Win32_Diskdrive | Where-Object { $_.DeviceID -eq ("\\.\PHYSICALDRIVE" + $DiskDrive.Number) }).SCSIPort - 2)
$BlockDevice = $null
$VirtualDevice = ($VirtualDeviceMap.GetEnumerator() | Where-Object { $_.Value -eq $BlockDeviceName }).Key | Select-Object -First 1
}
ElseIf ($DiskDrive.path -like "*PROD_AMAZON*") {
if ($DriveLetter -match '[^a-zA-Z0-9]') {
$i = 0
While ($i -ne ($array3.Count)) {
if ($array[2][$i] -eq $EbsVolumeID) {
$DriveLetter = $array[0][$i]
$DeviceName = $array[3][$i]
}
$i ++
}
}
$BlockDevice = ""
$BlockDeviceName = ($BlockDeviceMappings | Where-Object { $_.ebs.VolumeId -eq $EbsVolumeID }).DeviceName
}
ElseIf ($DiskDrive.path -like "*NETAPP*") {
if ($DriveLetter -match '[^a-zA-Z0-9]') {
$i = 0
While ($i -ne ($array3.Count)) {
if ($array[2][$i] -eq $EbsVolumeID) {
$DriveLetter = $array[0][$i]
$DeviceName = $array[3][$i]
}
$i ++
}
}
$EbsVolumeID = "FSxN Volume"
$BlockDevice = ""
$BlockDeviceName = ($BlockDeviceMappings | Where-Object { $_.ebs.VolumeId -eq $EbsVolumeID }).DeviceName
}
Else {
$BlockDeviceName = $null
$BlockDevice = $null
}
New-Object PSObject -Property @{
Disk = $Disk;
Partitions = $Partitions;
DriveLetter = If ($DriveLetter -eq $null) { "N/A" } Else { $DriveLetter };
EbsVolumeId = If ($EbsVolumeID -eq $null) { "N/A" } Else { $EbsVolumeID };
Device = If ($BlockDeviceName -eq $null) { "N/A" } Else { $BlockDeviceName };
VirtualDevice = If ($VirtualDevice -eq $null) { "N/A" } Else { $VirtualDevice };
VolumeName = If ($VolumeName -eq $null) { "N/A" } Else { $VolumeName };
DeviceName = If ($DeviceName -eq $null) { "N/A" } Else { $DeviceName };
}
} | Sort-Object Disk | Format-Table -AutoSize -Property Disk, Partitions, DriveLetter, EbsVolumeId, Device, VirtualDevice, DeviceName, VolumeName
PS C:\Users\Administrator\Documents> map.ps1
Could not access the AWS API, therefore, VolumeId is not available.
Verify that you provided your access keys or assigned an IAM role with adequate permissions.
Disk Partitions DriveLetter EbsVolumeId Device VirtualDevice DeviceName VolumeName
---- ---------- ----------- ----------- ------ ------------- ---------- ----------
0 1 C vol-0600381c3ab60ef40 N/A N/A NVMe Amazon Elastic B N/A
1 2 D vol-050191ed9ab11a70c N/A N/A NVMe Amazon Elastic B New Volume