修改Windows 2016 EC2实例右上角信息面板的技术求助
Great question! I’ve worked through this exact use case with Windows Server 2016 EC2 instances, and there are a couple of practical ways to pull instance tags into that RDP session info panel you’re referring to. Let’s break down the solutions step by step:
方法1:注入EC2标签到系统默认信息面板
Windows’ built-in system info panel (accessible via right-clicking "This PC" → Properties) doesn’t natively pull EC2 tags, but we can use the EC2 Instance Metadata Service (IMDS) to fetch tags and inject them into system properties—so they’ll show up in that panel automatically.
步骤1:编写PowerShell脚本获取EC2标签
First, create a script to pull your target tags via IMDS. Save this as UpdateEC2Tags.ps1 in a folder like C:\Scripts:
# Get IMDSv2 token (required for modern EC2 instances) $token = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token-ttl-seconds" = "21600"} -Method PUT -Uri http://169.254.169.254/latest/api/token # Fetch specific EC2 tags (replace with your tag keys, e.g., "Environment", "Project") $instanceId = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token" = $token} -Uri http://169.254.169.254/latest/meta-data/instance-id $environmentTag = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token" = $token} -Uri http://169.254.169.254/latest/meta-data/tags/instance/Environment $projectTag = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token" = $token} -Uri http://169.254.169.254/latest/meta-data/tags/instance/Project # Update the computer's description (shows up in System Properties) $currentDesc = Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" -Name "srvcomment" | Select-Object -ExpandProperty srvcomment $newDesc = "EC2 Instance: $instanceId | Env: $environmentTag | Project: $projectTag" Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" -Name "srvcomment" -Value $newDesc
步骤2:设置脚本开机自动运行
To keep the tags updated whenever the instance restarts:
- Open Task Scheduler → Create Basic Task
- Name it "Update EC2 Tags on Boot"
- Trigger: When the computer starts
- Action: Start a program → Path:
powershell.exe, Arguments:-ExecutionPolicy Bypass -File "C:\Scripts\UpdateEC2Tags.ps1" - Check "Run whether user is logged on or not" and "Run with highest privileges"
Now, when you RDP into the instance and open System Properties, you’ll see your EC2 tags listed in the computer description section.
方法2:创建自定义常驻信息面板
If you want a dedicated, always-visible panel in the RDP session’s corner, you can build a simple desktop widget or shortcut that displays EC2 tags on demand (or automatically on login).
步骤1:编写 a tag-display script
Save this as ShowEC2Tags.ps1:
# Fetch IMDS token and tags $token = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token-ttl-seconds" = "21600"} -Method PUT -Uri http://169.254.169.254/latest/api/token $instanceId = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token" = $token} -Uri http://169.254.169.254/latest/meta-data/instance-id $envTag = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token" = $token} -Uri http://169.254.169.254/latest/meta-data/tags/instance/Environment $ownerTag = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token" = $token} -Uri http://169.254.169.254/latest/meta-data/tags/instance/Owner # Display tags in a persistent window $form = New-Object System.Windows.Forms.Form $form.Text = "EC2 Instance Info" $form.Size = New-Object System.Drawing.Size(300,150) $form.StartPosition = "Manual" $form.Location = New-Object System.Drawing.Point([System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Width - 320, 10) # Top-right corner $form.TopMost = $true $label = New-Object System.Windows.Forms.Label $label.Text = "Instance ID: $instanceId`nEnvironment: $envTag`nOwner: $ownerTag" $label.Dock = "Fill" $label.Font = New-Object System.Drawing.Font("Arial",10) $form.Controls.Add($label) [void]$form.ShowDialog()
步骤2:Create a desktop shortcut
- Right-click the desktop → New → Shortcut
- Enter the target:
powershell.exe -ExecutionPolicy Bypass -File "C:\Scripts\ShowEC2Tags.ps1" - Rename it to "EC2 Instance Info" and drag it to the top-right corner of your desktop.
步骤3:(可选)Auto-run on RDP login
To make the panel open automatically when you log in:
- Press
Win + R→ typeshell:startup - Drag the shortcut into this folder.
Key Notes
- Ensure your EC2 instance has IMDS enabled (it’s on by default for new instances)
- If you’re using IMDSv1 (not recommended), you can skip the token step and call the metadata URL directly
- Adjust the tag keys in the scripts to match your actual EC2 tag names
内容的提问来源于stack exchange,提问作者user9683977

