Powershell Loop SQL Query -
two issues powershell code aware of , cannot solve.
i identifying number of csv files within directory aiming import hash table 1 one , run sql query update original csv.
the module in self have got working when not try run loop.
i searching through directory , picking csv files name cash.
i check imported file contains data , not blank record
if ($check.client) { write-host "$($name) contains data" $files = get-childitem $location | where-object {$_ -like "*cash*csv"}
at point (trying) running loop run same sql query on each item firstly doesn't seem working loop , secondly on export of compiled data getting error stating
a positional parameter cannot found accepts argument '+'
again sadly cannot see why there issue this?
i throwing out there after severely smashing head against wall , re-interpreting 1000 different ways cannot solution work...
ideas?
$location = "l:\controls\bcr\" $files = get-childitem $location | where-object {$_ -like "*cash*csv"} foreach ($file in $files) { $name = $file.name $basename = $file.basename $suffixname = $file.extension $variablea = "cl3" $nn = ($basename + $variablea + $suffixname) $check = import-csv $file if ($check.client) { write-host "$($name) contains data" try { ################ #### query #### ################ ##################################### $match = "isin" $matchs = "nsi" ## prepare sql ## $misa = 'xx.xx.x.xx' $misb = 'xx.xx.x.xx' $username = 'xxxxx' $password='xxxxx' $db = 'int' ## create sql connection ## $sqlconnection = new-object system.data.sqlclient.sqlconnection $sqlconnection.connectionstring = "data source=$misa;initial catalog=$db;initial catalog=$db;user id=$username;password=$password;" $sqlconnection.open() $sqlcmd = $sqlconnection.createcommand() ## process csv ## cd $location import-csv $name -header @("a","client","balance","account","reconciled","unique","sql","sql2","breakref","breaknarrative","breakisin","cl2balance") | select -skip 1 | #import csv foreach-object { # data sql $query = @" select ltrim(rtrim(cl3clientnum)) + ltrim(rtrim(cl3value)) 'unique', cl3clientnum, cl3referencenum breakref, cl3accountnum, cast(cl3value money) value, cl3menunum, cl3date1, cl3date2, cl3quantity, cl3securitynum, ltrim(rtrim(cl3remarks1)) + ' ' + ltrim(rtrim(cl3remarks2)) + ' ' + ltrim(rtrim(cl3remarks3)) + ' ' + ltrim(rtrim(cl3remarks4)) + ' ' + ltrim(rtrim(cl3remarks5)) 'remarks' t5cashl3 ltrim(rtrim(cl3clientnum)) = '$($_.client)' , ltrim(rtrim(cl3value)) = '$($_.balance)' , cl3accountnum = '$($_.account)' , cl3menunum = '211' , cl3revflag = 'n' order cl3referencenum desc "@ $sqlcmd.commandtext = $query.replace("%client%", $_.client) $sqlcmd.commandtext = $query.replace("%balance%", $_.balance) $sqlcmd.commandtext = $query.replace("%account%", $_.account) $result = $sqlcmd.executereader() $table = new-object "system.data.datatable" $table.load($result) if (!(test-path $location + $name)) { $table | export-clixml $location + $name } # pass row on through pipeline $_.breakref = $table.breakref $_.breaknarrative = $table.breaknarrative $_.breakisin = $table.breakisin write-output $_ } | export-csv ($nn) -notypeinformation } { # close sql connection if error encountered. $sqlconnection.close() } ### end of positive loop ### } else { write-host "$($name) not contain data" } $name }
Comments
Post a Comment