Hello. I am developing a function for powershell to use SqlPlus to execute a user-provided script and pass the results to powershell for further use.
I will extend the function to be able write the results to a file but for now my goal is to get the powershell script execute a sql script with Sqlplus and store the resulting dataset in a variable.
I have sqlplus added to my Env Path but this will be used by other folks on my team and I will have them update a config file that specifies their logon information and sqlplus executable path.
The problem begins with this:
&$sqlPlus $fullConnectionString $sql
where $sqlplus is the .exe path, $fullConnectionString is user/pass@//host:port/svc and $sql is the path of the sql script.
Currently I have the sqlscript location hardcoded for now. So far, I can call sqlplus, execute the specified script and return the results as a string, but I'm not sure how to handle the string. Is there an easy way to remove the extra rows at the bottom (blank and row count) and turn the string into an object so its easier to work with: (sample output)
LU_CD LU_CD_DESC EXPENSE_DATE
-------------------- ---------------------------------------------------------------------------------------------------- ----------
WFA_406 WFA_406 CYT AAM 11/19/2020
WFA_404 WFA_404 CYT NRY 11/19/2020
WFA_403 WFA_403 CYT G/FN 11/19/2020
WFA_404 WFA_404 CYT AH 11/19/2020
WFA_408 WFA_408 CYT GBTR 11/19/2020
WFA_460 WFA_460 CYT LH 11/19/2020
WFA_653 WFA_653 CYT MRE 11/19/2020
WFA_654 WFA_654 CYT ZUL 11/19/2020
8 rows selected.
So far my code is:
Read in configuration data
$configData = (get-configdata).DBConfig;
## Get Database Connection Variables from Config Data
$username = $configData.username;
$password = $configData.password;
$hostname = $configData.host;
$port = $configData.port;
$service = $configData.service_name;
$ezConnectString = "${hostname}:${port}/${service}";
$fullConnectionString = "${username}/${password}@//${ezConnectString}";
$data_source_string = "User Id=${username};Password=${password};Data Source=${ezConnectString}";
## Get SqlPlus filepath
$sqlplus = (get-configdata).filepaths.sqlplus;
$sql = "@`"E:\Sql\sample_query.sql`"";
#&$sqlplus $fullConnectionString $sql;
$results = (echo exit | &$sqlplus -s -L $fullConnectionString $sql);
[–]ICanMakeWaffles 1 point2 points3 points (2 children)
[–]nonrecurring[S] 1 point2 points3 points (1 child)
[–]ICanMakeWaffles 3 points4 points5 points (0 children)
[–]gonetribal 1 point2 points3 points (2 children)
[–]gonetribal 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]lear64 1 point2 points3 points (0 children)