none
Selecting particular elements of output when using Split

    Soru

  • Hi

    I'm trying to extract just the machine name from the Key Management Service event log in realtion to activation requests.  What I've got so far is:

    $entries = Get-EventLog 'Key Management Service' | ?  {$_.InstanceId -eq '12290'}

    foreach($entry in $entries)
        {
            ($entry.Message).Split(",")
        }

    but then I want to only display the 5th line of the output (which is where the name of the machine initiating the activation request is shown).

    I thought that maybe I could treat it as an array and then select the relevent element but so far my attempts have drawn a blank.

    Any assistance in this would be most appreciated and apologies if this has already been answered elsewhere and I simply haven't found it yet!

    Mat

    13 Mart 2012 Salı 15:23

Yanıtlar

  • Since I do not have that eventlog myself I cannot run the script here, but if you only want to display the 5th line you could just throw a counter in there:

    $entries = Get-EventLog 'Key Management Service' | ?  {$_.InstanceId -eq '12290'}
    foreach($entry in $entries)
        {
    $i = 0        
    ($entry.Message).Split(",") | % {$i++;if ($i -eq 5) {write-host $_} }
        }

    • Yanıt Olarak İşaretleyen MatF 13 Mart 2012 Salı 15:44
    13 Mart 2012 Salı 15:36
    Moderatör
  • This is all you need to do to get a specific element.

    $entry.Message).Split(",")[4]

    Just subscript the line and it will give you that element.


    ¯\_(ツ)_/¯

    • Yanıt Olarak İşaretleyen MatF 13 Mart 2012 Salı 17:07
    13 Mart 2012 Salı 16:19

Tüm Yanıtlar

  • Since I do not have that eventlog myself I cannot run the script here, but if you only want to display the 5th line you could just throw a counter in there:

    $entries = Get-EventLog 'Key Management Service' | ?  {$_.InstanceId -eq '12290'}
    foreach($entry in $entries)
        {
    $i = 0        
    ($entry.Message).Split(",") | % {$i++;if ($i -eq 5) {write-host $_} }
        }

    • Yanıt Olarak İşaretleyen MatF 13 Mart 2012 Salı 15:44
    13 Mart 2012 Salı 15:36
    Moderatör
  • Thanks Jaap.  Turns out that due to the formatting it was actually the 3rd proper line but changing ($i -eq 5) to ($i -eq 3) got it working.

    Much appreciated.

    13 Mart 2012 Salı 15:48
  • No problem, good luck scripting.
    13 Mart 2012 Salı 16:00
    Moderatör
  • This is all you need to do to get a specific element.

    $entry.Message).Split(",")[4]

    Just subscript the line and it will give you that element.


    ¯\_(ツ)_/¯

    • Yanıt Olarak İşaretleyen MatF 13 Mart 2012 Salı 17:07
    13 Mart 2012 Salı 16:19
  • Thanks jrv.  I like a solution even more when it requires even less typing!
    13 Mart 2012 Salı 17:07