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

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???

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

@Dave Wyatt

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

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

Yes it’s a typo, but this is not the cause of the problem :slight_smile:

I still can’t get it to work

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"

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

Thanks for the input guys :slight_smile: