John O
John O

Reputation: 5423

How can I write a binary Stream object to a file in PowerShell?

I think I've tried every wrong way, and those few that don't just give ugly error messages write a garbled file that cannot be opened (you can still see the JFIF in it, but the jpeg magic smoke has been lost).

The Stream itself is $contactInfo.Get_Item("Photo"). I think I need to do something like this:

$br = new-object System.IO.BinaryReader $contactInfo.Get_Item("Photo")

But past that, I don't know what to do. I've tried Googling, but I'm not even sure what I'm looking for to be quite honest.

The type of the Stream object is Microsoft.Lync.Model.UCStream.

Upvotes: 2

Views: 4894

Answers (1)

Keith Hill
Keith Hill

Reputation: 201652

I don't have access to this particular type (UCStream) but in general you would write this in PowerShell like so:

$br = new-object io.binaryreader $contactInfo.Get_Item("Photo")
$al = new-object collections.generic.list[byte]
while (($i = $br.Read()) != -1)
{
    $al.Add($i)
}

Set-Content photo.jpeg $al.ToArray() -enc byte

Upvotes: 3

Related Questions