Pass a variable to the MailboxDisplayName
I have a script that I'm trying to pass a variable into the SQL command and I can't seem to get it read in properly. Am I missing something? I have even tried to set it as basic as possible and when using a variable it just doesn't seem to work.See code below.On Error Resume Next
strComputer = "Exchange1"strName = "Smith, John"Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & _ "\ROOT\MicrosoftExchangeV2")wscript.echo strName ' This verifies the strName variableSet colItems = objWMIService.ExecQuery _("Select * from Exchange_Mailbox where MailboxDisplayName = 'Smith, John'") ' This works'("Select * from Exchange_Mailbox where MailboxDisplayName = '"" & strName & ""'") This line does not work
Wscript.Echo "LegacyDN, Mailbox Display Name, homeMDB"
For Each objItem in colItems Wscript.Echo objItem.LegacyDN & "~" & objItem.MailboxDisplayName & "~" & objItem.StoreNameNext
May 18th, 2009 7:26pm
strName variable requires only one double quote wounded, not two. Second double quote was ending the Select * command and which was causing syntax error...
Below script should work...
strComputer = "ESS-Exch301"
strName = "Ex2003 Admin"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & _
"\ROOT\MicrosoftExchangeV2")
'wscript.echo strName ' This verifies the strName variable
Set colItems = objWMIService.ExecQuery _
("Select * from Exchange_Mailbox where MailboxDisplayName = '"& strName &"'")
Wscript.Echo "LegacyDN, Mailbox Display Name, homeMDB"
For Each objItem in colItems
Wscript.Echo objItem.LegacyDN & "~" & objItem.MailboxDisplayName & "~" & objItem.StoreName
Next
Amit Tank | MVP Exchange Server | MCITP:EMA MCSA:M | http://ExchangeShare.WordPress.com
Free Windows Admin Tool Kit Click here and download it now
May 18th, 2009 8:31pm