2013-02-09 131 views
1

的问题是在Visual Studio 2010.Net Framework 4.0使用C#一个PowerShell 3.0cmdlet如何在PowerShell cmdlet中使用Get-Content?

我们正在写从管道需要输入一个PowerShell命令。 Like ...

Get-Content .\somefile.bin -Encoding Byte | Receive-Bytes 

Receive-Bytes cmdlet需要以与Set-Content工作方式相同的方式工作。像...

Get-Content .\somefile.bin -Encoding Byte | Set-Content otherfile.bin -Encoding Byte 

我们曾尝试以下三个选项:

[System.Management.Automation.Parameter(Position = 1, Mandatory = true, ValueFromPipeline = true)] 
public byte Input { private get; set; } 
public byte[] Input { private get; set; } 
public System.IO.BinaryReader Input { private get; set; } 

但是......字节,字节[],BinaryReader在所有导致同样的错误。

+ Get-Content .\somefile.bin | Receive-Bytes 
+ ~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo : InvalidArgument: [Receive-Bytes], ParameterBindingException 
+ FullyQualifiedErrorId : 

Receive-Bytes : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. 

我们注意到错误重复了几次,表明Get-Content实际上发送了几个数据块。研究发现,情况是这样的(http://brianreiter.org/2010/01/29/powershells-object-pipeline-corrupts-piped-binary-data/),但这让我们不能接近解决它。

所以......现在的问题是:我们如何建立一个System.Management.Automation.Parameter接受获取内容发送的字节流?

更新:下面点答案使用上获取内容-Encoding字节,而内收到字节使用...

IConvertible获取数据,但我们得到的接收字节没有按与我们使用Get-Content选择的源文件不匹配。

所以......我们如何设置的参数更改IConvertible回字节流?

+0

你试过'Get-Content。\ somefile.bin -Encoding byte'吗? – zdan 2013-02-09 01:49:43

回答

0

看看this article

接近尾声,例如7似乎涵盖了你的问题。

+0

Get-Content。\ somefile.bin -Encoding字节|接收字节 接收字节:无法转换类型'System.Byte []'的对象以键入'System.IConvertible'。 + Get-Content。\ somefile.bin -Encoding Byte | Receive-Bytes + ~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [Receive-Bytes],InvalidCastException – 2013-02-09 00:38:31

相关问题