开发者

Import script-modified .reg files with PowerShell?

I need to import a .reg file that contains standard configuration settings for a server product. However, it's possible that a single Windows box might contain multiple server instances. Each server instance has its own configuration keys in the Windows registry.

The first instance will have the default registry key:

HKLM\SOFTWARE\<Vendor>\<Product>\Settings

Any other instances will have variant names, eg:

HKLM\SOFTWARE\<Vendor>\<Product>\Settings-foo
HKLM\SOFTWARE\<Vendor>\<Product>\Settings-bar
HKLM\SOFTWARE\<Vendor>\<Product>\Settings-baz

etc...

The registry file contains settings that need to be applied to each of these server instances. The structure of the registry sub-keys is the same within each key. So, the manual deployment process is to take the .reg file and do a file search-and-replace for "SOFTWARE\<Vendor>\<Product>\Settings\" and replace it with "SOFTWARE\<Vendor>\<Product>\Settings-foo\", then import the newly tweaked .reg file. Rinse & repeat for bar, baz etc.

What I want to do is write a PowerShell script that gets a list of all the "Settings-" keys and does the equivalent search-and-replace before importing the .reg file. I haven't found a cmdlet that can import .reg files, so I guess I have to call reg.exe or regedit.exe. Both of those programs allow you to import the contents of a .reg file that's on disk.

My question is, do I actually have to create .reg files and开发者_StackOverflow中文版 write them to disk so that I can then call reg.exe /Import <filename> or regedit.exe /S <filename>? Or is there some way I can just load the original .reg file, modify it in memory, and get reg.exe or regedit.exe to import the modified registry keys without needing to write a whole stack of .reg files to disk?


As far as I know, basic PowerShell Cmdlets don't use .reg file at all.

So, on one hand you can modify your reg files and apply them with REG.EXE. On the other hand you can use PowerShell registry provider an 'Item' CMdlets to modify your registry.


You could create reg commands in memory and execute those without resorting to temp files. That is, construct reg add commands like so,

reg add \\reg-path /v value-name /f /t type-name /d value-data

which will add or overwrite value value-name with value-data as data into registry path reg-path.

Creating such commands in a Powershell loop shouldn't be too hard.


So you can do it two ways:

$Reg = get-itemproperty hkcu:\environtment\path #This will get all the keys in a path
$Reg.key1 = [int32]1
$Reg.key2 = [string]somevalue
set-item hkcu:\environment\path $Reg

OR

$Reg = @"
Windows Registry Editor 5.00

[HKEY_CURRENT_USER\environment\path]
"key1"=dword:00000001
"key2"="somevalue"
"@

$reg | Out-file registry.reg
regedit /s registry.reg
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