编写PowerShell脚本提取Test注册表项的Version和Server值
Got it, here's a PowerShell script tailored to your needs, plus breakdowns of how each part works so you can tweak it if needed:
# Define the base registry path where your "Test" key lives $baseRegPath = "HKLM:\Software\Test" # First, make sure the base path exists to avoid errors if (-not (Test-Path -Path $baseRegPath)) { Write-Error "Oops, the registry path $baseRegPath doesn't exist. Double-check the location!" exit 1 } # Grab all direct subkeys under the "Test" key $subkeys = Get-ChildItem -Path $baseRegPath # Loop through each subkey to extract your required values foreach ($subkey in $subkeys) { # Pull the "Version" string value from the current subkey $version = Get-ItemProperty -Path $subkey.PSPath -Name "Version" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Version # Build the path to the "Setting" subkey and get the "Server" value $settingSubPath = Join-Path -Path $subkey.PSPath -ChildPath "Setting" $serverValue = if (Test-Path -Path $settingSubPath) { Get-ItemProperty -Path $settingSubPath -Name "Server" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Server } else { "Not found" } # Format and print the result exactly as you requested $versionDisplay = $version ?? "Not found" $serverDisplay = $serverValue ?? "Not found" Write-Host "$($subkey.PSChildName) Version: $versionDisplay Server: $serverDisplay" }
Quick Notes & Customization Tips
- Update the Base Path: Change
$baseRegPathto match your actual registry location (e.g.,HKCU:\Software\Testif it's under the current user's hive instead of the local machine). - Handling Missing Values: The script uses
-ErrorAction SilentlyContinueand null-coalescing (??) to gracefully handle cases where a subkey lacks the "Version" value, or the "Setting" subkey doesn't exist. It'll just show "Not found" for missing values instead of crashing. - Output Options: If you want to save the results to a file instead of just printing to the console, replace
Write-HostwithAdd-Content -Path "C:\your-output-file.txt"to append each line to a text file.
内容的提问来源于stack exchange,提问作者SebastianS
相关产品推荐
相关产品推荐

