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

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Michael Jacobsen Michael Jacobsen 9 months, 2 weeks ago.

  • Author
    Posts
  • #35679
    Profile photo of Michael Jacobsen
    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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

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

  • #35682
    Profile photo of Michael Jacobsen
    Michael Jacobsen
    Participant

    @Dave Wyatt

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

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

  • #35685
    Profile photo of Michael Jacobsen
    Michael Jacobsen
    Participant

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

  • #35688
    Profile photo of Michael Jacobsen
    Michael Jacobsen
    Participant

    I still can't get it to work

  • #35711
    Profile photo of Aapeli Hietikko
    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
    Profile photo of Michael Jacobsen
    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.