3

I'm writing a PowerShell script that will open an application, run a process, then close it, and then open another application, run a process, and close it. (Ultimately more than two, but I'm starting small.)

I can run the following script, with no issues:

# Project Config
$projectName = "c:\temp\test3.egp"
$projectName2= "c:\temp\test4.egp"

# Start Enterprise Guide
$app = New-Object -ComObject SASEGObjectModel.Application.7.1

#Open the Enterprise Guide Project
$projectObject = $app.Open($projectName, "")

# Save the Enterprise Guide Project
$projectObject.Save()

# Close the Enterprise Guide Project
$projectObject.Close()

# Open the second project
$projectObject = $app.Open($projectName2, "")

# Save the second project
$projectObject.Save()

#Close the second project
$projectObject.Close()

# Quit Enterprise Guide
$app.Quit()

PS C:\temp> ./test.ps1
PS C:\temp>

However, if I run it a second time at that above prompt, without exiting PowerShell, I get errors.

PS C:\temp> ./test.ps1
Exception calling "Open" with "2" argument(s): "Path is not a directory 'C:': The filename, directory name, or volume label syntax is incorrect."
At C:\temp\test.ps1:9 char:27
    + $projectObject = $app.Open <<<< ($projectName, "")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation
... more errors related to $projectObject not having a value, and then a second copy of each of these for the second attempt ... 

PS C:\temp>

It works fine if I exit from Powershell, and reopen Powershell, and run it again.

Restrictions that are documented primarily include only one Project being able to be opened at one time; however, clearly $projectObject.Close() works fine, because it does successfully run two in one script. It's only when I've left the script and run it again that it's a problem.

Now, if I remove $app.Quit(), it then allows me to run it a second time in the same session; but I do want to quit the app (to ensure that any following run of the script isn't impacted by anything I just did; this is a programming environment, so things like setting session macro variables and such could have bad impacts). I also don't entirely understand why the line $app = New-Object ... doesn't create a new instance of the application (and so why the prior run's $app quit is relevant)?

Is there something in PowerShell I am doing wrong here? Or is this solely an issue with the API I'm using that I'll have to talk to SAS (the vendor)?

Joe
  • 62,789
  • 6
  • 49
  • 67
  • That is odd. Especially as I would expect all of your variables to have script scope only, so one run should not have an effect on another. I understand that removing `$app.Quit()` seems to make it work... but when it's included, have you verified that the application has fully quite? E.g. via Task manager. As the errors are related to `$projectName` perhaps `Write-Output` near script start or script end will yield more info. – G42 Jul 18 '17 at 16:00
  • 1
    Yes, I would think this is an issue you'd have to ask the vendor about. – Bill_Stewart Jul 18 '17 at 16:01
  • @gms0ulman Unfortunately it doesn't look like I can tell. Powershell seems to run the app under a different user ID, and I don't have admin rights to view processes from all users. (Or else it doesn't directly expose to task manager?) – Joe Jul 18 '17 at 16:06
  • I did confirm that there is some difference between running the script twice vs. running the two projects in one script, in that it does not persist session macro variables; so maybe I can just leave out `$app.quit` and not worry about it. Still odd, though. – Joe Jul 18 '17 at 16:22
  • Could you test by adding $app.Dispose() after your $app.Quit(). – Ty Savercool Jul 18 '17 at 16:44
  • @TySavercool No such method, unfortuantely. – Joe Jul 18 '17 at 16:54
  • Thanks for testing. Might be worth doing a `$app | get-method` and looking for a method that the devs left for such instances. – Ty Savercool Jul 18 '17 at 16:58
  • What is the powershell directory when you open a new powershell vs trying to re-run the script? if you CD to say `PS C:\Users\userProfile>` and then try to call the script does it run? – Austin T French Jul 18 '17 at 17:32
  • Nope... just the documented ones (new, open, profiles, quit, setActiveProfile). – Joe Jul 18 '17 at 17:32
  • @AustinFrench Doesn't make a difference. (I'm CDing to the location of the script each time I reopen Powershell, but I can run it from wherever as long as I put the path in correctly.) – Joe Jul 18 '17 at 17:33
  • @Joe Unfortunately I don't have any experience with Enterprise Guide to help further. Reg ComObjects, I've mainly worked with Excel which shows as running in the task manager. Any objects/child-processes spawned by the script should run as the same user AFAIK. Running `$app | Get-Member` may reveal some properties/methods that could help. I second Bill_Stewart's comment to refer to the vendor as they're best placed to help. Good luck! – G42 Jul 18 '17 at 18:54
  • You could try adding `Remove-Variable app` as the last line of the script to dispose of the app before running it a second time. Or maybe `If(Get-Variable app){Remove-Variable app}` just before you create `$app` on line 6. – TheMadTechnician Jul 18 '17 at 21:50
  • What will happens if you place the script in a PowerShell scriptblock in the following way: `& { <# place the script here #> }`? More details are [here](http://community.idera.com/powershell/powertips/b/tips/posts/using-custom-scopes) – Andrei Odegov Jul 19 '17 at 06:59
  • I am new to PowerShell and I have experienced this same issue. I am unable to run this code twice in the same session. I am just working with formatting, nothing big. `code WmiObject -ClassName Win32_BaseBoard | Select-Object Caption,Description,InstallDate,Manufacturer,Model,Name,OtherIdentifyingInfo,Product,SerialNumber,SKU,Version | Add-Content C:\Users\ComputerInfo.txt -Encoding unicode` – Jonathan Blaine Mar 17 '22 at 18:55
  • 1
    @JonathanBlaine You may want to ask a new question - this is pretty old and didn't really get a satisfactory answer, although I see there were some comments after I logged off that day that I didn't get around to addressing. – Joe Mar 17 '22 at 19:01
  • It is strange, I closed then re-opened PowerShell. It is now running both scripts. I guess problem solved. – Jonathan Blaine Mar 17 '22 at 19:38

1 Answers1

1

Inside your original PS session create a new PS session for each project: New-PSSession

More details and examples can be found on the Microsoft docs site here.

momo1644
  • 1,769
  • 9
  • 25