【问题标题】:Join function in CloudFormation template in YAML not working as expectedYAML 中 CloudFormation 模板中的加入功能未按预期工作
【发布时间】:2020-03-22 22:31:33
【问题描述】:

作为我在 AWS 中使用的 CloudFormation 模板的一部分,我正在创建一个本地 PS1 文件以将服务器加入域。

'c:\cfn\joindomain.ps1':
  content: !Join
    - ''
    - - $ServerNamePS = "
      - !Ref ServerName
      - |
        "
      - $UserPS = "
      - !Ref Domain
      - \
      - !Ref DomainUser
      - |
        "
      - $PassPS = ConvertTo-SecureString "
      - !Ref DomainPass
      - |
        " -AsPlainText -Force
      - |
      - $DomainCred = New-Object System.Management.Automation.PSCredential $UserPS, $PassPS
      - |
      - $DomainPS = "
      - !Ref Domain
      - |
        "
      - $DomainOU = "
      - !FindInMap 
        - OUTempComputer
        - !Ref 'AWS::Region'
        - !Ref NetbiosDomain
      - |
        "
      - |
        echo "Joining server $ServerNamePS to domain $DomainPS with user $UserPS"
      - |
        if (!(Get-ADComputer $ServerNamePS -Server (Get-ADDomainController -Discover -Service "GlobalCatalog" -DomainName $DomainPS).HostName[0] -Credential $DomainCred)) {Add-Computer -DomainName $DomainPS -Credential $DomainCred -ouPath $DomainOU -Restart -Force}

在创建的服务器上,我得到了这个 PowerShell 文件 c:\cfn\joindomain.ps1:

$ServerNamePS = "TST-SRV"
$UserPS = "locdom.com\ad.joiner"
$PassPS = ConvertTo-SecureString "123456" -AsPlainText -Force
$DomainCred = New-Object System.Management.Automation.PSCredential $UserPS, $PassPS$DomainPS = "locdom.com"
$DomainOU = "OU=AWS,DC=locdom,DC=com"
echo "Joining server $ServerNamePS to domain $DomainPS with user $UserPS"
if (!(Get-ADComputer $ServerNamePS -Server (Get-ADDomainController -Discover -Service "GlobalCatalog" -DomainName $DomainPS).HostName[0] -Credential $DomainCred)) {Add-Computer -DomainName $DomainPS -Credential $DomainCred -ouPath $DomainOU -Restart -Force}

如果您查看生成的 PS1 文件中的第 4 行,您会发现 PassPS 和 DomainPS 变量之间没有新行 ("$PassPS$DomainPS")。

如何确保这两个变量之间的给定 YAML 语法中有一个新行?显然我没有使用“|”正确。

非常感谢!

感谢@lexicore 的建议,这是更新后的工作解决方案:

    'c:\cfn\joindomain.ps1':
      content: !Sub
        - |
          $ServerNamePS = "${ServerName}"
          $UserPS = "${Domain}\${DomainUser}"
          $PassPS = ConvertTo-SecureString "${DomainPass}" -AsPlainText -Force
          $DomainCred = New-Object System.Management.Automation.PSCredential $UserPS, $PassPS
          $DomainPS = "${Domain}"
          $DomainOU = "${DomainOU}"
          echo "Joining server $ServerNamePS to domain $DomainPS with user $UserPS"
          $DomainGC = (Get-ADDomainController -Discover -Service "GlobalCatalog" -DomainName $DomainPS).HostName[0]
          try {Get-ADComputer $ServerNamePS -Server $DomainGC -Credential $DomainCred -ErrorAction Stop}
          catch {Add-Computer -DomainName $DomainPS -Credential $DomainCred -ouPath $DomainOU -Restart -Force}
        - {
          DomainOU: !FindInMap [OUTempComputer, !Ref "AWS::Region", !Ref NetbiosDomain]
          }

【问题讨论】:

    标签: powershell yaml amazon-cloudformation


    【解决方案1】:

    不完全是您问题的答案,但我建议将 !Join 替换为 !Sub。
    类似的东西:

    content: !Sub
      - |-
        $ServerNamePS = "${ServerName}"
        $UserPS = "${Domain}\${DomainUser}"
        ...
    

    我们发现!Sub 比!Join 更容易处理。

    【讨论】:

    • 有效!我将添加工作语法作为答案。谢谢@lexicore
    猜你喜欢
    • 2021-10-17
    • 2013-08-29
    • 2020-12-17
    • 2021-12-14
    • 1970-01-01
    • 2020-04-28
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多