May 11, 2011 at 1:14 pm
This may help you: http://poshcode.org/2276
July 22, 2011 at 9:02 am
Hi
I tried the following and seems to be working. You can tweak the code for your requirement , need to change the servername , databasename, tablename. I picked this code from "Loading Data With Powershell By Chad Miller, 2011/05/13"
from SQL SERVER CENTRAL.com
Thanks Chad....!!!!
********************************************************************
Function out-DataTable {
$dt = new-object Data.datatable
$First = $true
foreach ($item in $input){
$DR = $DT.NewRow()
$Item.PsObject.get_properties() | foreach {
if ($first) {
$Col = new-object Data.DataColumn
$Col.ColumnName = $_.Name.ToString()
$DT.Columns.Add($Col) }
if ($_.value -eq $null) {
$DR.Item($_.Name) = "[empty]"
}
elseif ($_.IsArray) {
$DR.Item($_.Name) =[string]::Join($_.value ,";")
}
else {
$DR.Item($_.Name) = $_.value
}
}
$DT.Rows.Add($DR)
$First = $false
}
return @(,($dt))
}
$dataTable = ls c:\temp -r *.bak | Sort -desc lastwriteTime | SELECT Directory,Name,LastWriteTime | out-DataTable
$connectionString = "Data Source=MyDBServerName;Integrated Security=true;Initial Catalog=MyDatabase;"
$bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString
$bulkCopy.DestinationTableName = "MyTable"
$bulkCopy.WriteToServer($dataTable)
*****************************************************************************************
July 22, 2011 at 9:03 am
Viewing 3 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply