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

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:\Users\john.SAVILLTECH> $agent = new-object -com Agent.Control.2
PS C:\Users\john.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:\Users\john.SAVILLTECH> $null = $agent.connected = 1
PS C:\Users\john.SAVILLTECH> $null = $agent.Characters.Load("Merlin")
PS C:\Users\john.SAVILLTECH> $null = $merlin = $agent.Characters.Character("Merlin")
PS C:\Users\john.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:\Users\john.SAVILLTECH> $null = $merlin.MoveTo(250, 250, 1)
PS C:\Users\john.SAVILLTECH> $null = $merlin.Play("DoMagic1")
PS C:\Users\john.SAVILLTECH> $null = $merlin.Play("DoMagic2")
PS C:\Users\john.SAVILLTECH> $null = $merlin.Speak("NTFAQ rocks")
PS C:\Users\john.SAVILLTECH> $null = $merlin.think("Complete Guide to Windows Server 2008 is cool")
PS C:\Users\john.SAVILLTECH> $null = $merlin.Hide()
PS C:\Users\john.SAVILLTECH> $merlin = $null
PS C:\Users\john.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:\Users\john.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

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish