Avatar of Akire4
Akire4
 asked on

Close Grecefully Application With Power Shell

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?
PowershellShell Scripting

Avatar of undefined
Last Comment
Akire4

8/22/2022 - Mon
Raheman M. Abdul

Question not clear. Can you explain again please.
Akire4

ASKER
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?
ASKER CERTIFIED SOLUTION
Raheman M. Abdul

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Akire4

ASKER
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()]
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Akire4

ASKER
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. Abdul

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.
Akire4

ASKER
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?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Akire4

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

Its been open since 9/26