header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
working with data table and variable
Last Post 21 Jun 2011 08:59 AM by astreet. 7 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Resolved
NonsecUser is Offline
New Member
New Member
Posts:4
Avatar

--
28 Jul 2010 06:44 PM

    Hello everyone,
    I have a script (actually find on Poshcode) I want define if and else conditions and write output. I am having issue figure out manipulate returning data. Here is the script;

    function Get-OLEDBData {
    param (
    [string]$server = "Server1",
    [string]$instance ="PPP"
     # [string]$port ="1527",
     # [int]$Threshold ="70" )
     $sql = "select * from dba_tablespace_usage_metrics"
     $connectstring = "password=mypass;UserID=myuser;DataSource=$instance;Provider=OraOLEDB.Oracle"
     $OLEDBConn = New-Object System.Data.OleDb.OleDbConnection($connectstring) \$OLEDBConn.open()
     $readcmd = New-Object system.Data.OleDb.OleDbCommand($sql,$OLEDBConn) $readcmd.CommandTimeout = '30'
    $da = New-Object system.Data.OleDb.OleDbDataAdapter($readcmd)
    $dt = New-Object system.Data.datatable
     [void]$da.fill($dt)
     $OLEDBConn.close()
     return $dt }

    this returns;

    TABLESPACE_NAME  USED_SPACE   TABLESPACE_SIZE USED_PERCENT
    ---------------  ---------    --------------- ------------
    ASSRM            52543200     57647104        91.14629591800483160437686514
    PSTRMC    	 2268816      3200000         70.9005
    

    Now I want to manipulate this data and define if and else condition such as = if (  tablespace_name -eq etc. ) or elseif ( used_percent -gt "30") and etc. However I am running into problem.

     if($_.used_percent -gt "30") {
    Write-host Table Space Name $Tablespacename is crossed the threshold current value is $Used_Percent and $Used_Space for the database $instance and on the server $Server

    the output i want to see for each table space for example;

    Table Space name ASSRM is crossed the threshold current value is 91.14 and used Space 52543200 for the database PPP and the server Server1

    I coud not figure to create variable for $tablespacename  $used_percent and $used_space then use for the output.
    Any help greatly appreciated.
     Thank you.



    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    29 Jul 2010 04:17 AM

    Try this:

    $server = "Server1"
    $instance = "PPP"

    $dataTable = Get-OLEDBData -Server $server -Instance $instance

    $dataTable.Rows | ForEach-Object {
        if ($_["USED_PERCENT"] -gt 30)
        {
            Write-host
            ("Table Space Name {0} is crossed the threshold current value is {1} and {2} for the database {3} and on the server {4}" `
            -f $_["TABLESPACE_NAME"], $_["USED_PERCENT"], $_["USED_SPACE"], $instance, $server)
        }
    }



    NonsecUser is Offline
    New Member
    New Member
    Posts:4
    Avatar

    --
    29 Jul 2010 10:17 PM
    GWHowarth88,

    Thank you very much for the response . It worked fine here is the result;

    Table Space Name SYSTEM has crossed the threshold current value is 76.789999999 and used space is 53444 for the database PPP on the server Server1

    if you don`t mind replying  i have a couple of question for you too.

    Thanks again.



    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    30 Jul 2010 02:48 AM
    Fire away.


    NonsecUser is Offline
    New Member
    New Member
    Posts:4
    Avatar

    --
    01 Aug 2010 09:04 PM
    GWHowarth88,

    Thank you very much.I appreciate your help.   Here are my questions;

    1- I want to put server names and database (instance) in a csv file. it will contain server name and database name. Script will go will read server name and instance name from csv file and and it will process for each data row.

    2- My second question is I am using OLEDB components for the previous script. I am also trying to do samething with Oracle client components so far I could not manage the same output. I attached the script and output. Would you mind looking that script?

    3- When i run the script (attached Oracle script) the below example the when i put comma after return it changes structe of the script returning data is different. What is funtion of the putting comma there?

    $table = new-object system.data.datatable
    $table = $set.Tables[0]
    return $table
    return, $table

    Thanks again. Please let me know if i can provide you more information on this.

    Oracleclient.txt

    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    02 Aug 2010 03:13 AM

    1. I updated the script so that it accepts a path to a CSV file. The script assumes that the CSV file is in the format:

    Server, Instance
    someServer, someInstance

    2. I'm not exactly sure what you mean when you say "same output". If you mean that you don't want "11" being outputted, pipe $adapter.Fill() to Out-Null. I updated, the script with this assumption.

    3. I don't know what the effect is when you put a comma after the return keyword. All I know is that when you precede a variable with a comma, it wraps the variable into an array.

    param (
        [String]$Path
    )

    function Get-OraTableSpace
    {
        param (
            [String]$Server,
            [String]$Instance,
            [String]$Port = "1527"
        )
        
        [System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient") | Out-Null
       
        $connection = New-Object System.Data.OracleClient.OracleConnection( `
            "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$server)(PORT=$port)) `
            (CONNECT_DATA=(SERVICE_NAME=$instance)));User Id=myuser;Password=mypass;");
        
        $set = New-Object System.Data.DataSet   
        $query = "select TABLESPACE_NAME , round ( USED_SPACE / (1024 * 1024),2) as USED_SPACE_MB , TABLESPACE_SIZE/1024 TABLESPACE_SIZE_MB , round(USED_PERCENT,2) USED_SPACE_PERCENTAGE from  dba_tablespace_usage_metrics"
        
        $adapter = New-Object System.Data.OracleClient.OracleDataAdapter ($query, $connection)
        $adapter.Fill($set) | Out-Null
        
        return $set.Tables[0]
    }

    function Main
    {
        Import-CSV -Path $Path | ForEach-Object {
            $server = $_.Server
            $instance = $_.Instance
       
            (Get-OraTableSpace -Server $server -Instance $instance).Rows | ForEach-Object {
                if ($_["USED_SPACE_PERCENTAGE"] -gt 30.00)
                {
                    Write-host
                    ("Table Space Name {0} is crossed the threshold current value is {1} and {2} for the database {3} and on the server {4}" -f $_["TABLESPACE_NAME"], $_["USED_SPACE_PERCENTAGE"], $_["USED_SPACE_MB"], $instance, $server)
                }
            }
        }
    }

    Main



    NonsecUser is Offline
    New Member
    New Member
    Posts:4
    Avatar

    --
    03 Aug 2010 10:15 PM
    GWHowarth88,

    Thank you very much for your help.


    astreetUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    21 Jun 2011 08:59 AM

    New to powershell

    What would the command look like when passing in comma delimitted server list?



    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer