将参数调用到CloudFormation Powershell脚本中

问题描述

是否可以在用户数据内部运行的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","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

let slashN = [];
for(let slash of array){
   var reg = RegExp('\\n','g')
  slashN.push(slash.match(reg).length)
}

console.log(slashN);

这是一个Yaml示例:

{
  "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"
}