[英]Invoke Parameter into a CloudFormation Powershell script
是否可以將參數調用到在 userdata 中運行的 PowerShell 腳本中?
我正在嘗試分配密碼、ADuser 和域,以更改本地計算機名稱並將服務器加入域。
我可以在堆棧創建期間添加自我輸入,但我不知道如何使用 userdata 中的信息,有沒有可以使用的 Ref?
我可以使用 userdata 中的所有信息來做到這一點,但我不想用我們的域信息和憑據保存堆棧。
"Parameters" : {
"VMName":{
"Description":"Name of the EC2 Windows instance",
"Type":"String"
},
"DomainUser":{
"Description":"Name of Service/User domain account to be used to join the EC2 instance into CX domain",
"Type" : "String",
"MinLength" : "3",
"MaxLength" : "25",
"AllowedPattern" : "[a-zA-Z0-9]+\\..+"
},
"DomainCredential":{
"Description":"Password of the Service/User domain account to be used to join the EC2 instance into CX domain",
"Type" : "String",
"MinLength" : "8",
"MaxLength" : "32",
"AllowedPattern" : "(?=^.{6,255}$)((?=.*\\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*",
"NoEcho" : "True"
},
"Resources" : {
"EC2InstanceOne":{
"Type":"AWS::EC2::Instance",
"DeletionPolicy" : "Retain",
"Properties":{
"InstanceType":{ "Ref" : "InstanceType" },
"SubnetId": { "Ref" : "MySubnetVM1" },
"SecurityGroupIds":[ { "Ref" : "SGUtilized" } ],
"SecurityGroupIds":[ { "Ref" : "SGUtilized2" } ],
"IamInstanceProfile" : { "Ref" : "RoleName" },
"KeyName": { "Ref" : "ServerKeyName" },
"ImageId":{ "Ref" : "AMIUtilized" },
"BlockDeviceMappings" : [
{
"DeviceName" : "/dev/sda1",
"Ebs" : {
"VolumeType" : "standard",
"DeleteOnTermination" : "false",
"VolumeSize" : "50"
}
}
],
"UserData" : { "Fn::Base64" : { "Fn::Join" : [ "", [
"<script>\n",
"PowerShell -Command \"& {$password = 'variable from parameter here' | ConvertTo-SecureString -asPlainText -Force ; $username = 'variable from parameter here'' ; $credential = New-Object System.Management.Automation.PSCredential($username,$password) ; Rename-Computer -NewName 'variable from parameter here'' -DomainCredential $credential}\" \n",
"PowerShell -Command \"& {$domain='variable from parameter here';$password = 'variable from parameter here'' | ConvertTo-SecureString -asPlainText -Force ;$username = 'variable from parameter here'' ; $credential = New-Object System.Management.Automation.PSCredential($username,$password) ; Add-Computer -DomainName $domain -Credential $credential}\" \n",
"PowerShell -Command \"& {Restart-Computer}\" \n",
"</script>"
]
]
}
} }
謝謝,最好的問候。
您可以像這樣使用Fn::Sub :
{
"Fn::Sub":
"PowerShell -Command \"& {$domain=${VMName};$password = ${DomainCredential}' | ConvertTo-SecureString -asPlainText -Force ;$username = ${DomainUser}' ; $credential = New-Object System.Management.Automation.PSCredential($username,$password) ; Add-Computer -DomainName $domain -Credential $credential}\" \n"
}
這是 yaml 示例:
UserData:
Fn::Base64:
!Sub |
echo ${ParamPassword} | tee - | passwd ec2-user
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.