如何添加注册表项

如何添加注册表项

我需要创建一个脚本来在我无法访问的服务器上运行。

我需要脚本将字符串附加到现有字符串值 (Type = REG_SZ)。脚本不能直接用新值替换整个字符串,因为我不知道条目中当前的内容,也不能丢失已经存在的内容。

我考虑过使用 regini.exe,但不知道如何使用 regini.exe 和批处理文件导出、附加和导入。也许 powershell 可以解决这个问题。

答案1

Powershell 在这里是一个可靠的选择。

$AppendValue="\Homes"
$RegRoot=Get-ItemProperty "hklm:\software\microsoft\windows\currentversion"
$RegValue=$RegRoot.CommonFilesDir+$AppendValue
Set-ItemProperty -path HKLM:\software\microsoft\windows\Currentversion -Name CommonFilesDir -Value $RegValue

任何涉及 reg 或 registry.exe 的东西,Windows 都会变得很敏感,而上述脚本则可以更无忧地运行。

答案2

Vbscript 也会这样做

Set WshShell = WScript.CreateObject("WScript.Shell")
Dim Temp
'For the purpose of demonstration create a new key and give it a default of 1
WshShell.RegWrite "HKCU\MyNewKey\", 1 ,"REG_SZ"
'Add a value 
WshShell.RegWrite "HKCU\MyNewKey\MyValue", "Hello world!"
'read the value we just wrote append more text to it and write it back
Temp = WshShell.RegRead("HKCU\MyNewKey\MyValue")
Temp = Temp & " More Text"
WshShell.RegWrite "HKCU\MyNewKey\MyValue",Temp

答案3

您可以使用 PowerShell 或 Windows 的 REG.EXE 实现此目的,例如:

@echo off  
setlocal

set SERVER=myserver  
set KEY=HKLM\Software\Microsoft\Windows\CurrentVersion\Run  
set VALUE=myvalue  
set APPEND_DATA=my appended text

REM *** GET THE EXISTING VALUE  
for /f "tokens=2,*" %%V in ('%SystemRoot%\System32\reg.exe query "\\%SERVER%\%KEY%" /v "%VALUE%"') do set DATA=%%W

REM *** SET THE VALUE  
set DATA=%DATA:"=\"%  
%SystemRoot%\System32\reg.exe add "\\%SERVER%\%KEY%" /v "%VALUE%" /t REG_SZ /f /d "%DATA%%APPEND_DATA%"  

endlocal

相关内容