CMD Prompt: Program shutting down when starting 2 programs with arguments - powershell

I am trying to run a powershell command and a program called Psuite simultaneously in a batch file.
I searched through S.O. and I found an interesting command: Call :RunProgramAsync, but I researched it and can't make it work at all.
Here are the commands I want to execute. Currently, it will run the first for a second, shut it off. I want it to run the first then the second while keeping the first on. Each command works on its own.
Here is what I want to run:
cd C:\ASD\scripts
start PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"
cd C:\psuitent\tests
start psuite -l200 -EGB5 $c:\psuitent\swt\hpsmtsas.swt
Can someone help me? I have been searching all over and can't find a solution.

when you use start it opens up a new window, so this might be causing an issue with cd, try something like this instead:
start "" /d "C:\ASD\scripts" PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"
start "" /d "C:\psuitent\tests" psuite -l200 -EGB5 $c:\psuitent\swt\hpsmtsas.swt
Hope this helps!
Source: Running multiple commands simultaneously

Related

How to pass arguments to PowerShell script executed as administrator from batch script?

I have a PowerShell script that needs to be executed using Windows' Batch Script as Administrator. The script is running without arguments, however when I pass arguments, the PowerShell window pops up and closes instantly. Here is what I have tried.
Batch script
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell '-NoProfile -
ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1" -FolderPath \"test"' -Verb
RunAs"
Powershell
param
(
[string]$FolderPath ='D:\batch_scripting'
)
echo $FolderPath
pause
I will add further functionalities in these scripts later. But I have to figure out this first.
The script can be executed if -FolderPath argument is not passed.
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell '-NoProfile -
ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1"' -Verb
RunAs"
I have also gone through following questions but this does not work for me.
I found the solution, although it's weird.
When you execute the powershell script as Administrator, trailing "/" must be added to the path of the script.
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell 'NoProfile - ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1\"' -Verb RunAs"
It works fine now.

How do I run a PowerShell script as administrator using a shortcut?

I'm trying to run a PowerShell script as administrator using a shortcut. I have tried many ways, but it still does not work:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoExit -Verb RunAs Start-Process powershell.exe -ArgumentList '-file C:\project\test.ps1'
With this command, it will create two PowerShell windows and one window will close.
I also tried this one:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoExit Start-Process powershell.exe -Verb RunAs -File 'C:\project\test.ps1'
Can some one please help?
Tl;dr
This will do the trick:
powershell.exe -Command "& {$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList \"-ExecutionPolicy ByPass -NoExit -Command Set-Location $wd; C:\project\test.ps1\"}"
Explanation
First, you have to call PowerShell to be able to execute Start-Process. You don't need any additional paramters at this point, because you just use this first PowerShell to launch another one. You do it like this:
powershell.exe -Command "& {...}"
Inside the curly braces you can insert any script block. First you will retrieve your current working directory (CWD) to set it in the new launched PowerShell. Then you call PowerShell with Start-Process and add the -Verb RunAs parameter to elevate it:
$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList ...
Then you need to add all desired PowerShell parameters to the ArgumentList. In your case, these will be:
-ExecutionPolicy ByPass -NoExit -Command ...
Finally, you pass the commands that you want to execute to the -Command parameter. Basically, you want to call your script file. But before doing so, you will set your CWD to the previously retrieved directory and THEN call your script:
Set-Location $wd; C:\project\test.ps1
In total:
powershell.exe -Command "& {$wd = Get-Location; Start-Process powershell.exe -Verb RunAs -ArgumentList \"-ExecutionPolicy ByPass -NoExit -Command Set-Location $wd; C:\project\test.ps1\"}"

Run powershell command as admin with a batch variable

I currently have a problem where there is a process that gets stuck in task manager. I'm tired of going into the task manager and ending the random amount of tasks for my users. I would love to simplify the script that I have set up to not require placing the variable in a text file.
This is what I have set up now:
set curuser=Domain\%username%
powershell -noprofile -executionpolicy Bypass -command " '%curuser%' | Out-File -filepath C:\software\lynctemp.txt"
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""powershellscript.ps1""' -Verb RunAs}"
pause
del C:\software\lynctemp.txt
powershell -executionpolicy Bypass -command "start-process -filepath 'C:\Program Files (x86)\Microsoft Lync\communicator.exe'"
The powershell script that gets called is literaly one command but it requires the current user variable. I couldn't for the life of me figure out how to get the Run As switch to work in just calling the powershell command. Instead I made a text file to hold the variable and then I delete it after its done being used. the only problem is that I would love to just pass the %curuser% variable right into the powershell call for example:
powershell -noprofile -executionpolicy Bypass -command "get-process -includeUsername | Where-Object {$_.Username -eq %curuser% -and $_.processname -eq "communicator"} | Stop-Process -force" -verb runas
The reason I want to do that, is because if this gets ran twice on the same host, the current user wont be the current user and its set for failure.
I was also looking at the taskkill.exe but I need to make sure it doesn't just end a random task because the users are using a shared rds host. so if I end task it needs to be for that user. If anyone has any ideas on better way of fixing this bug with Microsoft Lync 2010, Please let me know. im just setting up a bandaid for when it opens 50 communicator processes which makes the program unusable.
Hi I ended up finding a different solution after digging into the filter for taskkill.exe
set curuser=Domain\%username%
taskkill.exe /f /fi "USERNAME eq %curuser%" /im communicator.exe
ping pleasesleep.com
powershell -executionpolicy Bypass -command "start-process -filepath 'C:\Program Files (x86)\Microsoft Lync\communicator.exe'"
I've tested it on my local machine and am going to test tomorrow when the tickets come in.
I still am curious about my initial question though:
If I did want to pass paramaters to a powershell script that is being run as admin, How would I do so from a batch file?

Powershell Start-Process works but not from .ps1 script

If I paste this into Powershell blue window it runs fine and launches the program
Start-Process “C:\Program Files (x86)\Engine Pro\engine.exe” -ArgumentList "#21#”;
but if I try to run the same command in a script, run.ps1 script, that launches from a scheduled task in Windows, it does nothing
PowerShell.exe -windowstyle hidden -NoProfile -ExecutionPolicy Bypass C:\run.ps1
Does it have something to do with the -ExecutionPolicy Bypass? Do I have to have an Execution policy in the script as well? I really don't know what that is. I know what -windowstyle hidden is but -NoProfile -ExecutionPolicy Bypass I'm not sure why that is there, just found it on another page, but it's all working except for the program launching from within the script.
Thank you.
& Start-Process "C:\Program Files (x86)\Engine Pro\engine.exe" -ArgumentList "#21#";

Syntax error when starting powershell from bat file

I want to start a powershell script with RunAs from a bat file. This works.
#echo
SET "InstallerFolder=\\dc01\e\script"
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%InstallerFolder%\Script.ps1""' -Verb RunAs}";
But if i add:
-RedirectStandardOutput ""%InstallerFolder%\node.txt""
It breaks.
So the line looks like this:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-RedirectStandardOutput ""%InstallerFolder%\node.txt"" -NoProfile -ExecutionPolicy Bypass -File ""%InstallerFolder%\TSM Client Install Script.ps1""' -Verb RunAs}";
And resuslts in an powershell error which is gone so fast i can't see it.
Probably syntax?
Help much appreciated!
Thanks.
You get an error because powershell.exe does not have a -RedirectStandardOuptut parameter. (See Technet).
Also your syntax is way off (but since i dont see any reason to start powershell to start powershell again i wont bother explaining the syntax errors).
If you want to use RunAs from the cmd use it directly. For more info see Technet (again).
Also you can redirect output in Batch Files with > or >> if you want to append.