【发布时间】: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