Close Grecefully Application With Power Shell

Akire4
Akire4 used Ask the Experts™
on
I have an application that has not Exit button but just the window UI "X" (hard to believe) and Im trying to close this application gracefully from a button the application provides by the  use  of Shell, this button has two fields to run a Shell; Command and Command Arguments.

I'm trying to run a PowerShell that when I run it on the PowerShell window work fine;

PS C:\Users\MyProfile\Desktop:> Get-Process MyApplication | Foreach-object { $_.CloseMainwindow() | Out-Null }
It closes the application and it ask if I need to save any changes when appropriate when run from Powershell console.

How can I run this Powershell from the button the application provides?

Can it be run from with in the same application I'm trying to close?

I try to save the file with a .PS1 extension and call it from the Shell button but can not make it work, what is the syntax to do this or how can I run the Powershell line from the button?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Raheman M. AbdulMessaging and Directory Services

Commented:
Question not clear. Can you explain again please.

Author

Commented:
I need to use a Shell script to close and application, this application has a costume button that can run a Shell, but I don't want not close it by brute force I need to ask User if any changes need to be save. The application Form has a button that has to fields in its properties  for the Shell, field; command and filed; arguments. How can I run a Shell to close it properly?

When I use this Powershell script from the console;

PS C:\Users\MyProfile\Desktop:> Get-Process MyApplication | Foreach-object { $_.CloseMainwindow() | Out-Null }

 it closes the application properly the way I want, so how can I embed this line of script in to my button? or any other way to mimic what this line of script does from the Shell button i have in my application?
Messaging and Directory Services
Commented:
If you have the object of the form:

$form = New-Object System.Windows.Forms.Form

Open in new window

just use close method on the object.

$form.close()

Open in new window


If you have the code, attach here and someone will help.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
I don't have access to the IDE, its just a Form that you build by dropping Objects  with proprieties that have some of them available for you to costume the Form. If I use I assume the line of script you are contributing it will work on the IDE but all the access I have is the control  properties to run a Shell (Command and Command Arguments) when I run your scrip lines I get an error; "Error executing shell command! [$form=New-Object System.Windows.Forms.Form] Arguments [$form.close()]

Author

Commented:
Abdul, when I run this on the Powershell command window it work as I want;
PS C:\Users\MyProfile\Desktop:> Get-Process MyApplication | Foreach-object { $_.CloseMainwindow() | Out-Null }

How can I wrap this to run it from the application buttons that accepts Shell Command and Shell Arguments? I try to make a file with the script line and and save it as .PS1 and call it from this button but it fails. What is the easier way to call it from the application?
Raheman M. AbdulMessaging and Directory Services

Commented:
can you send the pictures of how you are running your button? i mean pictures of shell where you want to enter for more clarity.

Author

Commented:
I accomplished my goal by doing this;

On the button Command field (its just a WinForms property that can let you run a Shell Command) I run a VBS Script;
Command;C:\Scripts\CloseApp.VBS

CloseApp.VBS=
Set objShell = WScript.CreateObject("WScript.Shell")
ojbShell.Run(C:\Folder1\CloseApp.bat"),0,True

Why do I had to do this? so I can hide the .bat window when running

Batch file=CloseApp.bat(same name as the PowerShell file)
C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -executionPolicy bypass %~dp0%~n0.PS1

The reason for this is that since I can not run the PowerShell from my Button Command but I can run a Batch script  file, when I run the Batch and rename the PowerShell file name the same as the batch file when the Batch file runs it runs my PowerShell script since they both are in the same Folder directory.

PowerShell Script=CloseApp.PS1(same file name as the batch file name)

Get-Process MyApp | ForEach-Object {$_.CloseMainWindow() | Out-Null }

OK so I accomplished my  goal BUT how can I use just one either a VBS or BAT script to do what I accomplished with the 3 scripts; Close my application gracefully by running a command script window in a hidden window?

Author

Commented:
I've requested that this question be deleted for the following reason:

Its been open since 9/26

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial