建立 SecureString 參數,並將節點加入網域 (PowerShell) - AWS Systems Manager

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

建立 SecureString 參數,並將節點加入網域 (PowerShell)

此演練示範如何使用 AWS Systems Manager SecureString 參數和Run Command,將 Windows Server 節點加入網域。此逐步解說使用典型的網域參數,例如網域名稱和網域使用者名稱。這些值會以未加密的字串值的形式進行傳遞。網域密碼是以 AWS 受管金鑰 加密,並以加密字串的形式傳遞。

先決條件

此演練假設您已經在與您的 Amazon VPC 關聯的 DHCP 選項集中指定了網域名稱和 DNS 伺服器 IP 地址。如需資訊,請參閱《Amazon VPC 使用者指南》中的搭配使用 DHCP 選項集

建立 SecureString 參數並將節點加入網域
  1. 使用 AWS Tools for Windows PowerShell 將參數輸入至系統。

    在下列命令中,將每個使用者輸入預留位置替換為您自己的資訊。

    Write-SSMParameter -Name "domainName" -Value "DOMAIN-NAME" -Type String Write-SSMParameter -Name "domainJoinUserName" -Value "DOMAIN\USERNAME" -Type String Write-SSMParameter -Name "domainJoinPassword" -Value "PASSWORD" -Type SecureString
    重要

    僅加密 SecureString 參數的。參數名稱、說明和其他屬性不會加密。

  2. 將下列 AWS Identity and Access Management (IAM) 政策連接到節點的 IAM 角色許可:

    • AmazonSSMManagedInstanceCore – 必要。此 AWS 受管政策允許受管節點使用 Systems Manager 服務的核心功能。

    • AmazonSSMDirectoryServiceAccess – 必要。此 AWS 受管政策可讓 SSM Agent 代表您存取 AWS Directory Service,以請求透過受管節點加入網域。

    • S3 儲存貯體存取的自訂政策 - 必要。SSM Agent (位於您的節點並執行 Systems Manager 任務) 需要存取 Amazon 擁有的特定 Amazon Simple Storage Service (Amazon S3) 儲存貯體。在您建立的自訂 S3 儲存貯體政策中,您也可以提供 Systems Manager 操作所需的自有 S3 儲存貯體存取權限。

      範例:您可以將 Run Command 命令或 Session Manager 工作階段的輸出寫入 S3 儲存貯體,然後稍後使用此輸出進行稽核或故障診斷。您將存取指令碼或自訂修補基準清單儲存在 S3 儲存貯體中,然後在執行命令時或套用修補基準時參考指令碼或清單。

      如需有關為 Amazon Simple Storage Service (Amazon S3) 儲存貯體存取建立自訂政策的相關資訊,請參閱為執行個體設定檔建立自訂 S3 儲存貯體政策

      注意

      您可以選擇是否在 S3 儲存貯體中儲存輸出日誌資料,但如果您已決定使用,則建議在 Systems Manager 組態程序開始時進行設定。如需詳細資訊,請參閱 Amazon Simple Storage Service 主控台使用者指南中的建立儲存貯體

    • CloudWatchAgentServerPolicy – 選用。此 AWS 受管政策可讓您在受管節點上執行 CloudWatch。此政策可讓您讀取節點的資訊,並將資訊寫入 Amazon CloudWatch。只有在使用諸如 Amazon EventBridge 或 CloudWatch Logs 等服務時,您的執行個體設定檔才需要此政策。

      注意

      您可以選擇是否使用 CloudWatch 和 EventBridge 功能,但如果您決定使用,則建議在 Systems Manager 組態程序開始時進行設定。如需詳細資訊,請參閱《Amazon EventBridge 使用者指南》和《Amazon CloudWatch Logs 使用者指南》。

  3. 編輯連接至節點的 IAM 角色,並新增以下政策。此政策可讓節點許可呼叫 kms:Decryptssm:CreateDocument API。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "kms:Decrypt", "ssm:CreateDocument" ], "Resource": [ "arn:aws:kms:region:account-id:key/kms-key-id" ] } ] }
  4. 將以下的 json 文字複製並黏貼到純文字編輯器,並在以下位置將檔案儲存為 JoinInstanceToDomain.jsonc:\temp\JoinInstanceToDomain.json

    { "schemaVersion": "2.2", "description": "Run a PowerShell script to securely join a Windows Server instance to a domain", "mainSteps": [ { "action": "aws:runPowerShellScript", "name": "runPowerShellWithSecureString", "precondition": { "StringEquals": [ "platformType", "Windows" ] }, "inputs": { "runCommand": [ "$domain = (Get-SSMParameterValue -Name domainName).Parameters[0].Value", "if ((gwmi Win32_ComputerSystem).domain -eq $domain){write-host \"Computer is part of $domain, exiting\"; exit 0}", "$username = (Get-SSMParameterValue -Name domainJoinUserName).Parameters[0].Value", "$password = (Get-SSMParameterValue -Name domainJoinPassword -WithDecryption $True).Parameters[0].Value | ConvertTo-SecureString -asPlainText -Force", "$credential = New-Object System.Management.Automation.PSCredential($username,$password)", "Add-Computer -DomainName $domain -Credential $credential -ErrorAction SilentlyContinue -ErrorVariable domainjoinerror", "if($?){Write-Host \"Instance joined to domain successfully. Restarting\"; exit 3010}else{Write-Host \"Instance failed to join domain with error:\" $domainjoinerror; exit 1 }" ] } } ] }
  5. 在 Tools for Windows PowerShell 中執行下列命令,以建立新的 SSM 文件。

    $json = Get-Content C:\temp\JoinInstanceToDomain | Out-String New-SSMDocument -Name JoinInstanceToDomain -Content $json -DocumentType Command
  6. 在 Tools for Windows PowerShell 中執行下列命令,將節點加入網域。

    Send-SSMCommand -InstanceId instance-id -DocumentName JoinInstanceToDomain

    如果命令成功,系統會傳回類似如下的資訊。

    WARNING: The changes will take effect after you restart the computer EC2ABCD-EXAMPLE.
    Domain join succeeded, restarting
    Computer is part of example.local, exiting

    如果命令失敗,系統會傳回類似如下的資訊:

    Failed to join domain with error:
    Computer 'EC2ABCD-EXAMPLE' failed to join domain 'example.local'
    from its current workgroup 'WORKGROUP' with following error message:
    The specified domain either does not exist or could not be contacted.