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

Welcome Forums General PowerShell Q&A Problem using variable as parameter in cmd-let in a script

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
2 years, 9 months ago.

  • Author
    Posts
  • #35679

    Participant
    Points: 0
    Rank: Member

    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

    Member
    Points: 0
    Rank: Member

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

  • #35682

    Participant
    Points: 0
    Rank: Member

    @dave Wyatt

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

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

  • #35685

    Participant
    Points: 0
    Rank: Member

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

  • #35688

    Participant
    Points: 0
    Rank: Member

    I still can't get it to work

  • #35711

    Participant
    Points: 1
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

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

    Thanks for the input guys πŸ™‚

The topic ‘Problem using variable as parameter in cmd-let in a script’ is closed to new replies.