Patch/Cmdlets/SQL/Export-BlobData.ps1

function Export-BlobData {
    param (
        # SQL Server Instance
        [parameter(Mandatory=$true)]
        $Server,
        # Database name
        [parameter(Mandatory=$true)]
        $Database,
        # Path to export to
        [parameter(Mandatory=$true)]
        $Destination
    )
    $ErrorActionPreference = "Stop"

    $bufferSize = 8192; # Stream buffer size in bytes.
    $Sql = "SELECT [license] FROM [`$ndo`$dbproperty]";

    # Open ADO.NET Connection
    $con = New-Object Data.SqlClient.SqlConnection;
    $con.ConnectionString = "Data Source=$Server;Integrated Security=True;Initial Catalog=$Database";
    $con.Open();

    # New Command and Reader
    $cmd = New-Object Data.SqlClient.SqlCommand $Sql, $con;
    $rd = $cmd.ExecuteReader();

    # Create a byte array for the stream.
    $out = [array]::CreateInstance('Byte', $bufferSize)

    # Looping through records
    While ($rd.Read()) {
        # New BinaryWriter
        $fs = New-Object System.IO.FileStream $Destination, Create, Write;
        $bw = New-Object System.IO.BinaryWriter $fs;

        $start = 0;
        # Read first byte stream
        $received = $rd.GetBytes(0, $start, $out, 0, $bufferSize - 1);
        While ($received -gt 0) {
            $bw.Write($out, 0, $received);
            $bw.Flush();
            $start += $received;
            # Read next byte stream
            $received = $rd.GetBytes(0, $start, $out, 0, $bufferSize - 1);
        }

        $bw.Close();
        $fs.Close();
    }

    # Closing & Disposing all objects
    $fs.Dispose();
    $rd.Close();
    $cmd.Dispose();
    $con.Close();
}

Export-ModuleMember -Function "Export-BlobData"