Skip to content

Instantly share code, notes, and snippets.

@bretmartin
Last active September 1, 2021 13:01
Show Gist options
  • Save bretmartin/dc3b5e4a0adeca908489 to your computer and use it in GitHub Desktop.
Save bretmartin/dc3b5e4a0adeca908489 to your computer and use it in GitHub Desktop.
$region = 'us-east-1'
$varprefix = 'YOUR_PREFIX_'
$wpsettings = 'C:\Program Files\Amazon\Ec2ConfigService\Settings\WallpaperSettings.xml'
$xmlns = 'http://tempuri.org/WallpaperSettings.xsd'
$instanceId = ( Invoke-WebRequest `
-Uri http://169.254.169.254/latest/meta-data/instance-id ).
Content
$tags = Get-EC2Tag -Region $region `
-Filter @( @{ name='resource-id'; values=$instanceId } )
$wpxml = [xml]( Get-Content $wpsettings )
foreach ( $tag in $( 'Environment', 'Function', 'VPC') ) {
$wpi = $wpxml.WallpaperSettings.AppendChild(
$wpxml.CreateElement( 'WallpaperInformation', $xmlns ) )
$elements = @{}
foreach ( $x in $( 'name', 'source', 'identifier' ) ) {
$elements.$x = $wpi.AppendChild( $wpxml.CreateElement( $x, $xmlns ) )
}
$name = $elements.name.AppendChild(
$wpxml.CreateTextNode( $tag ) )
$source = $elements.source.AppendChild(
$wpxml.CreateTextNode( 'environmentvariable' ) )
$identifier = $elements.identifier.AppendChild(
$wpxml.CreateTextNode( $varprefix
+ $tag.ToUpper() ) )
[Environment]::
SetEnvironmentVariable( $varprefix + $tag.ToUpper(),
( $tags | Where-Object { $_.Key -eq $tag } ).Value,
'Machine' )
}
$wpxml.Save( $wpsettings )
$name = ( Get-EC2Instance -region {{ region }} -instance $instanceId )[0].
RunningInstance[0].Tag |
Where-Object { $_.Key -eq "Name" } |
select -expandProperty Value
Rename-Computer -NewName $name
Restart-Computer
@bretmartin
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment