as newbie in powershell, im trying read thru folder has multiple sql files , iterate them through poweshell scripts read data oracle , export csv.
if sqlfile has single line statement no issues code, working fine, if sql file has multiple line statement - has,
the powershell errors out saying "get-datatable : cannot process argument transformation on parameter 'sql' cannot convert value type system.string."
could please me how resolve issue? below code snapshot.
function get-datatable{ [cmdletbinding()] param( [parameter(mandatory=$true)] [oracle.dataaccess.client.oracleconnection]$conn, [parameter(mandatory=$true)] [string]$sql ) $cmd = new-object oracle.dataaccess.client.oraclecommand($sql,$conn) $da = new-object oracle.dataaccess.client.oracledataadapter($cmd) $dt = new-object system.data.datatable [void]$da.fill($dt) return ,$dt } foreach ($file in get-childitem -path $scriptsdirectory -filter *.sql | sort-object -desc ) { $sqlquery = get-content "$scriptsdirectory\$file" echo $sqlquery $filename = $file.name.split(".")[0] $dt = get-datatable $conn $sqlquery write-host "retrieved records:" $dt.rows.count -foregroundcolor green $dt | export-csv -notypeinformation -literalpath $workingdirectory\$filename.csv write-host "output written :" $workingdirectory\$filename.csv -foregroundcolor green }
get-content
returns array of lines. if you're using powershell v3 or higher can use -raw
parameter read file 1 big string:
$sqlquery = get-content "$scriptsdirectory\$file" -raw
alternatively re-join array line endings:
$sqlquery = $sqlquery -join "`r`n"
or can read file @ once .net classes:
$sqlquery = [system.io.file]::readalltext("$scriptsdirectory\$file")
Comments
Post a Comment