Help with text parsing

Welcome Forums General PowerShell Q&A Help with text parsing

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
1 month, 1 week ago.

  • Author
    Posts
  • #120811

    Participant
    Points: 31
    Rank: Member

    An edge device returns a text blob at me from the following code run on a remote server (this code works)

    
    $all_accounts = Invoke-Command -ComputerName $server -ScriptBlock {Invoke-WebRequest -Uri $args[0] -UseBasicParsing -Credential $args[1]} -ArgumentList $get_all_accounts_url, $credential -ErrorAction Stop
    
    

    $all_accounts is a variable with this info (below)

     

    what would be the best way to parse out the accounts after "admin="

    root="value"
    bin="value,value,value"
    daemon="value,value,value"
    sys="value,value"
    tty="value,value,value"
    admin="value,value,value,value"
  • #120817

    Participant
    Points: 809
    Helping Hand
    Rank: Major Contributor

    Its so easy in PowerShell way, would like you to put your hands in. Please read below help documentation.

    Get-Help Select-String -Full
    Or
    Get-Help Select-String -Online
    Get-Help ConvertFrom-String -Online

    Once you start, feel free to post your issues here, we are here to help you.

  • #120819

    Participant
    Points: 265
    Helping Hand
    Rank: Contributor

    Hmm. You can use -like or a regex -match to get the right string, and then a -replace for the quotation marks and a -split to get each value individually.

    It'd probably look something like this, I suppose?

    $AdminAccounts = $All_Accounts.Where{ $_ -like 'admin*' } -replace 'admin=|"' -split ','
  • #120820

    Participant
    Points: 31
    Rank: Member

    Hi sorry I should've said, this is my first attempt at select-string and tried it before posting

    
    $admin_accounts = $all_accounts | Select-string -Pattern "admin=" -Context
    
    

    doesn't do what I think it should, it just returns the entire text blob

  • #120822

    Participant
    Points: 809
    Helping Hand
    Rank: Major Contributor

    Got it, here is a similar topic. The reason for it is also mentioned in this post.

    https://powershell.org/forums/topic/select-string-for-linux-users/

  • #120825

    Participant
    Points: 31
    Rank: Member

    ahh figured it out.

    i needed to do this prior to select string

    
    $all_accounts -split "`n"
    
    

     

    sometimes talking to yourself out loud on the internet works

  • #121108

    Participant
    Points: 70
    Rank: Member

    sometimes talking to yourself out loud on the internet works

    🙂

You must be logged in to reply to this topic.