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

时间:2014-05-02 14:49:09

标签: amazon-web-services amazon-ec2 amazon-ebs

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

命名约定可以在一定程度上有所帮助。

但是有没有办法在EBS磁盘的窗口中获取卷ID(或某些标识符),以便可以在AWS控制台中找到它?

2 个答案:

答案 0 :(得分:1)

您可以使用AWS Tools for Powershell查询附加给定实例的所有卷。这是一个例子:

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

这将调用Get-EC2Volume cmdlet,并为连接到正在运行的实例的每个卷返回Amazon.EC2.Model.Volume个对象。从那里,您可以提取上面的卷名。

如果您是从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")

答案 1 :(得分:1)

我正在使用下面的脚本。此脚本将遍历所有EBS卷,并根据其磁盘标签向EBS添加标记。 您可以根据需要对其进行修改(请参阅底部if-else块)。 您的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
        }
    }