Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



Scripting With PowerShell COM Objects

Introduction to Scripting COM Objects with PowerShell

ComObject, 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 PowerShell COM Objects

 ♣

PowerShell New-Object -com

The 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. If necessary get your copy of PowerShell and .Net Framework
  2. Launch Windows PowerShell
  3. Type these commands at the PS> Prompt (or copy and paste my examples)

1) MapNetworkDrive

# PowerShell ComObject and MapNetworkDrive
$net = New-Object -comObject WScript.Network
$net.mapnetworkdrive("Y:","\\server\share")

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

#ComObject to launch the Internet Explorer application.
Clear-Host
$ie = New-Object -ComObject InternetExplorer.Application; $ie.visible=$true

Note 1: Incidentally, -com and -ComObject appear to be interchangeable.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v10

Solarwinds' Orion performance monitor will help you discover what's happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

Perhaps the NPM's best feature is the way it suggests solutions to network problems.  Its second best feature is the ability to monitor the health of individual VMWare virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you take advantage of Solarwinds' offer.

Download a free trial of the Network Performance Monitor.

New-Object -com | Get-Member

Let 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:

#PowerShell New-Object -com properties
New-Object -com wscript.network | Get-Member.

Result:

Name       MemberType (Method, Property)
----         ---------- ----------
AddPrinterConnection Method
AddWindowsPrinterConnection Method
EnumNetworkDrives Method
MapNetworkDrive Method
RemoveNetworkDrive Method
SetDefaultPrinter Method
ComputerName Property
Organization Property
Site Property string
UserDomain Property
UserName Property
UserProfile Property

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.  Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server.

Download your free copy of WMI Monitor

Selection of -com Applications

Create objects with new-object -com xyz.Application

1) Shell.Application
For example:

$shell = new-object -comObject Shell.Application  See more on Shell.Application

2) Alternative technique Invoke-Expression

Before you go any further with -ComObject and cmd.exe you may wish to try invoke-Expression.

3) WScript.Shell
For example:

$WSH = New-Object -com WScript.Shell
$WSH.Run("Explorer.exe")

Instead of "Explorer", substitute "Excel", "Word", "Calc" or any other registered application.

4) MSScriptControl.ScriptControl
VBscript example:

$VBScript = new-object -com MSScriptControl.ScriptControl
$VBScript.language = "vbscript"
$VBScript | Get-Member

5) InputBox() Example

$VBScript = new-object -com MSScriptControl.ScriptControl
$VBScript.language = "vbscript"
$VBScript.addcode("function getInput() getInput = inputbox(`"Guy Says Hello`",`"Guy's box`") End Function" )
$Input = $VBScript.eval("getInput")

Note: This is a complex example because it creates a function, which then does useful work in creating an inputbox().

6) Active Directory - DirectoryServices.DirectoryEntry

Guy Recommends:  Solarwinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

Summary of -COM objects

The 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.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials:

PowerShell Home   •Shell Application  • New-Object   • PowerShell create shortcut

PowerShell Logon Script   • Map Network Drive   • PowerShell add printer   • PowerShell -com

Invoke-Expression   • Invoke-Command 

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.