From e9af37e49379801e4e3c03f7cc3c88214857f6cf Mon Sep 17 00:00:00 2001 From: Jeremy McGee Date: Thu, 31 Jan 2019 15:49:02 -0700 Subject: [PATCH 1/2] Changed C# type to PowerShell --- Invoke-SqlCmd2/Public/Invoke-SqlCmd2.ps1 | 55 ++++++------------------ 1 file changed, 13 insertions(+), 42 deletions(-) diff --git a/Invoke-SqlCmd2/Public/Invoke-SqlCmd2.ps1 b/Invoke-SqlCmd2/Public/Invoke-SqlCmd2.ps1 index d347e37..572a92e 100644 --- a/Invoke-SqlCmd2/Public/Invoke-SqlCmd2.ps1 +++ b/Invoke-SqlCmd2/Public/Invoke-SqlCmd2.ps1 @@ -361,50 +361,21 @@ function Invoke-Sqlcmd2 { if ($As -eq "PSObject") { #This code scrubs DBNulls. Props to Dave Wyatt - $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)); - } + function DataRowToPSObject ($row) + { + $PSObject = New-Object -TypeName psobject + + if($null -ne $row -and ($row.RowState -band [System.Data.DataRowState]::Detached) -ne [System.Data.DataRowState]::Detached){ + foreach ($column in $row.Table.Columns) { + $value = $null + if(!$row.IsNull($column)){ + $value = $row[$column] + $PSObject | Add-Member -MemberType NoteProperty -Name $column.ColumnName -Value $value } - - return psObject; } } -'@ - - 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" - } + + $PSObject } } @@ -627,7 +598,7 @@ function Invoke-Sqlcmd2 { #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 $ds.Tables[0].Rows) { - [DBNullScrubber]::DataRowToPSObject($row) + DataRowToPSObject($row) } } } From bba19408dfa79018f026790f93e83cb87ae02fd4 Mon Sep 17 00:00:00 2001 From: Jeremy McGee Date: Thu, 31 Jan 2019 15:52:36 -0700 Subject: [PATCH 2/2] Fixed not adding Empty Fields. --- Invoke-SqlCmd2/Public/Invoke-SqlCmd2.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Invoke-SqlCmd2/Public/Invoke-SqlCmd2.ps1 b/Invoke-SqlCmd2/Public/Invoke-SqlCmd2.ps1 index 572a92e..53f6b7d 100644 --- a/Invoke-SqlCmd2/Public/Invoke-SqlCmd2.ps1 +++ b/Invoke-SqlCmd2/Public/Invoke-SqlCmd2.ps1 @@ -370,11 +370,11 @@ function Invoke-Sqlcmd2 { $value = $null if(!$row.IsNull($column)){ $value = $row[$column] - $PSObject | Add-Member -MemberType NoteProperty -Name $column.ColumnName -Value $value } + + $PSObject | Add-Member -MemberType NoteProperty -Name $column.ColumnName -Value $value } } - $PSObject } }