-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathImport-CsvToSqlTable.ps1
75 lines (65 loc) · 3.13 KB
/
Import-CsvToSqlTable.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function Import-CsvToSqTable {
[CmdletBinding()]
param([string]$InstanceName
,[string]$Database
,[string]$SourceFile
,[string]$SqlDataType = 'VARCHAR(255)'
,[pscredential]$SqlCred
,[string]$StagingTableName
,[Switch]$Append
)
#Check file existince. Should be a perfmon csv
if(-not (Test-Path $SourceFile) -and $SourceFile -notlike '*.csv'){
Write-Error "Invalid file: $SourceFile"
}
$source = Get-ChildItem $SourceFile
#Cleanup input file (Quoted Identifiers)
Write-Verbose "[Clean Inputs]"
(Get-Content $source).Replace('"','') | Set-Content $source
#Get csv header row, create staging table for load, remove first item 'cause it's junk
$Header = (Get-Content $source | Select-Object -First 1).Split(',')
$CleanHeader = @()
#Cleanup header names to be used column names
#Remove non-alphanumeric characters
foreach($h in $Header){
$CleanValue = $h -Replace '[^a-zA-Z0-9_]',''
$CleanHeader += $CleanValue
Write-Verbose "[Cleaned Header] $h -> $CleanValue"
}
#Build create table statement if target table does not exist
if(-not $Append){
$sql = @("IF EXISTS (SELECT 1 FROM sys.tables WHERE name = '$StagingTableName') DROP TABLE [$StagingTableName];")
} else {
$sql = @("IF NOT EXISTS (SELECT 1 FROM sys.tables WHERE name = '$StagingTableName')")
}
$sql += ("CREATE TABLE [$StagingTableName]($($CleanHeader[0]) $SqlDataType `n")
$CleanHeader[1..$CleanHeader.Length] | ForEach-Object {$sql += ",$_ $SqlDataType `n"}
$sql += ");"
$sql = $sql -join "`n"
Write-Verbose "[CREATE TABLE Statement] $sql"
try{
if($SqlCred){
Invoke-Sqlcmd -ServerInstance $InstanceName -Database $Database -Query $sql -Username $SqlCred.UserName -Password $SqlCred.GetNetworkCredential().Password
$cmd = "bcp 'dbo.$StagingTableName' in '$SourceFile' -S'$InstanceName' -d'$Database' -F2 -c -t',' -U'$($SqlCred.UserName)' -P'$($SqlCred.GetNetworkCredential().Password)'"
} else {
Invoke-Sqlcmd -ServerInstance $InstanceName -Database $Database -Query $sql
$cmd = "bcp 'dbo.$StagingTableName' in '$SourceFile' -S'$InstanceName' -d'$Database' -F2 -c -t',' -T"
}
Write-Verbose "[BCP Command] $cmd"
$cmdout = Invoke-Expression $cmd
if($cmdout -join '' -like '*error*'){
throw $cmdout
}
Write-Verbose "[BCP Results] $cmdout"
if($SqlCred){
$rowcount = Invoke-Sqlcmd -ServerInstance $InstanceName -Database $Database -Query "SELECT COUNT(1) [RowCount] FROM [$StagingTableName];" -Username $SqlCred.UserName -Password $SqlCred.GetNetworkCredential().Password
} else {
$rowcount = Invoke-Sqlcmd -ServerInstance $InstanceName -Database $Database -Query "SELECT COUNT(1) [RowCount] FROM [$StagingTableName];"
}
$output = New-Object PSObject -Property @{'Instance'=$InstanceName;'Database'=$Database;'Table'="$StagingTableName";'RowCount'=$rowcount.RowCount}
return $output
}
catch{
Write-Error $Error[0] -ErrorAction Stop
}
}