Changes

Jump to: navigation, search

Using COM with Windows PowerShell 1.0

8,837 bytes added, 14:58, 23 December 2008
Listing the Properties and Methods of a COM Object
Width Property int Width () {get} {set}
</pre>
 
== Interacting With COM Objects ==
 
With the information covered so far in this chapter, it is now possible to being working with COM objects. Clearly once an an instance of an object has been created, it is then possible to begin calling methods and setting properties on the object to make it perform tasks and behave in certain ways. As an example of this in action, we will created an instance of the ''InternetExplorer.Application'' object, make it visible on the Windows desktop and navigate to a specific URL:
 
<pre>
$iexplorer = new-object -com InternetExplorer.Application -strict # Create instance of IE
 
$iexplorer.Visible = $true # Make it visible on the desktop
 
$iexplorer.navigate2("http://www.techotopia.com") # Navigate to the Techotopia home page
</pre>
 
== Interacting with the Windows Shell ==
 
Windows PowerShell and COM provide a mechanism for interacting with various aspects of the Windows GUI through an object named ''Shell.Application''. By manipulating an instance of this object it is possible to perform such tasks as navigate the file system using Windows Explorer, launch control panel items and cascade and tile windows on the desktop.
 
As with other COM objects, instance of ''Shell.Application'' is instantiated using the ''new-object'' cmdlet as follows:
 
<pre>
$winshell = new-object -com Shell.Application
</pre>
 
Once the object is created a list of methods and properties may be obtained using ''get-member'' (gm):
 
<pre>
PS C:\tmp> $winshell | get-member
 
 
TypeName: System.__ComObject#{866738b9-6cf2-4de8-8767-f794ebe74f4e}
 
Name MemberType Definition
---- ---------- ----------
AddToRecent Method void AddToRecent (Variant, string)
BrowseForFolder Method Folder BrowseForFolder (int, string, int, Variant)
CanStartStopService Method Variant CanStartStopService (string)
CascadeWindows Method void CascadeWindows ()
ControlPanelItem Method void ControlPanelItem (string)
EjectPC Method void EjectPC ()
Explore Method void Explore (Variant)
ExplorerPolicy Method Variant ExplorerPolicy (string)
FileRun Method void FileRun ()
FindComputer Method void FindComputer ()
FindFiles Method void FindFiles ()
FindPrinter Method void FindPrinter (string, string, string)
GetSetting Method bool GetSetting (int)
GetSystemInformation Method Variant GetSystemInformation (string)
Help Method void Help ()
IsRestricted Method int IsRestricted (string, string)
IsServiceRunning Method Variant IsServiceRunning (string)
MinimizeAll Method void MinimizeAll ()
NameSpace Method Folder NameSpace (Variant)
Open Method void Open (Variant)
RefreshMenu Method void RefreshMenu ()
ServiceStart Method Variant ServiceStart (string, Variant)
ServiceStop Method Variant ServiceStop (string, Variant)
SetTime Method void SetTime ()
ShellExecute Method void ShellExecute (string, Variant, Variant, Variant, Variant)
ShowBrowserBar Method Variant ShowBrowserBar (string, Variant)
ShutdownWindows Method void ShutdownWindows ()
Suspend Method void Suspend ()
TileHorizontally Method void TileHorizontally ()
TileVertically Method void TileVertically ()
ToggleDesktop Method void ToggleDesktop ()
TrayProperties Method void TrayProperties ()
UndoMinimizeALL Method void UndoMinimizeALL ()
Windows Method IDispatch Windows ()
WindowsSecurity Method void WindowsSecurity ()
WindowSwitcher Method void WindowSwitcher ()
Application Property IDispatch Application () {get}
Parent Property IDispatch Parent () {get}
</pre>
 
Now that we have the object and a list of methods and properties it is time to start performing some tasks. For example, to invoke Windows Explorer initialized to a specified folder:
 
<pre>
PS C:\tmp> $winshell.explore("C:\tmp")
</pre>
 
To cascade all the windows on the desktop:
 
<pre>
PS C:\tmp> $winshell.cascadewindows()
</pre>
 
Similarly, to tile or minimize all windows:
 
