gpt4 book ai didi

powershell - YAML 中 CloudFormation 模板中的加入功能未按预期工作

转载 作者:行者123 更新时间:2023-12-03 07:35:47 30 4
gpt4 key购买 nike

作为我在 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]
}

最佳答案

不完全是您问题的答案,但我建议将 !Join 替换为 !Sub
大致思路是这样的:

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

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

关于powershell - YAML 中 CloudFormation 模板中的加入功能未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59068997/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com