Skip to content

Instantly share code, notes, and snippets.

@stknohg
Created June 25, 2024 08:38
Show Gist options
  • Save stknohg/28c20cab66319ef8ad3d7f923b5c5517 to your computer and use it in GitHub Desktop.
Save stknohg/28c20cab66319ef8ad3d7f923b5c5517 to your computer and use it in GitHub Desktop.
Windows EC2インスタンス内のドライブレターとEBSボリュームIDの紐づきを表示するコマンド
function Get-InstanceDrives () {
foreach ($disk in Get-Disk) {
# Get EBS volume ID
# * ref : https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-windows-volumes.html
$SerialNumber = $disk.SerialNumber
if ($SerialNumber -clike 'vol*') {
$EbsVolumeId = $SerialNumber.Substring(0, 20).Replace("vol", "vol-")
} else {
$EbsVolumeId = $SerialNumber.Substring(0, 20).Replace("AWS", "AWS-")
}
# Get partitions with Drive letter
foreach ($partion in (Get-Partition -DiskNumber $disk.Number -ErrorAction SilentlyContinue | Where-Object { $_.DriveLetter })) {
# Get volume
$volume = Get-Volume -DriveLetter $partion.DriveLetter
# Output results
[PSCustomObject]@{
DiskNumber = $disk.Number
EBSVolumeId = $EbsVolumeId
FriendlyName = $disk.FriendlyName
PartitionNumber = $partion.PartitionNumber
DirveLetter = $partion.DriveLetter
FileSystem = $volume.FileSystem
FileSystemLabel = $volume.FileSystemLabel
Size = $volume.Size
SizeRemaining = $volume.SizeRemaining
}
}
}
}
@stknohg
Copy link
Author

stknohg commented Jun 25, 2024

実行例

単一EBSボリュームのWindows Server 2022 EC2インスタンスにSSM Session Managerで接続してコマンドを実行した場合。

PS C:\Windows\system32> Get-InstanceDrives | Format-Table

DiskNumber EBSVolumeId           FriendlyName          PartitionNumber DirveLetter FileSystem FileSystemLabel        Size SizeRemaining
---------- -----------           ------------          --------------- ----------- ---------- ---------------        ---- -------------
         0 vol-0xxxxxxxxxxxxxxxx NVMe Amazon Elastic B               1           C NTFS                       32210153472   14828048384

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment