Guest User

Untitled

a guest
Oct 4th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. # Get Credentials from SSM
  2. echo $new_name
  3. $domain = "xxxx"
  4. $username = (Get-SSMParameterValue -Name ad_domain_user).Parameters[0].Value
  5. $password = (Get-SSMParameterValue -Name ad_domain_password -WithDecryption $True).Parameters[0].Value | ConvertTo-SecureString -asPlainText -Force
  6. $credential = New-Object System.Management.Automation.PSCredential($username,$password)
  7.  
  8. # Get VPC Tags for OU Name
  9. $vpc_id=(Invoke-RestMethod -uri http://169.254.169.254/latest/meta-data/network/interfaces/macs/$mac/vpc-id)
  10. $env_name=(aws ec2 describe-vpcs --vpc-ids $vpc_id --query 'Vpcs[*].[Tags[?Key==`Name`] | [0].Value]' --output text)
  11. echo "Attempting to join AD for env_name:$env_name" | Out-File "C:Program FilesAmazonEc2ConfigServiceLogspslog.txt" -Append
  12.  
  13.  
  14. # Join AD with newname
  15. Try {
  16. if ("$env_name" -match "xxxx*") {
  17. Add-Computer -DomainName $domain -ComputerName $env:computername -newname $new_name -Credential $credential -Force -Restart -ErrorAction 'Stop'
  18. } elseif ("$env_name" -match "xxxx-xxxx-*") {
  19. $oupath = "OU=Member Servers,OU=xxxx-xxxx,OU=xxxx,DC=aws,DC=ABC,DC=ORG"
  20. Add-Computer -DomainName $domain -ComputerName $env:computername -newname $new_name -Credential $credential -Force -Restart -ErrorAction 'Stop' -OUpath$oupath
  21. } elseif ("$env_name" -match "xxxx-xxxx-*") {
  22. $oupath = "OU=Member Servers,OU=xxxx-xxxx,OU=xxxx,DC=aws,DC=ABC,DC=ORG"
  23. Add-Computer -DomainName $domain -ComputerName $env:computername -newname $new_name -Credential $credential -Force -Restart -ErrorAction 'Stop' -OUpath $oupath
  24. }
  25. } Catch {
  26. echo $_.Exception | Out-File "C:Program FilesAmazonEc2ConfigServiceLogsError-JoinDomain.txt" -Append
  27. }
  28.  
  29. ## If Error File Exists try again
  30. Try{
  31. If (Test-Path "C:Program FilesAmazonEc2ConfigServiceLogsError-JoinDomain.txt" -PathType Leaf) {
  32. if ("$env_name" -match "xxxx*") {
  33. Add-Computer -DomainName $domain -ComputerName $env:computername -newname $new_name -Credential $credential -Force -Restart -ErrorAction 'Stop'
  34. } else {
  35. Add-Computer -DomainName $domain -ComputerName $env:computername -newname $new_name -Credential $credential -Force -Restart -ErrorAction 'Stop' -OUpath $oupath
  36. }
  37. }
  38. } Catch {
  39. echo $_.Exception | Out-File "C:Program FilesAmazonEc2ConfigServiceLogsError-JoinDomain.txt" -Append
  40. echo $_.Exception | Out-File "C:Program FilesAmazonEc2ConfigServiceLogspslog.txt" -Append
  41. }
Add Comment
Please, Sign In to add comment