Amazon web services 如何在AWS EC2上的Windows中查找驱动器的EBS卷ID

Amazon web services 如何在AWS EC2上的Windows中查找驱动器的EBS卷ID,amazon-web-services,amazon-ec2,amazon-ebs,Amazon Web Services,Amazon Ec2,Amazon Ebs,我们有几个EC2实例,都有几个驱动器。当有多个驱动器同时存在时,在AWS控制台中监视哪个驱动器是正确的驱动器会变得很混乱 命名约定可以在一定程度上帮助实现这一点 但是,是否有办法在EBS磁盘的windows中获取卷id(或某些标识符),以便在AWS控制台中找到该卷?您可以使用该方法查询连接到给定实例的所有卷。下面是一个例子: # Specify your instance's ID $instance = "i-1234abcd" # Get a collection of all volum

我们有几个EC2实例,都有几个驱动器。当有多个驱动器同时存在时,在AWS控制台中监视哪个驱动器是正确的驱动器会变得很混乱

命名约定可以在一定程度上帮助实现这一点

但是,是否有办法在EBS磁盘的windows中获取卷id(或某些标识符),以便在AWS控制台中找到该卷?

您可以使用该方法查询连接到给定实例的所有卷。下面是一个例子:

# Specify your instance's ID
$instance = "i-1234abcd"

# Get a collection of all volumes attached to the instance
$volumes = @(get-ec2volume) | ? { $_.Attachments.InstanceId -eq $instance}

# Get a collection of each volume's ID property
$volumeNames = $volumes | % { $_.VolumeId}
这将调用cmdlet,并为连接到运行实例的每个卷返回一个对象。从那里,您可以提取如上所述的卷名

如果您是从EC2实例本身运行此操作,请尝试从EC2 web服务中提取实例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")

我使用下面的脚本。此脚本将迭代所有EBS卷,并根据其磁盘标签向EBS添加标记。 您可以根据需要对其进行修改(如有其他块,请参阅底部)。 您的EC2需要正确的EC2写入IAM角色

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
        }
    }