none
Help with POWERSHELL where-object data extraction...

    질문

  • 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
    2012년 3월 19일 월요일 오후 2:41

답변

  • When you match a single item to an array, you need to put the array on the left-hand side:

    C:\> $col = 'abc','bcd','cde'
    PS C:\> 'b' -match $col
    False
    PS C:\> $col -match 'b'
    abc
    bcd

    $log -match $_.DisplayName


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • 답변으로 제안됨 Bigteddy 2012년 3월 19일 월요일 오후 3:04
    • 답변으로 표시됨 EdShep 2012년 3월 19일 월요일 오후 4:36
    2012년 3월 19일 월요일 오후 2:52

모든 응답

  • When you match a single item to an array, you need to put the array on the left-hand side:

    C:\> $col = 'abc','bcd','cde'
    PS C:\> 'b' -match $col
    False
    PS C:\> $col -match 'b'
    abc
    bcd

    $log -match $_.DisplayName


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • 답변으로 제안됨 Bigteddy 2012년 3월 19일 월요일 오후 3:04
    • 답변으로 표시됨 EdShep 2012년 3월 19일 월요일 오후 4:36
    2012년 3월 19일 월요일 오후 2:52
  • Geoff Guynn @ powershellcommunity.org suggested this... which worked

    thought I would post for anyone else struggling on the same

    looks like the same solution as mjolinor also...

    So thanks goes to you also for a prompt response.

    **********

    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 $_}

    2012년 3월 19일 월요일 오후 4:36