作为我在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]
}
答案 0 :(得分:1)
并非完全是您的问题的答案,但建议您将!Join
替换为!Sub
。
大致情况:
content: !Sub
- |-
$ServerNamePS = "${ServerName}"
$UserPS = "${Domain}\${DomainUser}"
...
我们发现!Sub
更容易处理!Join
。