Last active
September 1, 2021 13:01
-
-
Save bretmartin/dc3b5e4a0adeca908489 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code appears in my blog post Renaming an EC2 Windows instance and customizing its wallpaper with tags using PowerShell.