Scripting COM Objects with PowerShellIntroduction to Scripting - COM Objects with PowerShellComObject, or plain COM, increases the range of PowerShell activities. One way of looking at COM objects is as a mechanism for PowerShell to launch programs, for example, mimicking the RUN command. Another way of looking at ComObjects is performing the role previously undertaken by VBScript. For both those tasks, scripting with COM objects gives you a rich selection of options. The bonus of using PowerShell rather than VBScript is that you need fewer commands. Topics for COM Objects
New-Object -comThe secret of manipulating COM objects is starting with the command: New-Object -COM. What comes next depends on which type of object you need. Here are examples of creating, then manipulating ComObject with $variables:- Instructions and Assumptions
1) MapNetworkDrive $net = New-Object -com WScript.Network Note 1: Naturally, you need to amend \\ server\share to the name of a real UNC share on your network. Note 2: To see if your PowerShell script performs as planned, launch Windows Explorer. See here for a more detailed MapNetworkDrive example. 2) Internet Explorer $ie = New-Object -ComObject InternetExplorer.Application; $ie.visible=$true Note 1: Incidentally, -com and -ComObject appear to be interchangeable. New-Object -com | get-MemberLet us investigate the properties of the -com object. At first, get-Member appears not work, even help seems unsupportive. Fortunately, all that is missing is the
name of the -com object that you wish to research. For example: Name MemberType (Method, Property) Selection of -com ApplicationsCreate objects with new-object -com xyz.Application 1) Shell.Application $shell = new-object -comObject Shell.Application See more on Shell.Application 2) WScript.Shell $WSH = New-Object -com WScript.Shell Instead of "Explorer", substitute "Excel", "Word", "Calc" or any other registered application. 3) MSScriptControl.ScriptControl $VBScript = new-object -com MSScriptControl.ScriptControl 4) InputBox() Example $VBScript = new-object -com MSScriptControl.ScriptControl Note: This is a complex example because it creates a function, which then does useful work in creating an inputbox(). 5) Active Directory - DirectoryServices.DirectoryEntry Summary of -COM objectsThe ComObject family of commands add important capabilities to PowerShell. For example, creating network objects means that you don't have to revert to VBScript when you need to map network drives. Another way of looking at the -ComObject command is as a PowerShell method of accessing the Run dialog box programmatically. See more PowerShell real life tasks• PowerShell Home • Real life tasks • IpConfig • Exchange • Com objects • Services • Syntax Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
*
|
||||||