Author Posts

August 2, 2015 at 1:32 pm

I need a huge help. I have an Oracle database and I'm trying to get instance name with powershell.
Does anyone know how can i do this?

Here is the script:

##########################
[System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient") | Out-Null

#########################
#Connecting to Oracle DB:
###########################

$connection = New-Object System.Data.OracleClient.OracleConnection($connectionString);
$connection.Open();

##Check Status:

try {

### sql query command ###
$OracleSQLQuery = "select instance name from v$Instance";

### create object ###
$SelectCommand = New-Object System.Data.OracleClient.OracleCommand;
$SelectCommand.Connection = $connection
$SelectCommand.CommandText = $OracleSQLQuery
$SelectCommand.CommandType = [System.Data.CommandType]::Text

### create datatable and load results into datatable ###
$SelectDataTable = New-Object System.Data.DataTable
$SelectDataTable.Load($SelectCommand.ExecuteReader())

}
catch
{
write-error " An Exception has occured. See the following for the details : ";
write-host "The exception is " $_.Exception.ToString();
}

$connection.Close();

August 3, 2015 at 12:22 am

Hi Haley,

I think you just need to replace the double quotes of below line


$OracleSQLQuery = "select instance name from v$Instance";

with single quotes to prevent PowerShell's attempt to expand an $Instance variable which does not exist in your script to an empty value.


$OracleSQLQuery = 'select instance name from v$Instance';

I hope above helps.

Regards,
Daniel