From e5751240d054779c6c2fdb84d3cc4dfcde2846c4 Mon Sep 17 00:00:00 2001 From: Floris van Enter Date: Tue, 28 Nov 2017 15:06:49 +0100 Subject: [PATCH] First release with download-CsvToSQL --- .DS_Store | Bin 0 -> 8196 bytes PowerShell/.DS_Store | Bin 0 -> 8196 bytes PowerShell/MSSQLServer/download-CsvToSQL.ps1 | 81 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 .DS_Store create mode 100644 PowerShell/.DS_Store create mode 100755 PowerShell/MSSQLServer/download-CsvToSQL.ps1 diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..6a3f18e1bcfbf6f1913d267cbb5c083b6594c15e GIT binary patch literal 8196 zcmeI0OKjX!6o${4q^)n#G)_nxx5=~vNgyp#$V}R#Gz2EeBx%K?p_5mWLgE>_iG|0W zjy*GNL#pstQl2Xc8wBFj1p03gxEtz1prI*t4Jc1;WUGu(-k50Hlx%!3Xl{el)K5GW8R z5GW8R5Ge3pRDizOyh&xg`@$RafdYX7H>Cpn{Scvz+k|hIg!Nt>)OjTU*-D!B8jYzA z@Ny!)P55?6STB{1DXItbQ_(LmP^6PT!o$fXe7hv9NCy<@fc}}$uTaoCJLN^ZaKMDH zK_4g(C~&O;{IRKm5wM{|b^EINJza8gY12-V|BH0aG#t01;}V7{DmQH0BuQ0LRZZ2S zsd>AUa#gqB^sD)4zBi(}^J#xPYTM?Ov6>z?^wNynFkoA*YFK*SOExr}LXJ-umS&gw z?SiE_-f>dGAxTVPGqSR<&>8QDM&hyfVl=YQb%4hEV*3{tnbg=8>peW1J$+{3iN&+8 zi0ecD9Ram#uJ?Soi#6wBsf?Y?t%_e)6w@gV2vI`W6;Zxh5rwg8Ww2prct(~Bj($`( zRo5u$N!8Vz8M)RWbq;52Yb381Nr{P!Z5FZ?dFnE%nJG}P-e;OAC>yoZtiCL7sjb=8s3?<#v~jXvxTob7NfO6u zR(3M^Bk8zCt9;1Tc&6rglxF2NNt)*K19_c#G0obPJD99WqYkZD9AXcPGTC9HkJGId zsduxOBx`KURMWaCruMNeNq&G$6vbtU$$EBBVv}@xvb1PN%=IewOY-N^WBkMqs*Zak zr(1MxehGceigJw4_i380Ns`o(XczajCD9=UGXL6ahbY_!Lofvyu!#O=;T${%=iv>w z03X0b_!K^aFX1cr8ZN^R@FV;LKf|x^8~hG`ARxmktj0!+;H}t#t+*TG*ophG3wtn$ zgE)+b@L`<5Nu0uI%%F)`w6KIr_#{4!&*01W8oq^Z<2(2+eux*vxcxo_YSd{4an zLahA>e_LmwzH=;m=2v&IgKUn_%{~I}E5F02EDDXd104tNJWIrvs z+)I_$+7a4jY2%H@EeY$TQ0JB7WW92ntY40Mu@MU8ISJn`2}?87{_#Hog71Iu{rAYb Hxx2psUZ@UZ literal 0 HcmV?d00001 diff --git a/PowerShell/.DS_Store b/PowerShell/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..fb41676ed7228b805d59a5de6994d53b4eae629a GIT binary patch literal 8196 zcmeHMTTC2P7(V|3?F^Ha0TG6>;95#)E7o0FYHg%lb|IDm0$nJVw!rMp2oq;#)|uI* zKvCYjV0>!4)M($lC%#y(Z{A{~4_;8yKA4!k8Z{cD@kReLXSOM{+mM(TqH~gS{xj$Q z|8x1ycjnB$O9+8jR*4WI5kff3REh!?*GW`Q^ED~pwNNFXKOsFNK~!Rq9F~6a5%3Z4 z5%3Z45%3XMj|kwMO%qq(-e+3B^bzn8_+KJG-wz?oR7O3y$fWz~z`_jyP!?meo3M^? zfa??TWYm+3OuDYvCyO4ya|JKN0OpSR5H&{`_2eRx%$)#pC*X|?UW5Yf=;#-6%?U)A z^h+NBAAxHTpvOiay%@JA$iRyD-Ac~bnWSze(c(ffqiePk4u1=QqT-Sb8#%6&E8SFj zGBIt=Bplhv+A-NWPM`W^XFBQC2TfDIQkRp*G<9Y|tc;t6BWs3gxz3uRqREM2%}~sl zn3**c+wCX86dcEM{DdgY&+lmoH-}mxEep+|`F;DbzBkgku)uTGjghvl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dev/null +++ b/PowerShell/MSSQLServer/download-CsvToSQL.ps1 @@ -0,0 +1,81 @@ +<# +.SYNOPSIS +Retrieves information from an online CSV file, import it in a table of an database. + +.DESCRIPTION +This download-CsvToSQL.ps1 script uses the SQL Server Powershell module to connect to a SQL Server and import data in a table. +The source of the information gets downloaded from the internet and the CSV - value is ; +Written by Floris van Enter | EnterMI + +.PARAMETER sqlServer +a single computer name or an array of computer names. You mayalso provide IP addresses. + +.PARAMETER database +The database to put in the information. + +.PARAMETER schema +The schema to associate the objects with. +This is an optional parameter; if it is not included, dbo schema will be used. + +.PARAMETER table +The table to put in the information. If it does not exist, the table will be created. +This is an optional parameter; if it is not included, export table will be used. + +.PARAMETER url +The location of the CSV. It must be an internet location. + +.PARAMETER file +The location to store the downloaded CSV data in. + +.EXAMPLE +Read a file from a website into the table +download-CsvToSQL.ps1 -sqServer "SQL01" -database "migrate-data" -table "export" -url "http://localhost/data.csv" -file "c:\temp\file.csv" + +.LINK +https://github.com/beakerflo/EnterMI +#> +[CmdletBinding()] +param( + [string]$sqlServer, + [string]$database, + [string]$schema = 'dbo', + [string]$table = 'export', + [string]$url, + [string]$file + ) + +If (Test-Path $file) { + Remove-Item $file + } + +if (Get-Module -ListAvailable -Name SqlServer) { + Write-Verbose "Importing SqlServer" + Import-Module "sqlps" -DisableNameChecking +} elseif (Get-Module -ListAvailable -Name sqlps) { + Write-Verbose "Importing sqlps" + Import-Module "sqlps" -DisableNameChecking +} else { + Write-Host "Module does not exist" + Write-Host "This script needs 'SqlServer' or the older 'sqlps'" + Write-Host "Please install..." + exit +} + +function Run-SqlQuery { + param( + [string]$query + ) + Write-Verbose "execute $query on $sqlServer" + Invoke-SQLcmd -ServerInstance $sqlServer -Database $database -Query $query +} + +# Download CSV file from website +Write-Verbose "Download $url to $file" +(New-Object System.Net.WebClient).DownloadFile($url,$file) + +# Clean table to receive results from CSV +Run-SqlQuery -query "TRUNCATE TABLE [$schema].[$table]" + +# Read CSV file, import to database and run clean-up stored procedure +Write-Verbose "Import $file to $database.$schema.$table" +Import-Csv -Path $file -Delimiter ";" -header("Kolom1","Kolom2","Kolom3","Kolom4") | Write-SqlTableData -ServerInstance $sqlServer -DatabaseName $database -SchemaName $schema -TableName $table -Force \ No newline at end of file