COM objects and PowerShell process exit

powershell

    Next

  • 1. wake on lan
    -- Bill
  • 2. WMI to start\stop web sites
    I am trying to figure out how to stop and start a Website using WMI in PowerShell2 but my WMI experience is next to nothing. Does anyone have any ideas? -dm

COM objects and PowerShell process exit

Postby ZGF2aWQuamFkZQ » Sun, 05 Nov 2006 07:59:02 GMT

I've got a simple script file that creates an in-process COM object. I'm 
seeing some very strange behavior when PowerShell has a normal exit and the 
COM object is being released during clean up. What I'm seeing is that during 
the Release call within the COM object, the thread just vanishes and the 
PowerShell process exits with a normal return code. 

The COM object in question is mine so I've been debugging this and I can see 
that the thread that is making the COM call just vanishes right in the middle 
of fairly simplistic code. The point that this happens is never that same 
place within the object's Release() method code, it's always a random 
location and is often right in the middle simple C code. To help debug this 
issue, which seems like a timing issue to me, I put a simple for loop in the 
object's Release() method that does simple things (i.e. int_i = int_i + 5). 
Invariably the thread just vanishes while single stepping through this code.

While watching the other threads in the process I can tell that they are in 
the GC and are doing things like: GCHeap::FinalizerThreadWatchDogHelper right 
before the thread executing my code vanishes. 

My question is, is there something in PowerShell or the CLR that is watching 
for COM Release() calls that are taking too long and terminating the threads? 
If so, is there a way around this? The COM object in question really needs to 
have it's Release() call completely executing as it is holding references to 
system services and it they don't get cleaned up during release, the OS will 
become unstable (the volume shadow service on XP goes south).

Note that in my script I do of course call the custom COM object's clean up 
methods proactively so that the object in question has nothing to do in its 
Release() method. I also have a Trap handler to help with this. However I am 
just trying to ensure that the code that cleans up when all else fails (i.e. 
the object was just released without any proper clean up) is not being 
prevented from executing completely.

thanks,
david


Similar Threads:

1.Need an idiot check on technique for releasing out-of-process COM object in PowerShell

2.New-object preventing process from exiting

We have a C++ application that has an automation object model.

In PowerShell we create a new instance of the object model via New-Object. 
Everything works as expected in terms of calls to the created object.

Problems arise if the user closes the application first, then closes 
PowerShell. The process of the application will not have exited, as seen by 
Task Manager.

If PowerShell is exited, and then the application the process will exit 
correctly.

What type of solution can I provide to my customers where they don't have to 
worry about the order in which the application and PowerShell are exited?


Thanks for any insight.

3.Powershell interacting with COM automation object with late binding

Hello All,

I have a quick question relating to powershell interacting with COM
automation object with late binding, is it possible?

For instance , while in a classical VB (with Excel) IDE I'm able to do
the following:-


Dim objStationApp As Object
Set objStationApp = CreateObject("Station.Application")
objStationApp.Connect "C:\Program Files\Honeywell\Experion
PKS\Client\Station\Default.stn"
objStationApp.CurrentPage = "C:\TEMP\Display1.htm"
objStationApp.Quit
Set objStationApp = Nothing

however using Powershell
$a=New-Object -COM Station.Application;
$a.Connect("C:\Program Files\Honeywell\Experion
PKS\Client\Station\Default.stn");
$a.CurrentPage = "C:\TEMP\Display1.htm"
$a.Quit

I get the following error :-

Method invocation failed because [System.__ComObject] doesn't contain a
method
named 'Connect'.

Does this mean that Powershell can't access Automation COM objects with
late bindings?

Thanks in advanced

Karl

4.Powershell V/s VB script windows installer COM object

Hello,

I am trying to use the windows installer COM object with powershell
and I'm running into problems. It looks like the shell holds on to
the .wsi file that I'm trying to change with an UPDATE call, so I can
run the ps1 script exactly once and after that I cannot access the
file (either through the shell or a tool like ORCA) until I close the
shell.

I confirmed this fact using the windows process explorer. After
executing the ps1 script I can find the .wsi file under "find handle"
and it is held by the shell from which the script was executed.

So, I created a VB script that essentially does the exact same operation on
the .wsi file and it turns out that I can run this any number of times
from the same shell without any problems.

What am I missing????
I have included the VB and PS1 script here. Any help is appreciated.

=======================VB SCRIPT
Dim installer : Set installer = Nothing
Set installer = Wscript.CreateObject("WindowsInstaller.Installer")
Dim databasePath:databasePath = Wscript.Arguments(0)
Dim database : Set database = installer.OpenDatabase(databasePath, 1)
Dim query, view
query = Wscript.Arguments(1)
Set view = database.OpenView(query)
view.Execute
database.Commit
>>>>cscript blah.vbs "blah.wsi", "UPDATE property SET property.value='3.0' WHERE property.property='productversion' "

=======================PS1 SCRIPT
$file = $args]
$installer = new-object -comobject "WindowsInstaller.Installer"
$database = $installer.InvokeMethod( "OpenDatabase", $file, 1 )
$view = $database.InvokeMethod( "OpenView", "UPDATE  Property SET
Property.Value='32.0' WHERE Property.Property='ProductVersion' ")
$view.InvokeMethod( "Execute" )
$view.InvokeMethod( "Close" )
$database.InvokeMethod( "Commit" )

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($installer)
$installer = $null
>>>>> blah.ps1 "blah.wsi"

=========================Types.ps1xml, has to be called using update-
typedate Types.ps1xml before running the ps1 script

 
  System.__ComObject
   
        [script removed]  InvokeMethod
 [script removed]         $type = $this.gettype();
        #write-host $type
        $index = $args.count -1 ;
        #write-host "Index :" $index
        $methodargs=$args..$index]
        #write-host $methodargs
        #write-host "Arg 0 :" $args]
        $type.invokeMember($args],
[System.Reflection.BindingFlags]::InvokeMethod,$null,$this,
$methodargs)
        [script removed] 

5.Howto: Use a com object in Powershell

I am trying to use a com object instance of the Novell volume administration 
control (nwvola.ocx) to list file details on a Netware server.  I have used 
this technique successfully with vbscript and vb.net.  Here's what I get:

PS C:\> $vol = New-Object -comObject "NWVolAdmLib.NWVolAdmCtrl.1"
PS C:\> $vol | Get-Member

   TypeName: System.__ComObject#{ebe2fdab-6c8f-11d1-a149-00401c6047e4}

Name      MemberType Definition
----      ---------- ----------
AboutBox  Method     void AboutBox ()
FindEntry Method     INWEntry FindEntry (string)
Load      Method     bool Load ()
Unload    Method     bool Unload ()

PS C:\>

The problem is that only the methods are listed (there are several 
properties), and I cannot read or set the properties.  This also happens with 
other Novell objects.

6. returning COM objects from PowerShell CmdLet

7. PowerShell cannot use (dynamic) COM objects

8. COM Object problem powershell / frames



Return to powershell

 

Who is online

Users browsing this forum: No registered users and 95 guest