Thanks a lot @brianlist, I tried your suggestion and it gives me this error when I use it with bulkcopy, not sure if the value is passed to $value .
Cannot convert argument "0", with value: "System.Object[]", for "WriteToServer" to type "System.Data.DataRow[]": "Cannot convert the "System.Data.DataRow" value of type "Deserialized.System.Data.DataRow" to type "System.Data.DataRow"." At C:\test\modified.ps1:11 char:24 + $bulkCopy.WriteToServer <<<< ($value) + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
Get-Content 'C:\test\computers.txt' | ? { $_.trim() -ne "" } | ForEach-Object { $value = Invoke-Command -Computer $_ -ScriptBlock { Param($computer) #.. $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $global:mdtable = New-Object System.Data.DataTable $SqlAdapter.SelectCommand = $SqlCmd $nRecs = $SqlAdapter.Fill($mdtable) $mdtable } -ArgumentList $_ -Credential $cred } | test Function test { $Database = 'Test1' $table = 'dbo.table_2' $connectionstring = "Data Source=mylocal;Integrated Security=True;Initial Catalog=$Database" $Conn = New-Object System.Data.SqlClient.SQLConnection($connectionstring) $conn.open() $bulkcopy = New-Object Data.SqlClient.SqlBulkCopy($conn) $bulkcopy.DestinationTableName = $table $bulkCopy.WriteToServer($value) $conn.close() }
source share