Private/ConvertTo-PSObject.ps1
function ConvertTo-PSObject { param ( [Parameter( Mandatory, Position = 0, ValueFromPipeline = $true )] [System.Object] $InputObject ) begin { #$ErrorActionPreference = 'Stop' $cSharp = @' using System; using System.Data; using System.Management.Automation; public class DBNullScrubber { public static PSObject DataRowToPSObject(DataRow row) { PSObject psObject = new PSObject(); if (row != null && (row.RowState & DataRowState.Detached) != DataRowState.Detached) { foreach (DataColumn column in row.Table.Columns) { Object value = null; if (!row.IsNull(column)) { value = row[column]; } psObject.Properties.Add(new PSNoteProperty(column.ColumnName, value)); } } return psObject; } } '@ } process { <# try { if ($PSEdition -ne 'Core') { Add-Type -TypeDefinition $cSharp -ReferencedAssemblies 'System.Data', 'System.Xml' -ErrorAction stop } else { Add-Type $cSharp -ErrorAction stop } catch { if (-not $_.ToString() -like "*The type name 'DBNullScrubber' already exists*") { Write-Warning "Could not load DBNullScrubber. Defaulting to DataRow output: $_." $As = "Datarow" } } }#> if ($InputObject.Tables.Count -ne 0) { #Scrub DBNulls - Provides convenient results you can use comparisons with #Introduces overhead (e.g. ~2000 rows w/ ~80 columns went from .15 Seconds to .65 Seconds - depending on your data could be much more!) foreach ($row in $InputObject.Tables[0].Rows) { [DBNullScrubber]::DataRowToPSObject($row) } } } # Process } |