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

本文是小编为大家收集整理的关于PowerShell:如何使用标准输出代替文件名的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

推荐答案

如果您必须使用REG程序(而不是使用PowerShell查询/转储注册表 - 甚至在C#程序本身中进行此操作),那么您将要获得的最好的就是允许它要倾倒到临时文件,然后将文件的内容输送回标准,然后在您的C#程序中捕获它:

$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,则可以浏览注册表,就好像它是文件系统的一部分一样.也许这在其他一些方面有帮助?

cd HKCU:\Software\Microsoft\Windows
dir

其他推荐答案

只需使用'conout $'作为文件名(如在评论中播放的那样,这仅在Windows XP上起作用):

PS C:\> reg export HKLM\SOFTWARE\FileZilla 'CONOUT$'
 ■W i n d o w s   R e g i s t r y   E d i t o r   V e r s i o n   5 . 0 0

 [ H K E Y _ L O C A L _ M A C H I N E \ S O F T W A R E \ F i l e Z i l l a ]
 " I n s t a l l _ D i r " = " C : \ \ P r o g r a m   F i l e s \ \ F i l e Z i l l a "
 " R u n   i n   S e c u r e   M o d e " = " 0 "
 " U s e   R e g i s t r y " = " 0 "
 " L a n g u a g e " = " E n g l i s h "

此处显示的输出中有一些Unicode编码问题,但是分析时应该能够在缓冲区中处理.

本文地址:https://www.itbaoku.cn/post/2798758.html

问题描述

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?

推荐答案

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"

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

其他推荐答案

Just use 'CONOUT$' as the file name (as pojnted out in comments, this only works on Windows XP):

PS C:\> reg export HKLM\SOFTWARE\FileZilla 'CONOUT$'
 ■W i n d o w s   R e g i s t r y   E d i t o r   V e r s i o n   5 . 0 0

 [ H K E Y _ L O C A L _ M A C H I N E \ S O F T W A R E \ F i l e Z i l l a ]
 " I n s t a l l _ D i r " = " C : \ \ P r o g r a m   F i l e s \ \ F i l e Z i l l a "
 " R u n   i n   S e c u r e   M o d e " = " 0 "
 " U s e   R e g i s t r y " = " 0 "
 " L a n g u a g e " = " E n g l i s h "

There are some UNICODE encoding issues in the output shown here, but you should be able to handle that in the buffer when you parse it.