<pre>
PS C:\tmp> $winshell.tilevertically()
 
PS C:\tmp> $winshell.minimizeall()
</pre>
 
In order to run a control panel item, the ''ControlPanelItem()'' method of the object needs to be called with the ''.cpl'' file of the required item passed as an argument. A full list of items can be obtained from with the Windows PowerShell environment by issuing the following command:
 
<pre>
PS C:\tmp> dir $env:windir\system32 -recurse -include *.cpl
 
 
Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows\system32
 
 
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 1/18/2008 11:32 PM 1122304 appwiz.cpl
-a--- 1/18/2008 11:32 PM 990208 bthprops.cpl
-a--- 1/18/2008 11:32 PM 368640 desk.cpl
-a--- 1/18/2008 11:32 PM 2249216 Firewall.cpl
-a--- 11/2/2006 2:44 AM 183296 hdwwiz.cpl
-a--- 1/18/2008 11:32 PM 1827840 inetcpl.cpl
-a--- 1/18/2008 11:32 PM 337408 intl.cpl
-a--- 11/2/2006 2:44 AM 418816 irprops.cpl
-a--- 11/2/2006 2:44 AM 484864 main.cpl
-a--- 1/18/2008 11:32 PM 1102848 mmsys.cpl
-a--- 11/2/2006 2:44 AM 164864 ncpa.cpl
-a--- 1/18/2008 11:32 PM 163328 powercfg.cpl
-a--- 1/18/2008 11:32 PM 242688 sysdm.cpl
-a--- 11/2/2006 2:44 AM 106496 telephon.cpl
-a--- 1/18/2008 11:32 PM 714240 timedate.cpl
</pre>
 
As an example, the following command will display the Windows Display control panel window:
 
<pre>
PS C:\tmp> $winshell.controlpanelitem("desk.cpl")
</pre>
 
== Using the WScript.Shell Class ==
 
The WScript.Shell class provides a number of useful utilities that greatly extend the list of tasks that can be performed using Windows PowerShell and COM such as running applications, sending keystrokes to running applications and displaying popup message dialogs.
 
A WScript.Shell instance is created and a list of available methods and properties displayed as follows:
 
<pre>
PS C:\tmp> $wscript = new-object -com wscript.shell
PS C:\tmp> $wscript | gm
 
 
TypeName: System.__ComObject#{41904400-be18-11d3-a28b-00104bd35090}
 
Name MemberType Definition
---- ---------- ----------
AppActivate Method bool AppActivate (Variant, Variant)
CreateShortcut Method IDispatch CreateShortcut (string)
Exec Method IWshExec Exec (string)
ExpandEnvironmentStrings Method string ExpandEnvironmentStrings (string)
LogEvent Method bool LogEvent (Variant, string, string)
Popup Method int Popup (string, Variant, Variant, Variant)
RegDelete Method void RegDelete (string)
RegRead Method Variant RegRead (string)
RegWrite Method void RegWrite (string, Variant, Variant)
Run Method int Run (string, Variant, Variant)
SendKeys Method void SendKeys (string, Variant)
Environment ParameterizedProperty IWshEnvironment Environment (Variant) {get}
CurrentDirectory Property string CurrentDirectory () {get} {set}
SpecialFolders Property IWshCollection SpecialFolders () {get}
</pre>
 
The power of the ''WScript.Shell'' class is best demonstrated through a simple example. The following script launches the Notepad application, waits until the application has started to make it the active application (such that the focus is on the application) and then sends some text to the application:
 
<pre>
$wscript.run("notepad")
 
while ($wscript.appactivate("notepad") -ne $true)
{
"Waiting for app to start...."
}
 
$wscript.sendkeys("Hello From Windows PowerShell and COM")
</pre>
 
== Summary ==
 
In this chapter we have taken a tour of the basics of using Windows PowerShell in conjunction with the Component Object Model (COM). As with .NET, it should be evident that the availability of COM to the Windows PowerShell developer considerably enhances range of options for performing tasks within the contexts of Windows. Entire books could, and indeed have, been written on COM, so take what you learned in this chapter, grap a COM book from the library and unleash the power that is available to you as a Windows PowerShell programmer.

Navigation menu