5.6. Using new-object with COM ObjectsThere is one option on the new-object cmdlet that deserves further attention. Although most of the examples so far have focused on objects, classes, and all things related to the .NET Framework, the -ComObject option opens up a whole new world of possibilities via the Component Object Model (more commonly known as COM). new-object allows COM objects, which include ActiveX controls , to be used just like any other kind of object within MSH. Because many applications expose an object model for programmability, it is possible to use the familiar MSH cmdlets to interact with many different tools and applications that already exist on a machine. 5.6.1. How Do I Do That?Using new-object to instantiate a COM object is very similar to the process used for .NET classes, and it just requires the addition of the -ComObject option: MSH D:\MshScripts> $ie = new-object -ComObject "InternetExplorer.Application" There is nothing particularly special about the $ie variable aside from the fact that it's representing a type that we haven't used yet. get-member will quickly help us find out more information about the object model exposed by Internet Explorer: MSH D:\MshScripts> $ie | get-member -MemberType Method TypeName: System._ _ComObject#{d30c1661-cdaf-11d0-8a3e-00c04fc9e26e} Name MemberType Definition ---- ---------- ---------- ClientToWindow Method void ClientToWindow (int, int) ExecWB Method void ExecWB (OLECMDID, OLECMDEXECOPT, Variant, Var... GetProperty Method Variant GetProperty (string) GoBack Method void GoBack ( ) GoForward Method void GoForward ( ) GoHome Method void GoHome ( ) GoSearch Method void GoSearch ( ) Navigate Method void Navigate (string, Variant, Variant, Variant, ... Navigate2 Method void Navigate2 (Variant, Variant, Variant, Variant... PutProperty Method void PutProperty (string, Variant) QueryStatusWB Method OLECMDF QueryStatusWB (OLECMDID) Quit Method void Quit ( ) Refresh Method void Refresh ( ) Refresh2 Method void Refresh2 (Variant) ShowBrowserBar Method void ShowBrowserBar (Variant, Variant, Variant) Stop Method void Stop ( ) Let's try one of the overloaded Navigate methods, which takes a single URL as the target. We'll also set the Visible property to true so that we can actually see what is going on: MSH D:\MshScripts> $ie.Navigate("http://www.oreilly.com") MSH D:\MshScripts> $ie.Visible = $true At this point, an Internet Explorer window, already navigating its way over the Web toward the O'Reilly web site, should become visible. The Windows graphical shell (EXPLORER) also exposes several methods through a Shell.Application COM interface. We can use this interface to call up the folder browser: MSH D:\MshScripts> $ui = new-object -ComObject "Shell.Application" MSH D:\MshScripts> $path = $ui.BrowseForFolder(0, "Select a folder", 0, 0) At this point, the folder-selection dialog box (like the one shown in Figure 5-2) will appear and the script will wait until the dialog is dismissed. Figure 5-2. Browse For Folder dialog boxIf OK was selected, the $path variable will contain information relating to the selection. The full path of the selected folder can be found in the $path.Self.Path property. |