PowerShell retrieve records from Database

Here we have the function getPostCommandDetails that will retrieve some data from Oracle database and create objects of the custom type using the retrieved data.


Function getPostCommandDetails(){
   
    $clientsString="001";
    $resultSet="";
 
    $queryString="SELECT CLIENTID,COMMAND,SERVER,USERNAME,PASSWORD,DATASOURCE FROM TABLE_NAME WHERE clientid IN ($clientString) ";
    LogMessage $logFile "DEBUG" ("Query for getting postprocessing commands is "+$queryString);   
    try{
    [System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient");
    $conn = new-object System.Data.OracleClient.OracleConnection($script:testConnectionString);
    LogMessage $logFile "DEBUG" ("Getting connection using string "+$script:oamConnectionString);   
    $command = new-Object System.Data.OracleClient.OracleCommand($queryString, $conn);
    $command.CommandType="Text";
    $conn.Open();
    $resultSet=$command.ExecuteReader();
    while($resultSet.Read()){
        $clientId=$resultSet.getString(0);
        $command=$resultSet.getString(1);
        $server=$resultSet.getString(2);
        $username=$resultSet.getString(3);
        $password=$resultSet.getString(4);
        $datasource=$resultSet.getString(5);
         $script:clientsMap.add($clientId,$clientObj);
    }
    }catch{
        LogMessage $logFile "DEBUG" ("Some Exception Occured in getPostCommandDetails");   
        $errorOccured=$TRUE;
    }
    finally{
            try{
                $conn.Close();
            }catch{
                $errorOccured=$TRUE;
                LogMessage $logFile "DEBUG" ("Some Exception Occured closing connection in getPostCommandDetails");   
            }
    }
}

Here the value testconnectionString should be something like:
Password=****;User ID=***;Data Source=*****;Persist Security Info=True

where DataSource is the one specified in tnsnames.ora file of the Oracle Client.

No comments:

Post a Comment