running a bat file and powershell with elevated priviliges - powershell

so i have a bat file that goes:
powershell -noexit "My\file\location\myscript.ps1"
the bat file is in the start up. the powershell script changes the bcd. when executed in this manner it says i do not have privileges, access denied, and nothing in the bcd is changed.
sorry for being a newb, but is there a way to pass an elevated privileges value from the bat to the powershell script?

I don't know if this is possible to do from outside Power Shell but it can be done from inside. It's hacky but you could run a power shell script from your bat file that then launches the admin Power Shell session using "PS> Start-Process powershell -Verb runAs".
PowerShell: Running a command as Administrator

Related

How to run an application from powershell without elevated rights

I have a powershell script that needs to be run as admin to set IP addresses. Then I need to run an application as non-admin. As I understand it, this corresponds to the term "elevated rights".
If I simply double click the .exe from the file explorer (not "run as admin"), the app runs as intended without elevated rights.
I have found several tips online on how to accomplish this, however I haven't succeeded with the following commands in my script:
(from How to run exe with/without elevated privileges from PowerShell)
runas /trustlevel:0x20000 "\..\myApp.exe":
this results in an "Internal error" because access is denied to a certain ".lock" file related to an eclipse workspace.
Second approach:
Start-Process -filepath "\..\myApp.exe" -ArgumentsList "-ExecutionPolicy bypass -Scope CurrentUser"
this runs the application but it's run in elevated state.
EDIT: Third approach:
I tried making a second script from where I run
Start-Process -FilePath "\..\myApp.exe"
which I call from my main script using:
Start-Process PowerShell -ArgumentList '-File ""\..\mySecondScript.ps1""' -Verb open
This results in myApp running with elevated rights when its called from within the main script, but without elevated rights when run on powershell on its own.

SFX installer fails to run powershell script after powershell window is opened

I am creating an 7zip sfx installer in latest Windows 10 which is expected to run a powershell script. when extraction is done, the powershell window is shown up but quickly closed without executing the script.
Any help is much appreciated!
I have tried to sign the script file and the installer.exe itself with self signed certificate, but no luck.
I have changed the Powershell ExecutionPolicy to Unrestricted for all scopes for troubleshooting, but no luck either
I use process monitor in sysinternals to capture the powershell.exe behavor, and found powershell starts to exit thread after reading and writing c:\Users\xxx\AppData\Local\Microsoft\Windows\PowerShell\StartupProfileData-Interactive
Here is my config.txt for 7zip sfx:
;!#Install#!UTF-8!
Title="Cloud API"
BeginPrompt="Do you want to install Cloud API?"
RunProgram="powershell.exe -NoExit –NoProfile -ExecutionPolicy Bypass -File %%T\\cloud_api\\installer.PS1"
Directory="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\"
;!#InstallEnd#!
I am zipping the files in python by:
..\\7zr a -sfx7zSD.sfx cloud-api.7z .\\cloud-api -r -y
I am expecting the installer.PS1 getting executed after unzipping, but only powershell.exe is run and exit quickly without running the script at all.
A first step to troubleshooting this problem would be to declare the full path of the .ps1 script in your 7zip config. This will ensure you are calling the script you intend to launch.

Powershell script run as administrator prompt is coming

I am having below line of code which is part of my build process. Below code take SectionName and dotnet framework path for aspnet_regiis and encryption flag -pef to encrypt my web.config.
So when build happens i need to run the below code "Run as administrator" for that i used start-process with powershell and -Verb runas option. But when run this script i get a prompt in windows 10 with yes or no option.
How to avoid this.
$args="-pef '${SectionName}' '${configfilepath}'"
Start-process powershell -Verb runas "$(join-path ${frameworkPath} 'aspnet_regiis.exe') ${args}"
Though not the best option, you can have a shortcut to a script that runs the powershell script. Give that shortcut administrative permission and double click it / run it though CMD or something to have it run as administrator.
EDIT:
As suggested here:
PowerShell: Running a command as Administrator
You can add an if statement to ensure the -Verb runAs works.

How can I bypass execution policy when running scripts from Powershell ISE

So I can write a script in Powershell ISE, not save it, and it will run (F5/green arrow on the ISE). Once I save it, I get the error saying I can't run saved scripts. If I open a Powershell window in the directory the saved script exists, I can run it with
powershell -ExecutionPolicy ByPass -File script.ps1
But is there a way I can get this to work when running it via the ISE's green arrow/F5? I don't have admin access to this PC
Edit: Windows 10
Ok so I just found out you can set Execution Policy for yourself (current user) without having admin rights. So if you're coming here from google do this:
Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
Also you can run individual script without setting Execution Policy for current user, by passing Execution Policy only for file script.
For example:
Powershell -executionpolicy RemoteSigned -File "C:\scripts\script.ps1"
Very convenient for scheduled tasks in the Windows Task Scheduler to run PowerShell commands (scripts).
That's my addition for google users

PowerShell script to restart a service

My mission is to press a keyboard sequence, such as Ctrl +Shift +R, to restart a Windows Service.
I have a script which works fine in the PowerShell ISE, when launched with administrative privileges.
When I try with a PowerShell script it fails due to insufficient Administrative Privileges. It’s galling that I can get it to work with an old-fashioned bat file, but not PowerShell.
The root of the problem is that shortcuts to a PowerShell script have their Administrative privileges box greyed out. So far no work-around has overcome this privilege problem.
Any ideas?
One approach is to start another elevated PowerShell session within your script like so:
Start-Process PowerShell.exe -arg '-nologo -noprofile script.ps1' -verb runas
That should prompt to elevate the new PowerShell session. I think you should be able to set the -WindowStyle parameter such that the new window doens't appear (if you need that behavior). Note that you will need to specify the full path to your existing script.
You suggest you don't like solving this problem with a batch file (e.g. net start), I think because batch files are inherently more limited than powershell scripts. What you can do is wrap your Ps script in a batch file, though, for the sake of accomplishing your stated objective -- running a powershell script with a keyboard shortcut without access permissions issues. Try this in a batch file:
powershell set-executionpolicy remotesigned
powershell myscript.ps1