2012-12-17 80 views
1

I將reg文件轉換爲VBS命令。在VBS上使用變量

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run] 
@="" 
"VPService"="C:\\Windows\\System32\\VPService.exe" 

,但我不能使用的%systemroot%變量,而不是C:\ WINDOWS \這一點。

Option Explicit 
Dim objShell 
Set objShell = CreateObject("WScript.Shell") 

Dim strComputer, ArrOfValue, oReg 
const HKEY_USERS = &H80000003 
const HKEY_LOCAL_MACHINE = &H80000002 
const HKEY_CURRENT_USER = &H80000001 
const HKEY_CLASSES_ROOT = &H80000000 
strComputer = "." 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") 
objShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Run\", "" 
objShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Run\", "", "REG_SZ" 'Default value 
objShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Run\VPService", "C:\\Windows\\System32\\VPService.exe", "REG_SZ" 
Set objShell = Nothing 
WScript.Quit 

如何在這段代碼上使用%systemroot%變量代替C:\ Windows \?

回答

0

WScript.Shell可以擴大環境變量:

>> WScript.Echo CreateObject("WScript.Shell").ExpandEnvironmentStrings("%systemroot%") 
>> 
C:\WINDOWS 

Docs

0

使得該值爲expandable string

objShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Run\VPService" _ 
    , "%SystemRoot%\\System32\\VPService.exe", "REG_EXPAND_SZ"