Help on Writing Output Information to PS Console.

This topic contains 5 replies, has 3 voices, and was last updated by  Ivan 3 months, 1 week ago.

  • Author
    Posts
  • #78778

    Rajkumar Balasakuri
    Participant

    Below is my Sample Code
    ————————

    $scon2 = New-Object System.Data.SqlClient.SqlConnection
    $scon2.ConnectionString = "$sconstring"

    $cmd3 = New-Object System.Data.SqlClient.SqlCommand
    $cmd3.Connection = $scon2
    $cmd3.CommandType = [System.Data.CommandType]::StoredProcedure
    $cmd3.CommandText = "sp_replmonitorsubscriptionpendingcmds"

    $cmd3.Parameters.Add("@publisher",[System.Data.CommandType]::NVarChar)
    $cmd3.Parameters.Add("@publisher_db",[System.Data.CommandType]::NVarChar)
    $cmd3.Parameters.Add("@publication",[System.Data.CommandType]::NVarChar)
    $cmd3.Parameters.Add("@subscriber",[System.Data.CommandType]::NVarChar)
    $cmd3.Parameters.Add("@subscriber_db",[System.Data.CommandType]::NVarChar)
    $cmd3.Parameters.Add("@subscription_type",[System.Data.CommandType]::Int)
    $cmd3.CommandTimeout = 0
    $scon2.Open()

    From the above Code Everything Works fine but these below code of lines
    $cmd3.Parameters.Add("@publisher",[System.Data.CommandType]::NVarChar)
    $cmd3.Parameters.Add("@publisher_db",[System.Data.CommandType]::NVarChar)
    $cmd3.Parameters.Add("@publication",[System.Data.CommandType]::NVarChar)
    $cmd3.Parameters.Add("@subscriber",[System.Data.CommandType]::NVarChar)
    $cmd3.Parameters.Add("@subscriber_db",[System.Data.CommandType]::NVarChar)
    $cmd3.Parameters.Add("@subscription_type",[System.Data.CommandType]::Int)

    is writing the below information (which i dont want) to the console (am using Write-Host ) of power shell

    AllowDBNull : True
    AutoIncrement : False
    AutoIncrementSeed : 0
    AutoIncrementStep : 1
    Caption : DataBaseName
    ColumnName : DataBaseName
    Prefix :
    DataType : System.String
    DateTimeMode : UnspecifiedLocal
    DefaultValue :
    Expression :
    ExtendedProperties : {}
    MaxLength : -1
    Namespace :
    Ordinal : 0
    ReadOnly : False
    Table : {}
    Unique : False
    ColumnMapping : Element
    Site :
    Container :
    DesignMode : False

    ——– below this my actual output comes as below————-

    ********************************************************************
    From DUBIS3DVSQLP01\VIP01 To CHENS3DVSQLP01\VIP01
    ********************************************************************

    DataBaseName DataBaseType PendingTXns Estimated Time(Sec)
    ———— ———— ———– ——————-
    HDR9_CDB Catalog 0 0
    HDR9_CDB_SCHEMA CatalogSchema 0 0
    SONPD_MDB Model 0 0
    HDR9_SDB Site 0 0
    HDR9_SDB_SCHEMA SiteSchema 0 0

    ********************************************************************
    From DUBIS3DVSQLP01\VIP01 To SAUDS3DVSQLP01\VIP01
    ********************************************************************

    DataBaseName DataBaseType PendingTXns Estimated Time(Sec)
    ———— ———— ———– ——————-
    HDR9_CDB Catalog 0 0
    HDR9_CDB_SCHEMA CatalogSchema 0 0
    SONPD_MDB Model 0 0
    HDR9_SDB Site 0 0
    HDR9_SDB_SCHEMA SiteSchema 0 0

    1. Can any one please advise me how to get rid of that unnecessary stuff written on power shell console
    2. I have done using Cls but this will wipe entire my results and it will show only last result (as am calling this through a function in script three times).

  • #78779

    Chris Bakker
    Participant

    you can use

    [void]$... 

    This will suppress the message you code is generating.
    As for the loop, we need to see the code whats going on there.

    • #78785

      Rajkumar Balasakuri
      Participant

      Thanks for the information and quick help.

      Can you please explain little bit in detail. In terms where to use it my code or probably some sample code [void]$...

    • #78787

      Chris Bakker
      Participant

      I never work with System.Data.SqlClient.SqlConnection, so its a littlebit hard to quess where the output is comming from.
      Gt is in powershell_ice and go line by line. It should give the exact lines you have to change.

    • #78791

      Rajkumar Balasakuri
      Participant

      Thanks Mr Chris, Highly Appreciated.

  • #78814

    Ivan
    Participant

    There's also another way to prevent output instead of using [void] and it's outputting (piping) to Null.

    Example:

    Something here | Out-Null

You must be logged in to reply to this topic.