我如何添加到registry项

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

我需要脚本将string追加到现有的string值(Type = REG_SZ)。 该脚本不能用一个新的值replace整个string,因为我不知道当前正在input什么内容,我不能丢失已经存在的内容。

我正在考虑regini.exe的行,但我不知道如何导出,附加,导入regini.exe和一个batch file。 也许PowerShell可以做到这一点。

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 

Windows对于任何触及reg或registry.exe的东西都会感到</s> </s>不安,上面的脚本可以在抱怨较less的情况下运行。

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 

你可以使用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