GetObject()

GetObject()

Rate This
  • Comments 4

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

Leave a Comment
  • Please add 6 and 1 and type the answer here:
  • Post
  • So how would this work when you're attempting to bind to a remote object? Obviously, with a WMI GetObject you'd use Get-WMIObject instead. Cool. But what about GetObject("WinNT://CLIENTA/Administrator,user") or a similar LDAP call?
  • Speaking of control-break, is there a way to make it terminate only the current operation rather than the entire session?  I habitually break out of individual operations that are taking too long and it's pretty frustrating when that boots me out entirely.
  • Control-C does a best effort attempt to stop the current operation.

    Jeffrey P. Snover [MSFT]
    Windows PowerShell/Aspen Architect
  • We still have an NT4 domain for which I'd like to enumerate all the servers. This probably needs to go down the ASDI route, but how?

Page 1 of 1 (4 items)