如何使用VBS读取REG_DWORD注册表值并在BGinfo中展示?故障脚本问题排查
Let's break down the issues in your VBS script step by step, and fix it so it works reliably with BGinfo:
1. Incorrect Registry Path Format
- You included
HKEY_USERS\\in yourstrKeyPath, but since you're already using theHKEY_USERSconstant (&H80000003), the path should start directly from the SID. - The SID
S-1-5 20has a typo — it should beS-1-5-20(missing the hyphen between 5 and 20, which is the correct SID for the Local Service account). - Corrected path:
"S-1-5-20\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DeliveryOptimization\\Settings"
2. Variable Name Typo
You assigned the retrieved registry value to dwValue, but later tried to reference swValue (a misspelling). Since swValue is undefined, it evaluates to an empty string — that's why your message box only showed "t".
3. Unnecessary Variable Confusion
You first set DW="tct", then later overwrite the same variable (VBScript is case-insensitive) with "t "& swValue. This redundant assignment serves no purpose and creates confusion.
Here's a cleaned-up, error-handling version that properly retrieves the DownloadMode value:
Const HKEY_USERS = &H80000003 strComputer = "." Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\\root\\default:StdRegProv") strKeyPath = "S-1-5-20\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DeliveryOptimization\\Settings" strValueName = "DownloadMode" Dim dwValue ' Check if the value was retrieved successfully If oReg.GetDWORDValue(HKEY_USERS, strKeyPath, strValueName, dwValue) = 0 Then ' Output the value directly (BGinfo reads this output) WScript.Echo dwValue Else ' Handle cases where the key/value doesn't exist WScript.Echo "N/A" End If
To pull this value into your BGinfo display:
- Save the corrected script as
Get-DeliveryOptimizationMode.vbsin a secure, accessible location (e.g.,C:\Scripts\). - Open BGinfo, click Custom > New.
- In the custom field dialog:
- Set Identifier to something descriptive like
Delivery Optimization Mode. - Set Value to
WScript.exe //nologo "C:\Scripts\Get-DeliveryOptimizationMode.vbs"(replace the path with where you saved your script). - Click OK, then add this new field to your BGinfo template.
- Set Identifier to something descriptive like
This will automatically display the DownloadMode value (0, 1, or "N/A" if the setting is missing) in your BGinfo output.
内容的提问来源于stack exchange,提问作者Xavier Climent

