PowerShell:如何使用标准输出代替文件名 [英] PowerShell: How To Use Standard Output In Place of Filename

查看:60
本文介绍了PowerShell:如何使用标准输出代替文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 C# 类,它运行一个进程 (REG) 来导出注册表项.REG 要求您指定要导出到的文件名,但我宁愿将 REG 的输出定向到标准输出,以便我可以直接在我的 C# 代码中捕获它(使用 Process.StandardOutput).PowerShell 中有没有办法将标准输出指定为文件名?

I'm writing a C# class that runs a Process (REG) to export registry keys. REG requires that you specify a filename to export to but I would rather have the output of REG directed to the standard output so I can capture it directly in my C# code (using Process.StandardOutput). Is there a way in PowerShell to specify the standard output as the filename?

推荐答案

如果您必须使用 REG 程序(而不是使用 PowerShell 查询/转储注册表 - 甚至只是在 C# 程序本身中执行),可能最好的办法是允许它转储到一个临时文件,然后将文件的内容通过管道传输回标准输出,并以这种方式在 C# 程序中捕获它:

If you have to use the REG program (rather than use PowerShell to query/dump the registry - or even just do it in the C# program itself), Probably the best you are going to get is to allow it to dump out to a temporary file, then pipe the contents of the file back to standard out and capture it in your C# program that way:

$guid = [Guid]::NewGuid().ToString("N")
REG EXPORT HKCU\Software\Microsoft\Windows "$env:temp\$guid" | Out-Null
Get-Content "$env:temp\$guid"
Remove-Item "$env:temp\$guid"

如果您不知道:使用 PowerShell,您可以导航注册表,就像它是文件系统的一部分一样.也许这在其他方面有帮助?

In case you were not aware: Using PowerShell, you can navigate the registry as though it were part of the file system. Perhaps this is helpful in some other regard?

cd HKCU:\Software\Microsoft\Windows
dir

这篇关于PowerShell:如何使用标准输出代替文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