locked
Please help me understand the code line RRS feed

  • Question

  • I need help as soon as possible. Can somebody please explain in the human language what this power-shell line means, so I could write it in C#?

    Please help as soon as possible

    $visitor = $roles | ? { `
        ($groups | ? { $_.Title -like '*Visitors'} | % {$_.Id}) -Contains `
        $_.PrincipalId } | select -ExpandProperty PrincipalId

    Wednesday, January 10, 2018 5:12 PM

Answers

  • "|" is a pipe and defines a pipeline.

    If you are a trained and experienced C# programmer then this code would be a cinch to decode.  I am a C#/C++ (other) programmer and it makes sense to me.

    If it were written correctly and formatted correctly then it might be easy to read.

    This is what it looks like.

    $visitor = $roles | 
        Where-Object {
            (
                $groups | 
                Where-Object {
                    $_.Title -like '*Visitors' 
                } | 
                ForEach-Object { 
                    $_.Id 
                }
           ) -Contains $_.PrincipalId
        } | 
        Select-Object -ExpandProperty PrincipalId
    
    
    

    You can use Linq to understand and convert this code.


    \_(ツ)_/

    Wednesday, January 10, 2018 5:51 PM

All replies

  • Here is a full explanation: Learn PowerShell  

    Please explain why you need to know this?  Have you tried asking the author of the script for help?

    Since the script is incomplete there is really no way to explain what it is attempting to do.  The code is also written badly.

    Start by learning PowerShell syntax then analyze each element to see what it is doing.


    \_(ツ)_/

    Wednesday, January 10, 2018 5:32 PM
  • Sir, I am a C# developer, I have no the author of this code at my disposal. I was to implement the functionality in C# where the 'Visitors' list had to be used.

    I know that '|' means 'foreach' and I know that '?' means 'where', but this line of code i sway too complex, so I am not sure how to interpret it properly

    Does it represent a nested loop? Why part of it in the back quotes?

    Once again, I am a C# developer, not a PowerShell developer and hard pressed to finish the coding today; thus, I was asking for a friendly explanation of this line of code

    Thank you very much for your generous input

    Wednesday, January 10, 2018 5:43 PM
  • "|" is a pipe and defines a pipeline.

    If you are a trained and experienced C# programmer then this code would be a cinch to decode.  I am a C#/C++ (other) programmer and it makes sense to me.

    If it were written correctly and formatted correctly then it might be easy to read.

    This is what it looks like.

    $visitor = $roles | 
        Where-Object {
            (
                $groups | 
                Where-Object {
                    $_.Title -like '*Visitors' 
                } | 
                ForEach-Object { 
                    $_.Id 
                }
           ) -Contains $_.PrincipalId
        } | 
        Select-Object -ExpandProperty PrincipalId
    
    
    

    You can use Linq to understand and convert this code.


    \_(ツ)_/

    Wednesday, January 10, 2018 5:51 PM
  • Thank you very much for your help
    Wednesday, January 10, 2018 6:00 PM
  • I have translated many things in programming and in life.  The first rule of translating is that you must have a proficient understanding of both systems (languages) in order to translate.

    \_(ツ)_/

    Wednesday, January 10, 2018 6:12 PM
  • It's like a somewhat convoluted database query.  It's looking for the role principalid's that 
    match the id's of groups with titles like visitors.  I rearranged it a little this way.  I don't like using the % abbreviation for foreach and the ? abbreviation for where.  The backtick for continuation is rarely necessary.  Normally, you would join the two tables in sql, then filter for visitors.  There's no object join in powershell, although plenty of people have written scripts that do it.


    $roles = [pscustomobject]@{name='j';principalid=1}
    $groups = [pscustomobject]@{name='s';title='myvisitors';id=1}

    $
    groupVisitorIDs = $groups | where Title -like *Visitors | select -expand Id
    $visitors = $roles |
    where { $groupVisitorIDs -Contains $_.PrincipalId } |
    select -Expand PrincipalId
    $visitors

    1




    • Edited by JS2010 Friday, January 12, 2018 7:38 PM
    Wednesday, January 10, 2018 7:09 PM