April 25th, 2006

GetObject()

PowerShell Team
PowerShell Team

Monad provides a way to create new com objects with new-object

$ie = New-Object -ComObject InternetExplorer.Application
$ie.Navigate2(
http://blogs.msdn.com/monad)
$ie.Visible=1

Great but what about if you want to bind to an existing object?  Where is the equivalent of GetObject()?

This is one of those good new/bad news stories.  First the bad news.  Monad does not provide a Cmdlet that exposes this function.  Alas, to ship is to choose and we are not going to get to in for V1.  Now for the good news.  Monad provides great .NET support and .NET exposes this function.

 

MSH> $w=[System.Runtime.InteropServices.Marshal]::GetActiveObject(“Word.Application”)

MSH> $w.documents |ft name,path -auto
Name                                              Path
—-                                              —-
RE: Question regarding Word scripting with MSH… RE:
cmdlets as classes.doc                            \\imself-dfs-07\dfsroot\dfsmydocs\js…
Event Schema.doc                                  \\imself-dfs-07\dfsroot\dfsmydocs\js...

 

Let me be quick to point out a problem that exists in your beta code but is fixed in the upcoming release.  If you just typed $w.Documents – it is going to appear to have hung and you’ll either have to wait a LONG time or hit CTRL-BREAK to terminate the session.  The work around is very simple, pipe the output to Get-Member and use that to find out what properties you want and then ask for those specific properties.

Anyway, this is an example of how Monad’s support for a wide range of technologies (text processing, COM, WMI, ADSI, ADO, XML, .NET, etc) really pays off.  As a rule, we are trying to expose the system with Cmdlet and namespace semantics.  It is going to take a while before the entirety of the system is exposed this way but by providing direct access to these other technologies, it enables the community to provide this support.  For example the function:

 

Function Get-Object ($ProgID) {
[System.Runtime.InteropServices.Marshal]::GetActiveObject($ProgID)
}

 

and the following formating directives:

<Configuration>
    <ViewDefinitions>
        <View>
            <Name>CustomView</Name>
            <ViewSelectedBy>
                <TypeName>System.__ComObject#{0002096b-0000-0000-c000-000000000046}</TypeName>
            </ViewSelectedBy>
            <TableControl>
                <TableHeaders>
                    <TableColumnHeader>
                       <Label>Name</Label>
                       <Width>20</Width>
                     </TableColumnHeader>
                     <TableColumnHeader>
                       <Label>FullName</Label>
                     </TableColumnHeader>

            </TableHeaders>
                <TableRowEntries>
                    <TableRowEntry>
                        <TableColumnItems>
                     <TableColumnItem>
                       <PropertyName>Name</PropertyName>
                     </TableColumnItem>
                      <TableColumnItem>
                       <PropertyName>FullName</PropertyName>
                     </TableColumnItem>
                        </TableColumnItems>
                    </TableRowEntry>
                 </TableRowEntries>
            </TableControl>
        </View>       
    </ViewDefinitions>
</Configuration>

 

allows us to do the following:

 

MSH> $w = Get-Object -ProgId Word.Application
MSH> $w.documents
Name                                              Fullname
—-                                        ——–
RE: Question regarding Word scripting with MSH… RE:
cmdlets as classes.doc                            \\imself-dfs-07\dfsroot\dfsmydocs\js…Event Schema.doc                                  \\imself-dfs-07\dfsroot\dfsmydocs\js

 

Extend the system and Enjoy!

Jeffrey P. Snover
Monad Architect

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

PSMDTAG:TYPE:COM: InternetExplorerer.Application

PSMDTAG:FAQ: What is the equivalent of VBSCRIPT’s GetObject()?  Answer: [System.Runtime.InteropServices.Marshal]::GetActiveObject(“progid”)

PSMDTAG:FORMATEXTENSION: (Word.Application).Documents

Category
PowerShell
Topics
COMFAQ

Author

PowerShell Team
PowerShell Team

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

0 comments

Discussion are closed.