How do i get instance name from oracle database?

This topic contains 1 reply, has 2 voices, and was last updated by  Daniel Krebs 2 years, 4 months ago.

  • Author
    Posts
  • #28124

    Haley
    Participant

    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();

  • #28129

    Daniel Krebs
    Moderator

    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

You must be logged in to reply to this topic.