Problem using variable as parameter in cmd-let in a script

This topic contains 6 replies, has 3 voices, and was last updated by  Michael Jacobsen 1 year, 9 months ago.

  • Author
    Posts
  • #35679

    Michael Jacobsen
    Participant

    Hi,
    Can someone help me please?

    I can't get this to Work:

    #1

    $strSearchBase = $strOU + $srtDomain

    $Computers = get-adcomputer -filter * -searchbase $strSearchBase | select dnshostname

    I get this error:
    get-adcomputer : Directory object not found

    But this Works perfectly:

    #2

    $strSearchBase = โ€œou=Database servere, ou=Domain Servere, dc=egedal, dc=egekom, dc=orgโ€

    $Computers = get-adcomputer -filter * -searchbase $strSearchBase | select dnshostname

    In #1 when I make Write-host $strSearchBase I get this in the console:

    ou=Domain Servere, ou=Database servere, dc=egedal, dc=egekom, dc=org

    So the two string should be tha same???

  • #35681

    Dave Wyatt
    Moderator

    Depends on what's in your $strOU and $srtDomain variables. (And is that a typo, supposed to be $strDomain?)

  • #35682

    Michael Jacobsen
    Participant

    @dave Wyatt

    $strSearchBase = $strOU + $srtDomain
    Write-host $strSearchBase

    ou=Domain Servere, ou=Database servere, dc=egedal, dc=egekom, dc=org

  • #35685

    Michael Jacobsen
    Participant

    Yes it's a typo, but this is not the cause of the problem ๐Ÿ™‚

  • #35688

    Michael Jacobsen
    Participant

    I still can't get it to work

  • #35711

    Aapeli Hietikko
    Participant

    Looks to be absolutely correct, if your variables are as follows. Remember the last , in $strOU.

    $strOU = 'ou=Domain Servere, ou=Database servere, '
    $srtDomain = 'dc=egedal, dc=egekom, dc=org'
    $strSearchBase = $strOU + $srtDomain
    

    You could also combine your variables with double quotes. That shouldn't make a difference but it looks nice and you can also drop that trailing space from $strOU.
    As if the spaces would even matter in this case.

    $strOU = 'ou=Domain Servere, ou=Database servere,'
    $srtDomain = 'dc=egedal, dc=egekom, dc=org'
    $strSearchBase = "$strOU $srtDomain"
    
  • #35738

    Michael Jacobsen
    Participant

    It's working now. I had accidently reversed the order of the OU's in the strOU string.

    Thanks for the input guys ๐Ÿ™‚

You must be logged in to reply to this topic.