简体   繁体   English

Windows脚本:从Pipe / STDIN导入注册表?

[英]Windows Scripting: Registry Import from Pipe/STDIN?

Is it possible to import registry entries directly from the output of another process (ie over a pipe from STDIN)? 是否可以直接从另一个进程的输出中导入注册表项(即通过STDIN的管道)?

I have some registry entries in a .reg file that I'd like to import, but only after making some on-the-fly modifications (filling in placeholder values like username, etc.). 我想在.reg文件中导入一些注册表项,但是仅在进行一些即时修改(填写用户名等占位符值)之后才能导入。 I was planning on using the methods from How can you find and replace text in a file using the Windows command-line environment? 我正计划使用“ 如何使用Windows命令行环境查找和替换文件中的文本”中的方法? to do the modifications, but now I need to figure out how to get the modified version into the registry. 进行修改,但是现在我需要弄清楚如何将修改后的版本导入注册表。

(gc foo.reg) -creplace '^"UserName"=""$', "`"UserName`"=`"$env:USERNAME`"" | | ...

I've been looking at the documentation for REG IMPORT , which requires a filename as an arguments. 我一直在寻找REG IMPORT的文档,该文档需要文件名作为参数。 In Unix shell-scripting, which I have much more experience in, there are a lot of ways to solve the problem. 在我有很多经验的Unix shell脚本中,有很多方法可以解决该问题。 If we imagine a Unix version of the command reg -i which requires a filename, some possible solutions would be 如果我们想象命令reg -i的Unix版本需要文件名,则可能的解决方案是

sed 's/bar/baz/' foo.reg|reg -i

or 要么

sed 's/bar/baz/' foo.reg|reg -i -

or 要么

sed 's/bar/baz/' foo.reg|reg -i /dev/stdin

or even 甚至

reg -i <(sed 's/bar/baz/' foo.reg)

(Only the first two are at all natural.) (只有前两个是自然的。)

Is there an equivalent for Windows? Windows是否具有等效功能?

Note that I'm fine with solutions in either CMD- or PowerShell-style scripting, if that makes a difference. 请注意,如果这有所作为,我对CMD或PowerShell样式脚本中的解决方案都很好。

reg import expects a filename and not stdin AFAICT from its help. reg import需要一个文件名,而不是stdin AFAICT。 So I would read the original .reg file and modify the contents, save that to a new file and import that new file: 因此,我将读取原始的.reg文件并修改内容,将其保存到新文件中并导入该新文件:

(Get-Content foo.reg) -replace '^("UserName"\s*=\s*")"$', "`$1$env:UserName`"" > modfoo.reg
reg import modfoo.reg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM