

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

# 使用 的偵測範例 AWS CLI
<a name="cli_detective_code_examples"></a>

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 Detective 執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `accept-invitation`
<a name="detective_AcceptInvitation_cli_topic"></a>

以下程式碼範例顯示如何使用 `accept-invitation`。

**AWS CLI**  
**接受在行為圖表中成為成員帳戶的邀請**  
下列 `accept-invitation` 範例接受邀請，因而在行為圖表 arn:aws:detective:us-east-1:111122223333:graph:123412341234 中成為成員帳戶。  

```
aws detective accept-invitation \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*Amazon Detective 管理指南*》中的[回應行為圖表邀請](https://docs.aws.amazon.com/detective/latest/adminguide/member-invitation-response.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [AcceptInvitation](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/accept-invitation.html)。

### `create-graph`
<a name="detective_CreateGraph_cli_topic"></a>

以下程式碼範例顯示如何使用 `create-graph`。

**AWS CLI**  
**啟用 Amazon Detective，並建立新的行為圖表**  
下列`create-graph`範例為在執行命令的 區域中執行命令 AWS 的帳戶啟用 Detective。建立新的行為圖表，並以該帳戶做為其管理員帳戶。該命令也會將 Finance 值指派給 Department 標籤。  

```
aws detective create-graph \
    --tags '{"Department": "Finance"}'
```
輸出：  

```
{
    "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:027c7c4610ea4aacaf0b883093cab899"
}
```
如需詳細資訊，請參閱《Amazon Detective 管理指南》**中的[啟用 Amazon Detective](https://docs.aws.amazon.com/detective/latest/adminguide/detective-enabling.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateGraph](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/create-graph.html)。

### `create-members`
<a name="detective_CreateMembers_cli_topic"></a>

以下程式碼範例顯示如何使用 `create-members`。

**AWS CLI**  
**邀請成員帳戶至行為圖表**  
下列`create-members`範例會邀請兩個 AWS 帳戶成為行為圖表 arn：aws：detective：us-east-1：111122223333：graph：123412341234 中的成員帳戶。對於每個帳戶，請求會提供 AWS 帳戶 ID 和帳戶根使用者電子郵件地址。請求包含要插入邀請電子郵件的自訂訊息。  

```
aws detective create-members \
    --accounts AccountId=444455556666,EmailAddress=mmajor@example.com AccountId=123456789012,EmailAddress=jstiles@example.com \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234 \
    --message "This is Paul Santos. I need to add your account to the data we use for security investigation in Amazon Detective. If you have any questions, contact me at psantos@example.com."
```
輸出：  

```
{
    "Members": [
    {
        "AccountId": "444455556666",
        "AdministratorId": "111122223333",
        "EmailAddress": "mmajor@example.com",
        "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
        "InvitedTime": 1579826107000,
        "MasterId": "111122223333",
        "Status": "INVITED",
        "UpdatedTime": 1579826107000
   },
   {
        "AccountId": "123456789012",
        "AdministratorId": "111122223333",
        "EmailAddress": "jstiles@example.com",
        "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
        "InvitedTime": 1579826107000,
        "MasterId": "111122223333",
        "Status": "VERIFICATION_IN_PROGRESS",
        "UpdatedTime": 1579826107000
     }
    ],
    "UnprocessedAccounts": [ ]
}
```
如需詳細資訊，請參閱《*Amazon Detective 管理指南*》中的「邀請會員帳至行為圖表<https://docs.aws.amazon.com/detective/latest/adminguide/graph-admin-add-member-accounts.html>」。  
**邀請成員帳戶而不傳送邀請電子郵件**  
下列`create-members`範例會邀請兩個 AWS 帳戶成為行為圖表 arn：aws：detective：us-east-1：111122223333：graph：123412341234 中的成員帳戶。對於每個帳戶，請求會提供 AWS 帳戶 ID 和帳戶根使用者電子郵件地址。成員帳戶不會收到邀請電子郵件。  

```
aws detective create-members \
    --accounts AccountId=444455556666,EmailAddress=mmajor@example.com AccountId=123456789012,EmailAddress=jstiles@example.com \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234 \
    --disable-email-notification
