how add contents of the string to 2 table objects

  • Hi,

    I have string with following format

    $string = @'

    AvgSQLCpu,AVGCpu,AVGOtherCpu

    100,97,2

    alertType,alertStatus,alertGenerate,alertDetails

    HIGHCPU,ERROR,0,

    '@

    I have 2 table objects

    [object] $tbl1 = New-Object Data.DataTable "tbl1"

    $null=$tbl1.Columns.Add("AvgSQLCpu",[int])

    $null=$tbl1.Columns.Add("AVGCpu",[Int])

    $null=$tbl1.Columns.Add("AVGOtherCpu",[Int])

    [object] $tbl2 = New-Object Data.DataTable "tbl2"

    $null=$tbl2.Columns.Add("alertType",[string])

    $null=$tbl2.Columns.Add("alertStatus",[string])

    $null=$tbl2.Columns.Add("alertGenerate",[Int])

    $null=$tbl2.Columns.Add("alertDetails",[string])

    How do I insert

    100,97,2 into tbl1

    and

    HIGHCPU,ERROR,0, into tbl2

    Thank you

  • I would get the individual values as such:

    $v = $string.Split(",");

    This splits the values as follows:

    AvgSQLCpu

    AVGCpu

    AVGOtherCpu

    100

    97

    2

    alertType

    alertStatus

    alertGenerate

    alertDetails

    HIGHCPU

    ERROR

    0

    Where $v[0] is the string "AvgSQLCpu", $v[1] is the string "AVGCpu", $v[2] is the string "AVGOtherCpu" etc.

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Thanks a lot ! Good and simple!

    the only problem

    Where $v[4] # no comma and return 2 values

    AVGOtherCpu

    100

    Where $v[5] # no comma and return 2 values

    alertDetails

    HIGHCPU

    need somehow split also "end of the line"

  • $v = $string.Split("`n");

    $x = ($v[1]).split(',')

    $y = ($v[3]).split(',')

    $x[0]

    $x[1]

    $x[2]

    write-output "second dataset"

    $y[0]

    $y[1]

    $y[2]

  • Thanks for posting your final solution.

    (Mine worked for me but maybe copy 'n' paste lost the newline character).

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Gary Varga (11/22/2016)


    Thanks for posting your final solution.

    (Mine worked for me but maybe copy 'n' paste lost the newline character).

    Looking at the original post I don't know how mine ever worked :blink:

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply