BinaryDOM PowerShell Snapin

BinaryDOM PowerShell Snapin (PowerShell Provider) is an auxiliary component represented as a library (dll) file intended for use within Power Shell.

With this snapin, you may mount any valid file of supported format as a drive and enumerate, get, set, copy, move, remove, clear, get and set content of each individual element of document, and save the result.
Document will be represented as described in BDD schema, Structures and Document will work as folders, and Values and ValueArrays will work as files. It is possible to use BinaryDOM native relative paths (root.head.bfType) of filesystem-like relative paths (/head/bfType).

BinaryDOM PowerShell snapin provides additional way to automate work with binary documents when used in scripts.

PowerShell Snapin Installation and Usage


To install PowerShell snapin, you need to register it in the system. This is one-time action.
Set-Alias installutil $env:windir\Microsoft.NET\Framework\v2.0.50727\installutil
installutil C:\Program Files\Miraplacid\Miraplacid Binary DOM SDK 2.1\Bin\BinDomPSProvider.dll
This command will add snapin into PowerShell session:
Add-PSSnapin BinaryDOMPSProvider
This command will mount a file as a drive in the system:
New-PSDrive -name mybmp -psProvider BinaryDOM -root D:\mybmp.bmp
Now, it is possible to work with this file as a drive.

Here is and example of usage session:
Set-Location mybmp:
Get-Item /head/bfType
Copy-Item /head/bfReserved1 /bminfo
Remove-Item /head/bfReserved2
Get-Content /bits | Set-Content -Encoding Byte d:\temp\mybmp.bin
Remove-PSDrive mybmp

PowerShell Snapin Features and Behavior Details


BinaryDOM PowerShell Snapin has the following important features:
  • All the work with document takes place in "transaction". All changes made will not be saved until you will unmount the drive with Remove-PSDrive command. Snapin should ask you should it save the changes and save the modified file.
  • Since PowerShell is mostly text-oriented system, to get and set correct binary content, you need to use Get-Content and Get-Content cmdlets with -Encoding Byte parameter (see above).
  • Set-item and New-Item cmdlets are supported, but they expect a valid BinNode object as a parameter in this version, so, they may not be very useful.
  • Copy-Item cmdlet should be used with folder only name as a destination path; in result, destination folder will contain unnamed node copied from source node. Node will not have a name because metainformation will not be copied.
  • It is possible to get content from structure or document; it will contain all contents of contained nodes.
  • Set-Content cmdlet executed on structure will pass all data through BinReader, so, it will validate the data against a schema; if the data will be invalid, it will not be applied to the structure.
  • Clear-Item sets Values to 0 of appropriate type, removes all nodes from structures and ValueArrays.
  • In result of data manipulation we may get invalid document; BinaryDOM PowerShell Snapin will not revalidate it on save.
  • Add-Content cmdlet will work exactly as Set-Content.
  • If the file contains array of structures, all these structures will have the same name. To behave like a filesystem, BinaryDOM PowerShell Snapin will operate with the first element of such an array when you use its name. But, it still possible to address other structures if you will use path with (0-based) indices: /bits/image[2].
  • PowerShell is a very slow and inefficient system for large data volumes; we don't recommend to use BinaryDOM PowerShell Snapin with large files.

See also: