I'm coming from a *nix scripting background and I'm a total newbie to powershell and Windows admin in general. I'm trying to write a script that will check the SmartHost value on a collection of exchange/IIS smtp virtual hosts. I'm trying to figure out how to insert the looped variable into the ADSI query string but the + operator doesn't do the trick:
$hosts = @("host1","host2")
foreach ($hostname in $hosts) {
$SMTPSvc = [ADSI]'IIS://' + $hostname + '/smtpsvc/1'
echo $SMTPSvc.SmartHost
}
Using the + with single or double quotes gives me this error:
Method invocation failed because [System.DirectoryServices.DirectoryEntry] does not contain a method named 'op_Addition'.
At line:3 char:1
+ $SMTPSvc = [ADSI]'IIS://' + $hostname + '/smtpsvc/1'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
What would be the proper or preferred way to insert the looped host value into the ADSI query string?