```
輸出：  

```
{
    "Members": [
    {
        "AccountId": "444455556666",
        "AdministratorId": "111122223333",
        "EmailAddress": "mmajor@example.com",
        "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
        "InvitedTime": 1579826107000,
        "MasterId": "111122223333",
        "Status": "INVITED",
        "UpdatedTime": 1579826107000
   },
   {
        "AccountId": "123456789012",
        "AdministratorId": "111122223333",
        "EmailAddress": "jstiles@example.com",
        "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
        "InvitedTime": 1579826107000,
        "MasterId": "111122223333",
        "Status": "VERIFICATION_IN_PROGRESS",
        "UpdatedTime": 1579826107000
     }
    ],
    "UnprocessedAccounts": [ ]
}
```
如需詳細資訊，請參閱《*Amazon Detective 管理指南*》中的「邀請會員帳至行為圖表<https://docs.aws.amazon.com/detective/latest/adminguide/graph-admin-add-member-accounts.html>」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateMembers](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/create-members.html)。

### `delete-graph`
<a name="detective_DeleteGraph_cli_topic"></a>

以下程式碼範例顯示如何使用 `delete-graph`。

**AWS CLI**  
**停用 Detective 並刪除行為圖表**  
下列 `delete-graph` 範例會停用 Detective，並刪除指定的行為圖表。  

```
aws detective delete-graph \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《Amazon Detective 管理指南》**中的[停用 Amazon Detective](https://docs.aws.amazon.com/detective/latest/adminguide/detective-disabling.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteGraph](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/delete-graph.html)。

### `delete-members`
<a name="detective_DeleteMembers_cli_topic"></a>

以下程式碼範例顯示如何使用 `delete-members`。

**AWS CLI**  
**將成員帳戶從行為圖表中移除**  
下列 `delete-members` 範例從行為圖表 arn:aws:detective:us-east-1:111122223333:graph:123412341234 中移除兩個成員帳戶。若要識別帳戶，請求會提供 AWS 帳戶 IDs。  

```
aws detective delete-members \
    --account-ids 444455556666 123456789012 \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
輸出：  

```
 {
    "AccountIds": [ "444455556666", "123456789012" ],
    "UnprocessedAccounts": [ ]
}
```
如需詳細資訊，請參閱《*Amazon Detective 管理指南*》中的「從行為圖表<https://docs.aws.amazon.com/detective/latest/adminguide/graph-admin-remove-member-accounts.html> 移除成員帳戶」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteMembers](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/delete-members.html)。

### `disassociate-membership`
<a name="detective_DisassociateMembership_cli_topic"></a>

以下程式碼範例顯示如何使用 `disassociate-membership`。

**AWS CLI**  
**從行為圖表中放棄成員資格**  
下列 disassociate-membership 範例會從行為圖表 arn：aws：detective：us-east-1：111122223333：graph：123412341234 中移除執行命令 AWS 的帳戶。  

```
aws detective disassociate-membership \
     --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
如需詳細資訊，請參閱《*Amazon Detective 管理指南*》中的「從行為圖表<https://docs.aws.amazon.com/detective/latest/adminguide/member-remove-self-from-graph.html> 中移除您的帳戶」。  
+  如需 API 詳細資訊，請參閱《*AWS CLI 命令參考*》中的 [DisassociateMembership](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/disassociate-membership.html)。

### `get-members`
<a name="detective_GetMembers_cli_topic"></a>

以下程式碼範例顯示如何使用 `get-members`。

**AWS CLI**  
**擷取所選行為圖表成員帳戶的相關資訊**  
下列 `get-members` 範例擷取行為圖表 arn:aws:detective:us-east-1:111122223333:graph:123412341234 中兩個成員帳戶的相關資訊。對於這兩個帳戶，請求會提供 AWS 帳戶 IDs。  

```
aws detective get-members \
    --account-ids 444455556666 123456789012 \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
輸出：  

```
{
    "MemberDetails": [
    {
        "AccountId": "444455556666",
        "AdministratorId": "111122223333",
        "EmailAddress": "mmajor@example.com",
        "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
        "InvitedTime": 1579826107000,
        "MasterId": "111122223333",
        "Status": "INVITED",
        "UpdatedTime": 1579826107000
    }
    {
        "AccountId": "123456789012",
        "AdministratorId": "111122223333",
        "EmailAddress": "jstiles@example.com",
        "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
        "InvitedTime": 1579826107000,
        "MasterId": "111122223333",
        "Status": "INVITED",
        "UpdatedTime": 1579826107000
    }
],
    "UnprocessedAccounts": [ ]
}
```
如需詳細資訊，請參閱《*Amazon Detective 管理指南*》中的「檢視行為圖表中的帳戶清單<https://docs.aws.amazon.com/detective/latest/adminguide/graph-admin-view-accounts.html>」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetMembers](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/get-members.html)。

### `list-graphs`
<a name="detective_ListGraphs_cli_topic"></a>

以下程式碼範例顯示如何使用 `list-graphs`。

**AWS CLI**  
**檢視您的帳戶所管理的行為圖表清單**  
下列 `list-graphs` 範例會擷取目前區域內呼叫帳戶做為管理員的行為圖表。  

```
aws detective list-graphs
```
輸出：  

```
{
    "GraphList": [
        {
            "Arn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
            "CreatedTime": 1579736111000
        }
    ]
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListGraphs](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/list-graphs.html)。

### `list-invitations`
<a name="detective_ListInvitations_cli_topic"></a>

以下程式碼範例顯示如何使用 `list-invitations`。

**AWS CLI**  
**檢視帳戶所屬或受邀加入的行為圖表清單**  
下列 `list-invitations` 範例會擷取呼叫帳戶已受邀請的行為圖表。結果僅包含開啟和接受的邀請。不包含遭拒的邀請或移除的成員資格。  

```
aws detective list-invitations
```
輸出：  

```
{
    "Invitations": [
    {
        "AccountId": "444455556666",
        "AdministratorId": "111122223333",
        "EmailAddress": "mmajor@example.com",
        "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
        "InvitedTime": 1579826107000,
        "MasterId": "111122223333",
        "Status": "INVITED",
        "UpdatedTime": 1579826107000
    }
]
}
```
如需詳細資訊，請參閱《*Amazon Detective 管理指南*》中的「檢視行為圖表邀請清單<https://docs.aws.amazon.com/detective/latest/adminguide/member-view-graph-invitations.html>」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListInvitations](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/list-invitations.html)。

### `list-members`
<a name="detective_ListMembers_cli_topic"></a>

以下程式碼範例顯示如何使用 `list-members`。

**AWS CLI**  
**列出行為圖表中的成員帳戶**  
下列 `list-members` 範例會擷取行為圖表 `arn:aws:detective:us-east-1:111122223333:graph:123412341234` 的受邀和啟用的成員帳戶。結果不包含已移除的成員帳戶。  

```
aws detective list-members \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
輸出：  

```
{
    "MemberDetails": [
        {
            "AccountId": "444455556666",
            "AdministratorId": "111122223333",
            "EmailAddress": "mmajor@example.com",
            "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
            "InvitedTime": 1579826107000,
            "MasterId": "111122223333",
            "Status": "INVITED",
            "UpdatedTime": 1579826107000
        },
        {
            "AccountId": "123456789012",
            "AdministratorId": "111122223333",
            "EmailAddress": "jstiles@example.com",
            "GraphArn": "arn:aws:detective:us-east-1:111122223333:graph:123412341234",
            "InvitedTime": 1579826107000,
            "MasterId": "111122223333",
            "PercentOfGraphUtilization": 2,
            "PercentOfGraphUtilizationUpdatedTime": 1586287843,
            "Status": "ENABLED",
            "UpdatedTime": 1579973711000,
            "VolumeUsageInBytes": 200,
            "VolumeUsageUpdatedTime": 1586287843
        }
    ]
}
```
如需詳細資訊，請參閱《*Amazon Detective 管理指南*》中的[檢視行為圖表中的帳戶清單](https://docs.aws.amazon.com/detective/latest/adminguide/graph-admin-view-accounts.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListMembers](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/list-members.html)。

### `list-tags-for-resource`
<a name="detective_ListTagsForResource_cli_topic"></a>

以下程式碼範例顯示如何使用 `list-tags-for-resource`。

**AWS CLI**  
**擷取指派給行為圖表的標籤**  
下列 `list-tags-for-resource` 範例會傳回指派給指定之行為圖表的標籤。  

```
aws detective list-tags-for-resource \
    --resource-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
輸出：  

```
{
    "Tags": {
        "Department" : "Finance"
    }
}
```
如需詳細資訊，請參閱《*Amazon Detective 管理指南*》中的[管理行為圖表的標籤](https://docs.aws.amazon.com/detective/latest/adminguide/graph-tags.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListTagsForResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/list-tags-for-resource.html)。

### `reject-invitation`
<a name="detective_RejectInvitation_cli_topic"></a>

以下程式碼範例顯示如何使用 `reject-invitation`。

**AWS CLI**  
**拒絕成為行為圖表中的成員帳戶的邀請**  
下列 `reject-invitation` 範例拒絕在行為圖表 arn:aws:detective:us-east-1:111122223333:graph:123412341234 中成為成員帳戶的邀請。  

```
aws detective reject-invitation \
    --graph-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*Amazon Detective 管理指南*》中的「回應行為圖表邀請<https://docs.aws.amazon.com/detective/latest/adminguide/member-invitation-response.html>」。  
+  如需 API 詳細資訊，請參閱《*AWS CLI 命令參考*》中的 [RejectInvitation](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/reject-invitation.html)。

### `tag-resource`
<a name="detective_TagResource_cli_topic"></a>

以下程式碼範例顯示如何使用 `tag-resource`。

**AWS CLI**  
**將標籤指派給資源**  
下列 `tag-resource` 範例將 Department 標籤的值指派給指定的行為圖表。  

```
aws detective tag-resource \
    --resource-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234 \
    --tags '{"Department":"Finance"}'
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*Amazon Detective 管理指南*》中的[管理行為圖表的標籤](https://docs.aws.amazon.com/detective/latest/adminguide/graph-tags.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [TagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/tag-resource.html)。

### `untag-resource`
<a name="detective_UntagResource_cli_topic"></a>

以下程式碼範例顯示如何使用 `untag-resource`。

**AWS CLI**  
**將標籤值從資源中移除**  
下列 `untag-resource` 範例從指定的行為圖表中移除 Department 標籤。  

```
aws detective untag-resource \
    --resource-arn arn:aws:detective:us-east-1:111122223333:graph:123412341234 \
    --tag-keys "Department"
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*Amazon Detective 管理指南*》中的[管理行為圖表的標籤](https://docs.aws.amazon.com/detective/latest/adminguide/graph-tags.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UntagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/detective/untag-resource.html)。