2013-02-09 40 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

相關問題