How to pick certain word from an array?

Tagged: ,

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of nitheesh babu nitheesh babu 3 weeks, 3 days ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #53177
    Profile photo of nitheesh babu
    nitheesh babu
    Participant

    I am trying to return certain column of an array

    My array looks like this(with all these number of spaces in between):
    Soft One server01 1156
    Soft One server04 1172
    Soft One server03 1137
    Soft One server02 1184

    I only need to read the value of last column(that is the number) and supply it for other commands.

    $xaload = qfarm /load /continue /app "Soft One" 
    for ($i = 4; $i -lt $xaload.Length; $i++)
    { 
       $line =  ([string]$xaload[$i]).Split(',')[0];
       $line.split()[0] #what number I should give in the square braces to get the last column value only?
    #when I select 0 it returns the first word of each line which is "soft"
    #when I select 1 it returns the second word which is"one"
    }
    

    Thanks in advance for attending.

    #53186
    Profile photo of Craig Duff
    Craig Duff
    Participant

    You can count backwards from the end with negatives, so -1

    #53193
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    If you always want the last item, then you typically use upperbound to get highest part of the array. In this case, you have 4 items in the array, so you could also use a static index value like $arrRow[3].

    $array = @(
    "Soft One server01 1156",
    "Soft One server04 1172",
    "Soft One server03 1137",
    "Soft One server02 1184"
    )
    
    $results = foreach ($row in $array) {
        $arrRow = $row -Split " "
        $arrRow[$arrRow.Count -1]
        #or
        $arrRow[$arrRow.Length -1]
        #or
        $arrRow[$arrRow.getupperbound(0)]
    }
    
    $results
    
    #53221
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Have you looked at using the Citrix XenApp PowerShell SDK and its cmdlets to get the details as proper PowerShell objects instead of doing prayer based text parsing of the qfarm command line tool output?

    https://www.citrix.com/downloads/xenapp/sdks/powershell-sdk.html

    http://powerschill.com/citrix/using-powershell-to-get-xenapp-server-load/
    https://www.citrix.com/blogs/2010/08/17/getting-a-farm-inventory-with-xenapp-6-powershell-scripting/

    • This reply was modified 3 weeks, 3 days ago by Profile photo of Daniel Krebs Daniel Krebs.
    #53223
    Profile photo of nitheesh babu
    nitheesh babu
    Participant

    Thanks Craig. Thanks Rob. That was really helpful.

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

You must be logged in to reply to this topic.