locked
expanding variables as command parameters RRS feed

  • Question

  • Greetings,

    This works:

    $ldapfilter = '(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(|(dstAccountType=staff)(dstAccountType=contractor)))'
    $users = (\\pstools\joeware$\adfind.exe -csv -csvdelim "|" -f $ldapfilter givenname sn mail samaccountname dstaccounttype | convertfrom-csv -Delimiter '|') 

    Id like to store the output attributes in a variable like this:

    $ldapfilter = '(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(|(dstAccountType=staff)(dstAccountType=contractor)))'
    $Attributes = 'givenname sn mail samaccountname dstaccounttype'
    $users = (\\pstools\joeware$\adfind.exe -csv -csvdelim "|" -f $ldapfilter $Attributes | convertfrom-csv -Delimiter '|') 

    but it doesnt work.

    What's the best way to do this please?

    Cheers

    David Z

    Tuesday, September 18, 2018 10:15 PM

Answers

  • Got it!

    $cmd = '\\pstools\joeware$\adfind.exe'
    $arg1 = '-csv'
    $arg2 = '-f'
    $ldapfilter = '(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(|(dstAccountType=staff)(dstAccountType=contractor)))'
    $Attributes = @('givenname','sn','mail','samaccountname','dstaccounttype')
    $users = & $cmd $arg1 $arg2 $ldapfilter $Attributes | convertfrom-csv

    • Marked as answer by David Zemdegs Wednesday, September 19, 2018 3:24 AM
    Wednesday, September 19, 2018 3:24 AM

All replies

  • And you're using adfind.exe instead of the AD cmdlets because...?

    -- Bill Stewart [Bill_Stewart]

    Wednesday, September 19, 2018 12:43 AM
  • its waaaaay quicker

    and PS cant handle custom AD attributes all the time in a consistent fashion.

    Wednesday, September 19, 2018 12:46 AM
  • $results = & "\\pstools\joeware$\adfind.exe -csv -f $ldapfilter $Attributes"
    $results | ConvertFrom-Csv


    \_(ツ)_/

    Wednesday, September 19, 2018 12:52 AM
  • Thanks but:

    & : The term '\\pstools\joeware$\adfind.exe -csv -f (&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(|(dstAccountType=staff)(dstAccountType=contractor))) givenname sn mail samaccountname dstaccounttype' is not recognized as the name of a cmdlet, function, script file, or 
    operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:3 char:12
    + $users = & "\\pstools\joeware$\adfind.exe -csv -f $ldapfilter $Attrib ...
    +            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (\\pstools\joewa... dstaccounttype:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
     

    Wednesday, September 19, 2018 12:57 AM
  • You will likely have to do this:

    $ldapfilter = '"(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(|(dstAccountType=staff)(dstAccountType=contractor)))"'
    

    Note the added quotes.


    \_(ツ)_/

    Wednesday, September 19, 2018 1:00 AM
  • damn - I get this:

    $ldapfilter = '"(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(|(dstAccountType=staff)(dstAccountType=contractor)))"'
    $Attributes = 'givenname sn mail samaccountname dstaccounttype'
    $users = & "\\pstools\joeware$\adfind.exe -csv -f $ldapfilter $Attributes"

    & : The term '\\pstools\joeware$\adfind.exe -csv -f "(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(|(dstAccountType=staff)(dstAccountType=contractor)))" givenname sn mail samaccountname dstaccounttype' is not recognized as the name of a cmdlet, function, script file, 
    or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:3 char:12
    + $users = & "\\pstools\joeware$\adfind.exe -csv -f $ldapfilter $Attrib ...
    +            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (\\pstools\joewa... dstaccounttype:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Wednesday, September 19, 2018 1:44 AM
  • If the following fails you have other issues:

    & "\\pstools\joeware$\adfind.exe"


    \_(ツ)_/

    Wednesday, September 19, 2018 1:51 AM
  • Now that on its own works.

    And so does what I posted first:

    $users = (\\pstools\joeware$\adfind.exe -csv -csvdelim "|" -f $ldapfilter givenname sn mail samaccountname dstaccounttype | convertfrom-csv -Delimiter '|') 

    Wednesday, September 19, 2018 2:15 AM
  • If your original code works then what is your issue?


    \_(ツ)_/

    Wednesday, September 19, 2018 2:20 AM
  • I wanted to 'variablise' the output attributes which doesnt work: e.g.

    $ldapfilter = '(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(|(dstAccountType=staff)(dstAccountType=contractor)))'
    $Attributes = 'givenname sn mail samaccountname dstaccounttype'
    $users = (\\pstools\joeware$\adfind.exe -csv -csvdelim "|" -f $ldapfilter $Attributes | convertfrom-csv -Delimiter '|') 

    Wednesday, September 19, 2018 3:09 AM
  • I wanted to 'variablise' the output attributes which doesnt work: e.g.

    What does that mean. The output is in a variable. It is now a CSV derived custom object collection.


    \_(ツ)_/

    Wednesday, September 19, 2018 3:16 AM
  • Got it!

    $cmd = '\\pstools\joeware$\adfind.exe'
    $arg1 = '-csv'
    $arg2 = '-f'
    $ldapfilter = '(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(|(dstAccountType=staff)(dstAccountType=contractor)))'
    $Attributes = @('givenname','sn','mail','samaccountname','dstaccounttype')
    $users = & $cmd $arg1 $arg2 $ldapfilter $Attributes | convertfrom-csv

    • Marked as answer by David Zemdegs Wednesday, September 19, 2018 3:24 AM
    Wednesday, September 19, 2018 3:24 AM