Powershell YAML中CloudFormation模板中的联接函数未按预期工作
作为我在AWS中使用的CloudFormation模板的一部分,我正在创建一个本地PS1文件以将服务器加入到域中Powershell YAML中CloudFormation模板中的联接函数未按预期工作,powershell,yaml,amazon-cloudformation,Powershell,Yaml,Amazon Cloudformation,作为我在AWS中使用的CloudFormation模板的一部分,我正在创建一个本地PS1文件以将服务器加入到域中 'c:\cfn\joindomain.ps1': content: !Join - '' - - $ServerNamePS = " - !Ref ServerName - | " - $UserPS = " - !Ref Domain - \ - !Ref DomainUse
'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]
}
不完全是对你问题的回答,但我建议替换
!用加入!Sub
大致如下:
content: !Sub
- |-
$ServerNamePS = "${ServerName}"
$UserPS = "${Domain}\${DomainUser}"
...
我们找到了!Sub
更容易处理该!加入
它可以工作!我将添加工作语法作为答案。谢谢你@lexicore