从 windows 2016 EC2 -Powershell 上的卷 ID 获取驱动器号
Getting Drive letter from volume id on windows 2016 EC2 -Powershell
是否有任何简单的方法来获取特定驱动器的 EBS 卷 ID letter/label?
我这样做只是提供卷 ID,但无法弄清楚如何获取驱动器号。
# Get Instance ID from the EC2 metadata web service
$instanceID = (New-Object System.Net.WebClient).DownloadString("http://169.254.169.254/latest/meta-data/instance-id")
# Get a collection of all volumes attached to the instance
$volumes = @(get-ec2volume) | ? { $_.Attachments.InstanceId -eq $instanceID}
# Get a collection of each volume's ID property
$volumeNames = $volumes | %{$_.attachment.device}
$volumeNames
实际上,我想用特定的 ec2 实例名称和驱动器号来标记 ebs 卷。
参见:Listing the Disks Using Windows PowerShell
您可以只使用代码的相关部分来只显示盘符。我没有测试它的设置。
Disk Partitions DriveLetter EbsVolumeId Device VirtualDevice VolumeName
---- ---------- ----------- ----------- ------ ------------- ----------
0 0 N/A N/A xvdca ephemeral0 N/A
1 0 N/A N/A xvdcb ephemeral1 N/A
2 1 C: vol-0064aexamplec838a /dev/sda1 root Windows
3 0 N/A vol-02256example8a4a3 xvdf ebs2 N/A
终于想通了。
该脚本将遍历所有 EBS 卷并根据其磁盘标签向 EBS 添加标签。
您可以根据需要修改它(请参阅底部的 if-else 块)。
EC2 需要具有适当的 IAM 角色才能调用 New-Ec2 标签 API.
Start-Transcript -Path C:\cfn\log\Tag-EBS-Volumes.ps1.txt -Append
# Create a hash table that maps each device to a SCSI target
$Map = @{"0" = '/dev/sda1'}
for($x = 1; $x -le 25; $x++) {$Map.add($x.ToString(), [String]::Format("/dev/xvd{0}",[char](97 + $x)))}
for($x = 78; $x -le 102; $x++) {$Map.add($x.ToString(), [String]::Format("/dev/xvdc{0}",[char](19 + $x)))}
Try {
# Use the metadata service to discover which instance the script is running on
$InstanceId = (Invoke-WebRequest '169.254.169.254/latest/meta-data/instance-id').Content
$AvailabilityZone = (Invoke-WebRequest '169.254.169.254/latest/meta-data/placement/availability-zone').Content
$Region = $AvailabilityZone.Substring(0, $AvailabilityZone.Length -1)
# Get the list of volumes attached to this instance
$BlockDeviceMappings = (Get-EC2Instance -Region $Region -Instance $InstanceId).Instances.BlockDeviceMappings
}
Catch
{
Write-Host "Could not access the AWS API, are your credentials loaded?" -ForegroundColor Yellow
}
Get-WmiObject -Class Win32_DiskDrive | %{
$Drive = $_
# Find the partitions for this drive
Get-WmiObject -Class Win32_DiskDriveToDiskPartition | Where-Object {$_.Antecedent -eq $Drive.Path.Path} | %{
$D2P = $_
# Get details about each partition
$Partition = Get-WmiObject -Class Win32_DiskPartition | Where-Object {$_.Path.Path -eq $D2P.Dependent}
# Find the drive that this partition is linked to
$Disk = Get-WmiObject -Class Win32_LogicalDiskToPartition | Where-Object {$_.Antecedent -in $D2P.Dependent} | %{
$L2P = $_
# Get the drive letter for this partition, if there is one
Get-WmiObject -Class Win32_LogicalDisk | Where-Object {$_.Path.Path -in $L2P.Dependent}
}
$BlockDeviceMapping = $BlockDeviceMappings | Where-Object { $_.DeviceName -eq $Map[$Drive.SCSITargetId.ToString()] }
If( $Disk.VolumeName -eq "") {
$tagvalue= "$env:COMPUTERNAME-Root"
} ElseIf ($Disk.VolumeName -eq "ABC" )
{
$tagvalue= "$env:COMPUTERNAME-ABC"
}ElseIf ($Disk.VolumeName -eq "DEF" )
{
$tagvalue= "$env:COMPUTERNAME-DEF"
}Else
{
$tagvalue= ""
}
New-EC2Tag -Resources $BlockDeviceMapping.Ebs.VolumeId -Tags @{ Key = "Name"; Value = $tagvalue } # Add volume name tag that matches VolumeId
}
}
为了快速查找,您可以 运行 PowerShell 命令 "Get-disk" 并使用序列号列中的值在 AWS 中的 EC2 仪表板的卷选项卡上进行查找。序列号以 "vol" 而非 "vol-" 开头,但如果您只是使用十六进制值来匹配卷 ID。
是否有任何简单的方法来获取特定驱动器的 EBS 卷 ID letter/label?
我这样做只是提供卷 ID,但无法弄清楚如何获取驱动器号。
# Get Instance ID from the EC2 metadata web service
$instanceID = (New-Object System.Net.WebClient).DownloadString("http://169.254.169.254/latest/meta-data/instance-id")
# Get a collection of all volumes attached to the instance
$volumes = @(get-ec2volume) | ? { $_.Attachments.InstanceId -eq $instanceID}
# Get a collection of each volume's ID property
$volumeNames = $volumes | %{$_.attachment.device}
$volumeNames
实际上,我想用特定的 ec2 实例名称和驱动器号来标记 ebs 卷。
参见:Listing the Disks Using Windows PowerShell
您可以只使用代码的相关部分来只显示盘符。我没有测试它的设置。
Disk Partitions DriveLetter EbsVolumeId Device VirtualDevice VolumeName
---- ---------- ----------- ----------- ------ ------------- ----------
0 0 N/A N/A xvdca ephemeral0 N/A
1 0 N/A N/A xvdcb ephemeral1 N/A
2 1 C: vol-0064aexamplec838a /dev/sda1 root Windows
3 0 N/A vol-02256example8a4a3 xvdf ebs2 N/A
终于想通了。 该脚本将遍历所有 EBS 卷并根据其磁盘标签向 EBS 添加标签。 您可以根据需要修改它(请参阅底部的 if-else 块)。 EC2 需要具有适当的 IAM 角色才能调用 New-Ec2 标签 API.
Start-Transcript -Path C:\cfn\log\Tag-EBS-Volumes.ps1.txt -Append
# Create a hash table that maps each device to a SCSI target
$Map = @{"0" = '/dev/sda1'}
for($x = 1; $x -le 25; $x++) {$Map.add($x.ToString(), [String]::Format("/dev/xvd{0}",[char](97 + $x)))}
for($x = 78; $x -le 102; $x++) {$Map.add($x.ToString(), [String]::Format("/dev/xvdc{0}",[char](19 + $x)))}
Try {
# Use the metadata service to discover which instance the script is running on
$InstanceId = (Invoke-WebRequest '169.254.169.254/latest/meta-data/instance-id').Content
$AvailabilityZone = (Invoke-WebRequest '169.254.169.254/latest/meta-data/placement/availability-zone').Content
$Region = $AvailabilityZone.Substring(0, $AvailabilityZone.Length -1)
# Get the list of volumes attached to this instance
$BlockDeviceMappings = (Get-EC2Instance -Region $Region -Instance $InstanceId).Instances.BlockDeviceMappings
}
Catch
{
Write-Host "Could not access the AWS API, are your credentials loaded?" -ForegroundColor Yellow
}
Get-WmiObject -Class Win32_DiskDrive | %{
$Drive = $_
# Find the partitions for this drive
Get-WmiObject -Class Win32_DiskDriveToDiskPartition | Where-Object {$_.Antecedent -eq $Drive.Path.Path} | %{
$D2P = $_
# Get details about each partition
$Partition = Get-WmiObject -Class Win32_DiskPartition | Where-Object {$_.Path.Path -eq $D2P.Dependent}
# Find the drive that this partition is linked to
$Disk = Get-WmiObject -Class Win32_LogicalDiskToPartition | Where-Object {$_.Antecedent -in $D2P.Dependent} | %{
$L2P = $_
# Get the drive letter for this partition, if there is one
Get-WmiObject -Class Win32_LogicalDisk | Where-Object {$_.Path.Path -in $L2P.Dependent}
}
$BlockDeviceMapping = $BlockDeviceMappings | Where-Object { $_.DeviceName -eq $Map[$Drive.SCSITargetId.ToString()] }
If( $Disk.VolumeName -eq "") {
$tagvalue= "$env:COMPUTERNAME-Root"
} ElseIf ($Disk.VolumeName -eq "ABC" )
{
$tagvalue= "$env:COMPUTERNAME-ABC"
}ElseIf ($Disk.VolumeName -eq "DEF" )
{
$tagvalue= "$env:COMPUTERNAME-DEF"
}Else
{
$tagvalue= ""
}
New-EC2Tag -Resources $BlockDeviceMapping.Ebs.VolumeId -Tags @{ Key = "Name"; Value = $tagvalue } # Add volume name tag that matches VolumeId
}
}
为了快速查找,您可以 运行 PowerShell 命令 "Get-disk" 并使用序列号列中的值在 AWS 中的 EC2 仪表板的卷选项卡上进行查找。序列号以 "vol" 而非 "vol-" 开头,但如果您只是使用十六进制值来匹配卷 ID。