

Doc AWS SDK Examples GitHub リポジトリには、他にも SDK の例があります。 [AWS](https://github.com/awsdocs/aws-doc-sdk-examples)

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# CLI で `StartSession` を使用する
<a name="ssm_example_ssm_StartSession_section"></a>

次のサンプルコードは、`StartSession` を使用する方法を説明しています。

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

**AWS CLI**  
**例 1: Session Manager のセッションを開始するには**  
この `start-session` の例では、Session Manager のセッションのためにインスタンスとの接続を確立します。このインタラクティブなコマンドでは、呼び出しを実行するクライアントマシンに Session Manager プラグインがインストールされている必要があることに留意してください。  

```
aws ssm start-session \
    --target "i-1234567890abcdef0"
```
出力:  

```
Starting session with SessionId: Jane-Roe-07a16060613c408b5
```
**例 2: SSH を使用して Session Manager のセッションを開始するには**  
この `start-session` の例では、SSH を使用して Session Manager のセッションのためにインスタンスとの接続を確立します。このインタラクティブなコマンドでは、呼び出しを実行するクライアントマシンに Session Manager プラグインがインストールされている必要があり、コマンドは Linux の EC2 インスタンスの `ec2-user` など、インスタンスのデフォルトユーザーを使用することに留意してください。  

```
ssh -i /path/my-key-pair.pem ec2-user@i-02573cafcfEXAMPLE
```
出力:  

```
Starting session with SessionId: ec2-user-07a16060613c408b5
```
詳細については、「Systems Manager ユーザーガイド」の[「セッションの開始](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-sessions-start.html)」および「CLI 用の Session Manager プラグインのインストール」を参照してください。 [AWS](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html) *AWS *  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[StartSession](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ssm/start-session.html)」を参照してください。

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

**Tools for PowerShell V4**  
**例 1: この例では、Session Manager のセッションのターゲットへの接続を開始し、ポート転送を有効にします。**  

```
Start-SSMSession -Target 'i-064578e5e7454488f' -DocumentName 'AWS-StartPortForwardingSession' -Parameter @{ localPortNumber = '8080'; portNumber = '80' }
```
**出力:**  

```
SessionId    StreamUrl
----------    ----------
random-id0    wss://ssmmessages.amazonaws.com/v1/data-channel/random-id
```
+  API の詳細については、*AWS Tools for PowerShell コマンドレットリファレンス (V4)* の「[StartSession](https://docs.aws.amazon.com/powershell/v4/reference)」を参照してください。

**Tools for PowerShell V5**  
**例 1: この例では、Session Manager のセッションのターゲットへの接続を開始し、ポート転送を有効にします。注: AWSCredentials を使用して設定していない場合は、Region パラメータを追加する必要があります。**  

```
Start-SSMSession -Target 'i-064578e5e7454488f' -DocumentName 'AWS-StartPortForwardingSession' -Parameter @{ localPortNumber = '8080'; portNumber = '80' } -Region 'us-west-1'
```
**出力:**  

```
Starting session with SessionId: testUser-xi4glew849asyeryde34u4dfsdfy
Port 8080 opened for sessionId testUser-xi4glew849asyeryde34u4dfsdfy.
Waiting for connections...
```
**例 2: この例では、Session Manager セッションに指定されたインスタンスを使用してインタラクティブセッションを作成します。**  

```
Start-SSMSession -Target 'i-1234567890abcdef0' -Region 'us-west-1'
```
**出力:**  

```
Starting session with SessionId : testUser-xi4glew849asyeryde34u4dfsdfy
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
                                                                                                                                                                                                
Install the latest PowerShell for new features and improvements!
                                                                                                                                                      
PS C:\Windows\system32> whoami
ec2amaz-fnsdrwv\ec2-test-user
PS C:\Windows\system32>
```
**例 3: この例では、セッションに接続せずにセッションを作成し、セッションへの接続に必要な SessionId、StreamUrl、TokenValue のプロパティを返します。**  

```
Start-SSMSession -Target 'i-1234567890abcdef0' -Region 'us-west-1' -DisablePluginInvocation
```
**出力:**  

```
SessionId        : testUser-xi4glew849asyeryde34u4dfsdfy
StreamUrl        : {StreamUrl value redacted}
TokenValue       : {Token value redacted}
ContentLength    : 1207
HttpStatusCode   : OK
```
+  API の詳細については、AWS Tools for PowerShell コマンドレットリファレンス (V5) の「[StartSession](https://docs.aws.amazon.com/powershell/v5/reference)」を参照してください。**

------