header1   header
header
header : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012

PowerShellCommunity.org is moving!  This community software, and the hardware that it sits on, are no longer serving the purposes of this community.  As a result, we have decided to move this community to a new home at PowerShell.org.  PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site.  We've already started getting some great questions from members of the community over there so please, come on over and join us!

While we are going through this transition, this site will remain up for the short term.  New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.

[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.

If you have any questions, please let us know on the PowerShell.org site.

Thank you,

Kirk "Poshoholic" Munro

 
Help with where-object / foreach PLEASE help
Last Post 19 Mar 2012 08:42 AM by Geoff Guynn. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Resolved
EdShepUser is Offline
New Member
New Member
Posts:3
Avatar

--
19 Mar 2012 06:14 AM

    Hi All,

    Firstly I appologise if this is really easy or I explain it really bad, but I am new to all this powershell / database gubbins - using for about 1 week...

    I am trying to pull out the call logs - user comments, analyst comments etc from a table of a call logging system (Service Centre System Manager).  I can find all the details i need for the call and can get it to pull out the information when there is a single log attached, but when there are multiple logs the procedure fails.
     
    example 1- 

          get-scsmobject -class $actionlogclass -computername d-sm01| where-object {$_.DisplayName -match "ef4982e9-4f95-4d11-9c21-      b4d27ccca3b1"} |format-list Description, EnteredBy, EnteredDate

    returns

          Description : Please can you look into this incident ASAP. Thanks 
          EnteredBy : Ashley Elms 
          EnteredDate : 31/01/2012 15:53:51
          

    Where there are multiple logs attached to a single call, I have found all the ID's for the logs and defined them as a variable $Log.
     
    So $Log looks like this

    ef4982e9-4f95-4d11-9c21-b4d27ccca3b1
    bd4ad5bc-367f-4ad5-beb3-7806daa116d6
    37d4edf3-d31e-4e83-9150-4731effee75d

    and I was hoping I could have simply replaced the specific ID on the above example with $Log

    example2

          get-scsmobject -class $actionlogclass -computername d-sm01| where-object {$_.DisplayName -match $log} |format-list Description, 
          EnteredBy, EnteredDate


    returns nothing


    However, if $log = "ef4982e9-4f95-4d11-9c21-b4d27ccca3b1"  - the value specified in example1, it returns as expected... 

          Description : Please can you look into this incident ASAP. Thanks 
          EnteredBy : Ashley Elms 
          EnteredDate : 31/01/2012 15:53:51

    therefore I know it works with single values

    So,

    how do iget it to loop through the $log variable to return all the deteils of the call log we want?

    Any help is greatly apprecaited

    Thanks

    Ed


    Geoff GuynnUser is Offline
    Basic Member
    Basic Member
    Posts:140
    Avatar

    --
    19 Mar 2012 08:16 AM
    • Accepted Answer
    Try this instead.

    get-scsmobject -class $actionlogclass -computername d-sm01| Where-Object {$log -contains $_.DisplayName} | format-list Description, EnteredBy, EnteredDate




    Here's a greatly simplified example of what's going on here. (Using the alias ? in place of Where-Object and gettype() to show that this works on single and arrays.)

    $array1 = "123", "456", "789"
    $array2 = "987", "654", "321", "123"

    Write-Host "`r`nArray1 is a(n): $($Array1.GetType().Name)"

    $array1 | ? {$array2 -contains $_}

    $array1 = "123"

    Write-Host "`r`nArray1 is a(n): $($Array1.GetType().Name)"

    $array1 | ? {$array2 -contains $_}
    EdShepUser is Offline
    New Member
    New Member
    Posts:3
    Avatar

    --
    19 Mar 2012 08:28 AM
    Geoff,

    Thanks! I think I could kiss you. I knew I had to swap it around but was trying to put the $ log at the start... didnt think to swap around the bits within the {}.

    Excellent Thanks

    and thanks for the numbties explanation... having to crack through this with no knowledge or training in these sorts of things is just mind blowing to start with!

    Google might be my friend, but forums are quickly becoming my Bezzie Mate!

    thanks again Geoff!

    (is it sad to get so excited about such things??)
    Geoff GuynnUser is Offline
    Basic Member
    Basic Member
    Posts:140
    Avatar

    --
    19 Mar 2012 08:42 AM
    Not a bit, I think all of us have beat the wall over something that seems so simple in retrospect. It's a great feeling to finally put it all together and watch our baby take flight.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Many thanks to our original sponsors: Quest Software • SAPIEN Technologies • Compellent • Microsoft footer
    footer   footer