从Windows 2016 EC2 -Powershell上的卷ID获取驱动器号

问题描述 投票:0回答:3

是获取特定驱动器号/标签的EBS卷ID的简单方法吗?

我这样做只是提供卷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卷:和驱动器号。

powershell amazon-web-services amazon-ec2 amazon-ebs
3个回答
0
投票

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

0
投票

终于想通了。此脚本将遍历所有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
        }
    }

-1
投票

只需快速查找,您就可以运行PowerShell命令“Get-disk”并使用序列号列中的值在AWS中的EC2仪表板的Volumes选项卡上查找。序列号以“vol”而不是“vol-”开头,但如果您只是使用十六进制值来匹配卷ID。

© www.soinside.com 2019 - 2024. All rights reserved.