Previous Topic: mv, ren Commands--Move Files and Directories (Funclets)Next Topic: new-SSHSession Command--Create a new Secure SSH Session (Funclet)


new-comObject Command--Instantiate a COM Object (Funclet)

COM is a Windows-specific standardized API that allows application to expose services for consumption by COM clients. COM client support allows access to a wide range of COM servers like Internet Explorer, Microsoft Office, or various management-related OS services, in particular WMI. The new-comObject command creates proxy client objects that make the services exposed by a COM server available for scripting.

The command has the following syntax:

new-comObject progid [-locale lcid]
progid

Specifies a string that identifies the COM object to instantiate. The string can either be the human readable program ID (PROGID) or the internal CLSID. When specifying a CLSID, it must be contained in curly brackets.

lcid

Specifies an integer that defines the desired locale for COM servers supporting multiple locales.

Default: 0

Return value

The new-comObject returns an object that represents a proxy between the AutoShell and the COM server. The object mirrors the methods and properties exposed by the COM server. For details about the public interface of a COM server, refer to the programmer documentation of that particular server. The exposed methods and properties are accessed using regular JavaScript syntax on the returned AutoObject.

Examples

Open a web page in Internet Explorer and query its URL:

o = new-comObject InternetExplorer.Application
// Make Internet Explorer visible
o.Visible = true;
o.navigate("www.ca.com");
// Get active document (another COM object)
doc = o.document
? doc.Url     // for example http://www.ca.com/us

Get the computer name using the scripting object:

o = new-comObject WScript.Network
? o.ComputerName

Retrieve some information about the operating system and logical drives using WMI:

loc=new-comObject WbemScripting.SWbemLocator
// Security is actually not required for localhost
loc.Security_.AuthenticationLevel=0; // wbemAuthenticationLevelDefault
loc.Security_.ImpersonationLevel=3; // wbemImpersonationLevelImpersonate
wmisrv=loc.connectServer("localhost", "root\cimv2");
items = wmisrv.ExecQuery("Select * from Win32_OperatingSystem");
// Returned collection has only one item,
// since there is just one OS running
os = items[0];
? "OS Type:", os.Caption;
? "Service Pack:", os.CSDVersion;
? "Computer name:", os.CSName;
? "FreePhysicalMemory:", os.FreePhysicalMemory;
? "RegisteredUser:", os.RegisteredUser;
? "TotalVisibleMemorySize:", os.TotalVisibleMemorySize;
? "Version:", os.Version;
? "WindowsDirectory:", os.WindowsDirectory;
// Now for the disks
items = wmisrv.ExecQuery("Select * from Win32_LogicalDisk");
// Iterate over returned disks
for(i in items)
{
    disk = items[i];
    ? "Description:", disk.Description;
    ? "DeviceID:", disk.DeviceID;
    ? "FileSystem:", disk.FileSystem;
    ? "FreeSpace:", disk.FreeSpace;
    ? "Name:", disk.Name;
    ? "Size:", disk.Size;
}