

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 搭配使用 `DescribeVolumes` 與 CLI
<a name="example_ec2_DescribeVolumes_section"></a>

下列程式碼範例示範如何使用 `DescribeVolumes`。

------
#### [ CLI ]

**AWS CLI**  
**範例 1：描述磁碟區**  
下列 `describe-volumes` 範例描述目前區域中的指定磁碟區。  

```
aws ec2 describe-volumes \
    --volume-ids vol-049df61146c4d7901 vol-1234567890abcdef0
```
輸出：  

```
{
    "Volumes": [
        {
            "AvailabilityZone": "us-east-1a",
            "Attachments": [
                {
                    "AttachTime": "2013-12-18T22:35:00.000Z",
                    "InstanceId": "i-1234567890abcdef0",
                    "VolumeId": "vol-049df61146c4d7901",
                    "State": "attached",
                    "DeleteOnTermination": true,
                    "Device": "/dev/sda1"
                }
            ],
            "Encrypted": true,
            "KmsKeyId": "arn:aws:kms:us-east-2a:123456789012:key/8c5b2c63-b9bc-45a3-a87a-5513eEXAMPLE,
            "VolumeType": "gp2",
            "VolumeId": "vol-049df61146c4d7901",
            "State": "in-use",
            "Iops": 100,
            "SnapshotId": "snap-1234567890abcdef0",
            "CreateTime": "2019-12-18T22:35:00.084Z",
            "Size": 8
        },
        {
            "AvailabilityZone": "us-east-1a",
            "Attachments": [],
            "Encrypted": false,
            "VolumeType": "gp2",
            "VolumeId": "vol-1234567890abcdef0",
            "State": "available",
            "Iops": 300,
            "SnapshotId": "",
            "CreateTime": "2020-02-27T00:02:41.791Z",
            "Size": 100
        }
    ]
}
```
**範例 2：描述連接至特定執行個體的磁碟區**  
下列 `describe-volumes` 範例描述連接至指定執行個體的所有磁碟區，並設定為在執行個體終止時刪除。  

```
aws ec2 describe-volumes \
    --region us-east-1 \
    --filters Name=attachment.instance-id,Values=i-1234567890abcdef0 Name=attachment.delete-on-termination,Values=true
```
如需 `describe-volumes` 的輸出範例，請參閱範例 1。  
**範例 3：描述特定可用區域中的可用磁碟區**  
下列 `describe-volumes` 範例描述狀態為 `available`，且位於指定可用區域中的所有磁碟區。  

```
aws ec2 describe-volumes \
    --filters Name=status,Values=available Name=availability-zone,Values=us-east-1a
```
如需 `describe-volumes` 的輸出範例，請參閱範例 1。  
**範例 4：根據標籤描述磁碟區**  
下列 `describe-volumes` 範例描述具有標籤索引鍵 `Name`，且開頭為 `Test` 的值的所有磁碟區。然後，系統會使用僅顯示磁碟區標籤和 ID 的查詢來篩選輸出。  

```
aws ec2 describe-volumes \
    --filters Name=tag:Name,Values=Test* \
    --query "Volumes[*].{ID:VolumeId,Tag:Tags}"
```
輸出：  

```
[
    {
       "Tag": [
           {
               "Value": "Test2",
               "Key": "Name"
           }
       ],
       "ID": "vol-1234567890abcdef0"
   },
   {
       "Tag": [
           {
               "Value": "Test1",
               "Key": "Name"
           }
       ],
       "ID": "vol-049df61146c4d7901"
    }
]
```
如需使用標籤篩選條件的其他範例，請參閱*《Amazon EC2 使用者指南》*中的[使用標籤](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#Using_Tags_CLI)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribeVolumes](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/describe-volumes.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：此範例描述指定的 EBS 磁碟區。**  

```
Get-EC2Volume -VolumeId vol-12345678
```
**輸出：**  

```
Attachments      : {}
AvailabilityZone : us-west-2c
CreateTime       : 7/17/2015 4:35:19 PM
Encrypted        : False
Iops             : 90
KmsKeyId         :
Size             : 30
SnapshotId       : snap-12345678
State            : in-use
Tags             : {}
VolumeId         : vol-12345678
VolumeType       : standard
```
**範例 2：此範例描述狀態為 'available' 的 EBS 磁碟區。**  

```
Get-EC2Volume -Filter @{ Name="status"; Values="available" }
```
**輸出：**  

```
Attachments      : {}
AvailabilityZone : us-west-2c
CreateTime       : 12/21/2015 2:31:29 PM
Encrypted        : False
Iops             : 60
KmsKeyId         :
Size             : 20
SnapshotId       : snap-12345678
State            : available
Tags             : {}
VolumeId         : vol-12345678
VolumeType       : gp2
...
```
**範例 3：此範例描述所有 EBS 磁碟區。**  

```
Get-EC2Volume
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DescribeVolumes](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：此範例描述指定的 EBS 磁碟區。**  

```
Get-EC2Volume -VolumeId vol-12345678
```
**輸出：**  

```
Attachments      : {}
AvailabilityZone : us-west-2c
CreateTime       : 7/17/2015 4:35:19 PM
Encrypted        : False
Iops             : 90
KmsKeyId         :
Size             : 30
SnapshotId       : snap-12345678
State            : in-use
Tags             : {}
VolumeId         : vol-12345678
VolumeType       : standard
```
**範例 2：此範例描述狀態為 'available' 的 EBS 磁碟區。**  

```
Get-EC2Volume -Filter @{ Name="status"; Values="available" }
```
**輸出：**  

```
Attachments      : {}
AvailabilityZone : us-west-2c
CreateTime       : 12/21/2015 2:31:29 PM
Encrypted        : False
Iops             : 60
KmsKeyId         :
Size             : 20
SnapshotId       : snap-12345678
State            : available
Tags             : {}
VolumeId         : vol-12345678
VolumeType       : gp2
...
```
**範例 3：此範例描述所有 EBS 磁碟區。**  

```
Get-EC2Volume
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [DescribeVolumes](https://docs.aws.amazon.com/powershell/v5/reference)。

------

如需 AWS SDK 開發人員指南和程式碼範例的完整清單，請參閱 [使用 SDK 建立 Amazon EC2 資源 AWS](sdk-general-information-section.md)。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。