Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell code Error while loading the powershell output to SQL server database

$
0
0

Hello All,

Below powershell code is not working. I need your help to find out the error where i am struggling. Also let me know how to store the powershell output to SQL server database table.

function Write-DataTable 

    [CmdletBinding()] 
    param( 
    [Parameter(Position=0, Mandatory=$true)] [string]$ServerInstance, 
    [Parameter(Position=1, Mandatory=$true)] [string]$Database, 
    [Parameter(Position=2, Mandatory=$true)] [string]$TableName, 
    [Parameter(Position=3, Mandatory=$true)] $Data,  
    [Parameter(Position=4, Mandatory=$false)] [string]$Username, 
    [Parameter(Position=5, Mandatory=$false)] [string]$Password, 
    [Parameter(Position=6, Mandatory=$false)] [Int32]$BatchSize=50000, 
    [Parameter(Position=7, Mandatory=$false)] [Int32]$QueryTimeout=0, 
    [Parameter(Position=8, Mandatory=$false)] [Int32]$ConnectionTimeout=15 
    ) 
     
    $conn=new-object System.Data.SqlClient.SQLConnection 
 
    if ($Username) 
    { $ConnectionString = "Server={0};Database={1};User ID={2};Password={3};Trusted_Connection=False;Connect Timeout={4}" -f $ServerInstance,$Database,$Username,$Password,$ConnectionTimeout } 
    else 
    { $ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2}" -f $ServerInstance,$Database,$ConnectionTimeout } 
    ##$ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2}" -f $ServerInstance,$Database,$ConnectionTimeout
    $conn.ConnectionString=$ConnectionString 
 
    try 
    { 
        $conn.Open() 
        $bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString 
        $bulkCopy.DestinationTableName = $tableName 
        $bulkCopy.BatchSize = $BatchSize 
        $bulkCopy.BulkCopyTimeout = $QueryTimeOut 
        $bulkCopy.WriteToServer($dt) 
        $conn.Close() 
    } 
    catch 
    { 
        $ex = $_.Exception 
        Write-Error "$ex.Message" 
        continue 
    } 
 }
foreach ($instance in get-content "C:\Backup\TEST.txt")
{
$instance
# Assumes V: does not already exists
##Clear-Content C:\Backup\A.txt

 $map = new-Object -com Wscript.Network
 $map.MapNetworkDrive("v:","$instance")
 $fso = new-Object -com Scripting.FileSystemObject
 $do = $fso.getdrive("v")
## $do.AvailableSpace / (1024*1024*1024)
## $do.TotalSize 
## $do | FT  ShareName,TotalSize,AvailableSpace -AutoSize >> C:\Backup\A.txt
$dt = $do | FT  ShareName,TotalSize,AvailableSpace -HideTableHeaders 

 Write-DataTable -ServerInstance NJDBASQLD01\SQL2K8R2 -Database PFGDBA -TableName ShareInfo -Data $dt
 $map.RemoveNetworkDrive("v:")
 }

Error Details: 

Write-DataTable : System.Management.Automation.MethodException: Cannot convert argument "rows", with value: "System.Object[]", for "WriteToServer" to type "System.Data.DataRow[]": "Cannot convert the "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" value of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" to type "System.Data.DataRow"." ---> System.Management.Automation.PSInvalidCastException: Cannot convert the "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" value of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" to type "System.Data.DataRow".   at System.Management.Automation.LanguagePrimitives.ThrowInvalidCastException(Object valueToConvert, Type resultType)   at System.Management.Automation.LanguagePrimitives.ConvertNoConversion(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable)

Table structure 

CREATE TABLE [dbo].[ShareInfo](
[ShareName] [varchar](100) NULL,
[TotalSize] [int] NULL,
[AvailSize] [int] NULL
) ON [PRIMARY]


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>