locked
Powershell command to filter values RRS feed

  • Question

  • PS C:\> Get-EC2Instance |%{ $_.RunningInstance } | Select-Object InstanceId,@{Name='TagValues'; Expression={($_.Tag |%{$_.Value }) -join ','}}

    InstanceId                                              TagValues
    ----------                                                  ---------
    i-230151d                                                Sco
    i-8c80f27                                                 landscape123,test
    i-3d3195                                                  temp,landscape234
    i-a2f216                                                  Ras
    i-1a594c1                                                Aeau

    How do I filter the values so the output can be

    InstanceId                                              TagValues
    ----------                                                  ---------
    i-8c80f27                                                landscape123
    i-3d3195                                                 landscape234

    *The TagValues will always be landscape*

    Tuesday, May 26, 2015 5:11 AM

Answers

  • Get-EC2Instance | 
        %{ $_.RunningInstance } | 
        %{
            $id=$_.InstanceID
            foreach($tagvalue in $_.Tag){
                [PSCustomObject]@{$InstancID=$id;TagValue=$tagvalue}
            }
        } | 
        ?{$_.TagValue -match 'landscape'}
    


    \_(ツ)_/

    • Proposed as answer by AnnaWY Thursday, June 4, 2015 12:21 PM
    • Marked as answer by AnnaWY Saturday, June 6, 2015 9:24 AM
    Tuesday, May 26, 2015 10:50 AM

All replies

  • # Short Version

    Get-EC2Instance | %{ $_.RunningInstance } | % { if($TagValues = ($_.Tag -like 'landscape*')) { [PSCustomObject]@{ InstanceId=$_.InstanceId; TagValue=($TagValues -join ",")} } }

    • Edited by Jean-Chris Tuesday, May 26, 2015 7:54 AM
    Tuesday, May 26, 2015 7:52 AM
  • # Long Version if you want to keep your code (The array is the result of your previous statements)

    $array = @(

                [PSCustomObject]@{ InstanceId="i-230151d  "; TagValues="Sco"}

                [PSCustomObject]@{ InstanceId="i-8c80f27"; TagValues="landscape123,test"}

                [PSCustomObject]@{ InstanceId="i-3d3195"; TagValues="temp,landscape234"}

                [PSCustomObject]@{ InstanceId="i-a2f216"; TagValues="Ras"}

                [PSCustomObject]@{ InstanceId="i-1a594c1"; TagValues="Aeau"}

              )

     

     

    # Find "landscape*" with RegEx

    $array | % { if($_.TagValues -match "\A.*(?<TagValue>landscape[^,]*).*\z") { [PSCustomObject]@{ InstanceId=$_.InstanceId; TagValue=$matches["TagValue"]} } } 

    Tuesday, May 26, 2015 7:54 AM
  • ?{$+.TagValues -match 'landscape'}

    This will match anywhere.  No need to get fancy.


    \_(ツ)_/

    Tuesday, May 26, 2015 9:48 AM
  • You forgot that he/she wants to change the output as well...
    • Edited by Jean-Chris Tuesday, May 26, 2015 10:49 AM
    Tuesday, May 26, 2015 10:37 AM
  • Thanks. Your solution doesn't actually work.....I am not sure why....

    This is the command and output:

    I only want the output display i-8c80f27a with TagValue column value "landscape123".....Is it possible?


    • Edited by Beau Beau Tuesday, May 26, 2015 10:56 AM
    Tuesday, May 26, 2015 10:50 AM
  • Get-EC2Instance | 
        %{ $_.RunningInstance } | 
        %{
            $id=$_.InstanceID
            foreach($tagvalue in $_.Tag){
                [PSCustomObject]@{$InstancID=$id;TagValue=$tagvalue}
            }
        } | 
        ?{$_.TagValue -match 'landscape'}
    


    \_(ツ)_/

    • Proposed as answer by AnnaWY Thursday, June 4, 2015 12:21 PM
    • Marked as answer by AnnaWY Saturday, June 6, 2015 9:24 AM
    Tuesday, May 26, 2015 10:50 AM