使用 PowerShell 导入脚本修改的 .reg 文件?
我需要导入包含服务器产品标准配置设置的 .reg 文件。但是,单个 Windows 机器可能包含多个服务器实例。每个服务器实例在 Windows 注册表中都有自己的配置密钥。
第一个实例将具有默认注册表项:
HKLM\SOFTWARE\<Vendor>\<Product>\Settings
任何其他实例都将具有变体名称,例如:
HKLM\SOFTWARE\<Vendor>\<Product>\Settings-foo
HKLM\SOFTWARE\<Vendor>\<Product>\Settings-bar
HKLM\SOFTWARE\<Vendor>\<Product>\Settings-baz
等...
注册表文件包含需要应用于每个服务器实例的设置。每个注册表项中的注册表子项的结构都是相同的。因此,手动部署过程是获取 .reg 文件并对 "SOFTWARE\
进行文件搜索和替换,并将其替换为“SOFTWARE\
,然后导入新调整的 .reg 文件。冲洗&重复 bar、baz 等。
我想要做的是编写一个 PowerShell 脚本,获取所有“Settings-”键的列表,并在导入 .reg 文件之前执行等效的搜索和替换。我还没有找到可以导入 .reg 文件的 cmdlet,所以我想我必须调用 reg.exe 或 regedit.exe。这两个程序都允许您导入磁盘上的 .reg 文件的内容。
我的问题是,我实际上是否必须创建 .reg 文件并将它们写入磁盘,以便我可以调用 reg.exe /Import
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,基本的 PowerShell Cmdlet 根本不使用 .reg 文件。
因此,一方面您可以修改 reg 文件并使用 REG.EXE 应用它们。另一方面,您可以使用 PowerShell 注册表提供程序和“Item”CMdlet 来修改注册表。
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.
您可以在内存中创建 reg 命令并执行这些命令,而无需求助于临时文件。也就是说,构造 reg add 命令,如下所示,
reg add \\reg-path /v value-name /f /t type-name /d value-data
它将添加或覆盖值 value-name将值数据作为数据放入注册表路径 reg-path 中。
在 Powershell 循环中创建此类命令应该不会太难。
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:
OR