Scripting Console Operations

In some cases, creating a script to simulate SMS operations that are usually done by using the SMS Administrator console might be difficult or even impossible. For example, conventional SMS scripts cannot set account details. In such cases, an alternative is to script the SMS Administrator console itself.

Microsoft Management Console (MMC) 2.0 includes a scripting object model. This object model can be used to control most of the SMS Administrator console functions. Any functions that cannot be controlled by the MMC 2.0 scripting object model can be controlled by using the WSH SendKeys object, which simulates keyboard entries. If the function can be controlled from the keyboard, SendKeys can control it. SendKeys can be used to control most of the SMS Administrator console, but the MMC 2.0 scripting object model is generally more reliable. If anything causes the Windows focus to be shifted from the SMS Administrator console, SendKeys can be directed to the wrong application.

MMC 2.0 is including with Windows XP and the operating systems in the Windows Server 2003 family. You must run MMC 2.0-based scripts from an SMS Administrator console on a computer running Windows XP or the operating systems in the Windows Server 2003 family.

Sample C.32 shows using the MMC 2.0 scripting object model and SendKeys to control the SMS Administrator console. Sample C.32 opens the SMS Administrator console to the site's addresses and then sets them all to use the same predefined account and password. For obvious security reasons, you need to ensure that only authorized staff can read any script that contains account details.

Sample C.32 SetAddressAccount.vbs - sets address security details

Set mmc = WScript.CreateObject( "MMC20.Application.1" )
mmc.Load ("\sms\bin\i386\sms.msc")
mmc.Show
Set doc = mmc.Document
Set namespace = doc.ScopeNamespace
Set View = doc.Views.Item(1)
mmc.UserControl = 1   'so the console doesn't close prematurely
set WshShell = WScript.CreateObject("WScript.Shell")
Set sms = namespace.GetRoot
Set Database = namespace.GetChild(sms)
While Left(Database.Name, 7) = "Connect": WScript.Sleep 1000: Wend
namespace.Expand (Database)
WScript.Sleep 2000
Set extra = namespace.GetChild(Database)
namespace.Expand (extra)
WScript.Sleep 6000
Set hierarchy = namespace.GetChild(extra)
namespace.Expand (hierarchy)
WScript.Sleep 8000
Set site = namespace.GetChild(hierarchy)
namespace.Expand (site)
WScript.Sleep 2000
Set settings = namespace.GetChild(site)
namespace.Expand (settings)
WScript.Sleep 2000
Set address = namespace.GetChild(settings)
View.ActiveScopeNode = address
WScript.Sleep 5000
'for all addresses in the console
for i=1 to View.ListItems.Count
   'next address
   Set objNode1 = View.ListItems(i)
   View.Select objNode1
   SetAddressAccount  'they're all set to the same username and password
next
WScript.Sleep 1000
mmc.quit
WScript.Sleep 500
WshShell.SendKeys "N"'------------------------------------------------------------
Sub SetAddressAccount
View.ExecuteSelectionMenuItem ("Properties")
WScript.Sleep 500
WshShell.SendKeys "%E"              'alt E - Set... button
WScript.Sleep 500
WshShell.SendKeys "testdomain\testuser"      'username
WScript.Sleep 500
WshShell.SendKeys "{TAB}"WScript.Sleep 500
WshShell.SendKeys "password"              'password
WScript.Sleep 500
WshShell.SendKeys "{TAB}"WScript.Sleep 500
WshShell.SendKeys "password"              'password again
WScript.Sleep 500
WshShell.SendKeys "{ENTER}"            'OK password dialog box done
WScript.Sleep 500
WshShell.SendKeys "{ENTER}"            'OK address dialog box done
End Sub
For More Information

Did you find this information useful? Please send your suggestions and comments about the documentation to smsdocs@microsoft.com.

Show: