Jump to content

How to start an application via a script using powershell


Recommended Posts

Posted

I'm trying to start an application that will start via PS script.

I create pulseway script using Windows Powershell to run this command but I can't get the application to start.

 

$env:Path += "C:\Program Files (x86)\NetDocuments\ndOffice\ndOffice.exe" 
& "C:\Program Files (x86)\NetDocuments\ndOffice\ndOffice.exe"

 

However if I go to the remote computer, open Windows Powershell and run these commands the application opens as desired.

All my other scripts I have work on this computer, so I'm executing it correctly.

 

Please advise.

 

Greg

 

Posted
On 10/29/2021 at 5:30 PM, Gregory Candido said:

I'm trying to start an application that will start via PS script.

I create pulseway script using Windows Powershell to run this command but I can't get the application to start.

 

$env:Path += "C:\Program Files (x86)\NetDocuments\ndOffice\ndOffice.exe" 
& "C:\Program Files (x86)\NetDocuments\ndOffice\ndOffice.exe"

 

However if I go to the remote computer, open Windows Powershell and run these commands the application opens as desired.

All my other scripts I have work on this computer, so I'm executing it correctly.

 

Please advise.

 

Greg

 

Hey Greg,

 

There are a number of ways to start a program, however, when you say you go to the remote computer, are you actually logging into the remote computer and opening PowerShell on the local machine and running those commands? If so, that's the difference.  When running anything through Pulseway, it will default to running as the system account. So trying to run those commands through Pulseway, you're opening the program (if it even supports non-interactive) as the system account, and no user will see it.  You could check processes to confirm that though.  

You would need to run it under user context and right now, Pulseway only has that ability to do this if you log onto the machine in question, open Pulseway Manager, go to Settings - Runtime and scroll to the bottom to Enable PowerShell User Impersonation.  This will run scripts through Pulseway as the user you entered.  This will do the trick.  You can then call them different ways.  Start-Process "Path\to\exe\" also will work. 

Posted (edited)

If I enable PowerShell User Impersonation, what username and password would I use? An administrator service account or would I enter the end users account?

 

Let me give you more insight what I am trying to do.

I have little to no knowledge when it comes to scripting.  I just keep trying until I succeed.

 

I created a script in PW that updates an application we have called ndOffice.  This scripts works perfectly.  However after the update I want the program to start because it will ask the user to sign into the application after each update.  Right now after I run my script to install the new update, I have to manually go into Windows --> Programs and launch the application.

I tried the user impersonation but that does work when I entered an administrator service account and try to run the script again.  Anything you can think that I am doing wrong.  The scripts work if I open PS on the user computer and manually type the command below.

Script 1:

$env:Path += 'C:\Program Files (x86)\NetDocuments\ndOffice\ndOffice.exe"'
& 'C:\Program Files (x86)\NetDocuments\ndOffice\ndOffice.exe'

 

Script 2:

Start-Process 'C:\Program Files (x86)\NetDocuments\ndOffice\ndOffice.exe'

 

Thoughts or next steps.

Edited by Gregory Candido
Updated info
Posted

@Greg Candido

You need to impersonate the actual user that you want the program to launch under.  Keep that in mind because if the script to update it requires administrator privileges, but the user does not, then the update script may fail once you turn on impersonation.  However, if that's not a concern, then run impersonation as the user you need the program to launch under.  

Alternatively, and I think this may work for you, it's a bit... emm janky lol, but it will serve the purpose.  Log onto the machine and create a scheduled task that is triggered on-demand only, and have that task start the program under the user account you need.  Now, in your update script, at the end, once the update has completed, have your script trigger the scheduled task.  If the scheduled task is correctly set up to run as the specific user, it should launch the program when you trigger the task from your script. 

I realized you said you are not well versed in scripting.  Let's make this a little easier.  Turn off impersonation so you don't have to worry about permission issues in the future with other scripts.  

Log into the machine as the user you need (this will make creating the scheduled task easier). Create the scheduled task.  I'll show you screenshots of an example one I did to show you steps. You can even test this yourself using Chrome and then triggering the task through PW and you will see it launches under the user account.  Make sure you have the correct user selected.  Should be the user you need if you're logged in as them already.  Change Configure for to suit your needs.  No trigger, no conditions. Set it up as below.

When you're done setting up the scheduled task, add this line to your update script:

Start-ScheduledTask -TaskName "THENAMEOFYOURSCHEDULEDTASK"

You can open PW, navigate to the machine, go down to PowerShell, and run the above command to test before even adding it to your script to confirm. 

 

image.png.b8200416fd5b1688da88989d2f9ac47e.png

image.png.07af46df1a991f8149c88dfcc92726f6.png

image.png.07745800abb62b27e3b6b9cbff50f715.png

image.png.14b7306f3d107010ebfd2d68f3e8b7fa.png

 

Posted

You can do this, but need psexec and Windows is not designed to really do this.  It's a security flaw since each user session is supposed to be a controlled session. That said, the below works for me (with a copy of psexec somewhere on the machine .... can copy to anywhere on machine via script).

#Add command to copy psexec to computer
Copy-Item -Path \\server\share\psexec.exe -Destination C:\scripts\psexec.exe
#Get the console user session ID
$session = tasklist /fo CSV | findstr Console ; $session = $session.Split(",")[3] ; $session.split('"')[1]
#Launch notepad via session variable
& .\psexec.exe -s -i $session notepad.exe

Again, this is a security flaw and some apps may not work as well as notepad does but it does work.

  • 4 weeks later...
Posted (edited)

@Greg Candido I'm going to guess by now that you've probably have resolved this perhaps with one of the given methods.  But I wanted to bump this thread because of another way to accomplish this, which is actually probably easier than the other methods suggested, and it will work well for others needing to run scripts as users. 

There is a PS module on PSGallery called RunAsUser which allows you do just that.  You can very easily implement this and run through Pulseway.  As an example, using the same scenario I did above where I wanted to launch Chrome as the user, look how much less work this is lol.

###Check if module is installed and if not, install it###
if (!(Get-InstalledModule | Where-Object { $_.Name -eq "RunAsUser" })) {
    Install-Module RunAsUser -Force
}
 
###Whatever code you want to run as the user###
$ScriptBlock = {
    Start-Process "C:\Program Files\Google\Chrome\Application\chrome.exe"
}
 
Invoke-ASCurrentUser -ScriptBlock $ScriptBlock

 

Adding link to the module for reference: https://github.com/KelvinTegelaar/RunAsUser

Edited by Mark G38

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...