Q. How do I talk to COM objects or MSAgents in PowerShell?

John Savill

May 1, 2008

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. The Microsoft Agents are just COM objects, which means we can hook into them from anything that supports COM, which includes PowerShell. Run the following commands to see Merlin in action:

PS C:Usersjohn.SAVILLTECH> $agent = new-object -com Agent.Control.2
PS C:Usersjohn.SAVILLTECH> $agent | get-member
TypeName: System.__ComObject#{8563ff20-8ecc-11d1-b9b4-00c04fd97575}

Name MemberType Definition
---- ---------- ----------
ShowDefaultCharacterProperties Method void ShowDefaultCharacterProperties (Variant, Variant)
AudioOutput Property IAgentCtlAudioObjectEx AudioOutput () {get}
Characters Property IAgentCtlCharacters Characters () {get}
CommandsWindow Property IAgentCtlCommandsWindow CommandsWindow () {get}
Connected Property bool Connected () {get} {set}
PropertySheet Property IAgentCtlPropertySheet PropertySheet () {get}
RaiseRequestErrors Property bool RaiseRequestErrors () {get} {set}
SpeechInput Property IAgentCtlSpeechInput SpeechInput () {get}
Suspended Property bool Suspended () {get}


PS C:Usersjohn.SAVILLTECH> $null = $agent.connected = 1
PS C:Usersjohn.SAVILLTECH> $null = $agent.Characters.Load("Merlin")
PS C:Usersjohn.SAVILLTECH> $null = $merlin = $agent.Characters.Character("Merlin")
PS C:Usersjohn.SAVILLTECH> $null = $merlin.show()
At this point Merlin is visible and you can drag him around as shown in the figure:
http://www.windowsitpro.com/content/content/99026/powershellagent.jpg
You can then have a little fun using the following code:
PS C:Usersjohn.SAVILLTECH> $null = $merlin.MoveTo(250, 250, 1)
PS C:Usersjohn.SAVILLTECH> $null = $merlin.Play("DoMagic1")
PS C:Usersjohn.SAVILLTECH> $null = $merlin.Play("DoMagic2")
PS C:Usersjohn.SAVILLTECH> $null = $merlin.Speak("NTFAQ rocks")
PS C:Usersjohn.SAVILLTECH> $null = $merlin.think("Complete Guide to Windows Server 2008 is cool")
PS C:Usersjohn.SAVILLTECH> $null = $merlin.Hide()
PS C:Usersjohn.SAVILLTECH> $merlin = $null
PS C:Usersjohn.SAVILLTECH> $agent = $null

You can also pipe information to an agent. For example, the agent could speak out alerts or output. For example, to find and speak the names of all processes with more than 1000 handles, use this command:
PS C:Usersjohn.SAVILLTECH> $null = Get-process | Where { $_.handles -gt 1000 } | sort handles | foreach {$merlin.speak($_.Proc
essName + " has " + $_.handles + " handles.")}

See the Figure to see how this works.

http://www.windowsitpro.com/content/content/99026/powershellagenhandles.jpg

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like