[REQUEST] Nested quotes hell (Batch script + Powershell)

Moltuae

Rest In Peace
Reaction score
3,671
Location
Lancs, UK
I've got a one-line Powershell script (that I created from a multi-line script I found online), which changes network connections from public to private.

I'm calling the Powershell script from within a batch file, like this:
Code:
PowerShell -ExecutionPolicy Bypass -command "$nlm = [Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]'{DCB00C01-570F-4A9B-8D69-199FDBA5723B}')); $connections = $nlm.getnetworkconnections(); $connections |foreach {if ($_.getnetwork().getcategory() -eq 0) {$_.getnetwork().setcategory(1)}}"


If I run the batch file with elevated privileges it works perfectly. However, I'd like to have the batch file elevate itself (ie triggering a UAC prompt).

It seems that I 'just' need to use Powershell to call the script in an elevated Powershell window, as in the example in Step 3 here:
https://www.howtogeek.com/204088/how-to-use-a-batch-file-to-make-powershell-scripts-easier-to-run/
Code:
PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"


So, using the above example, it would seem I need to enclose my Powershell script like this:
Code:
PowerShell.exe -Command "& {Start-Process [POWERSHELL SCRIPT HERE] -Verb RunAs}"

However, this introduces further double quotes, enclosing a script that already contains a number of double and single quotes. I would usually solve something like this by 'escaping' the quotes, using extra quotes or use some mix of double and single quotes to get around it. While that might sound straightforward enough, the level of quote nesting here, combined with the mix of batch and Powershell has got me perplexed. I just can't seem to get the damn thing to work.

Anyone Powershell experts like to offer the solution?

Thanks in advance!
 
I'm not sure about that method but I use this code to auto elevate my batch scripts. At the top of my batch file:
Code:
net file 1>nul 2>nul && goto :run || powershell -ex unrestricted -Command "Start-Process -Verb RunAs -FilePath '%comspec%' -ArgumentList '/c %~fnx0 %*'"
goto :eof
:run
 
I'm not sure about that method but I use this code to auto elevate my batch scripts. At the top of my batch file:
Code:
net file 1>nul 2>nul && goto :run || powershell -ex unrestricted -Command "Start-Process -Verb RunAs -FilePath '%comspec%' -ArgumentList '/c %~fnx0 %*'"
goto :eof
:run
Well I was attempting to elevate just the Powershell script but that solution will do just fine! Thanks :)

But how the hell does it work? ... I'm assuming it somehow re-runs itself to self-elevate the batch file but, in that case, the "goto :run" and "goto :eof" aren't doing what I'd expect them to do.
 
Last edited:
Ok so look at it like this:
Code:
net file 1>nul 2>nul && echo "You have admin rights" || echo "You don't have admin rights"

The && and || mean:

Command 1 && Command 2 || Command 3
If Command 1 is successful, run command 2. If command 1 fails, run command 3.

The net file is testing for admin, it won't be successful without admin. So if it fails the part after the || runs. If it's successful then it runs the goto :run.
After the || has the powershell command that will self reference the file and launch it with admin privileges.
Next line is for when it fails, just skips to the end of the file so after it launches as admin the original file doesn't run it's code and closes.
Then you have the :run so when the file is relaunched as admin, or you run it as admin it will skip the goto :eof and run the code.
 
@mikeroq What he wanted is a way for his script to force a demand on admin rights on screen to avoid choosing power-shell admin from what I've read. Currently on a job but if no solution was found I'll fix the code when I get home.
 
@mikeroq What he wanted is a way for his script to force a demand on admin rights on screen to avoid choosing power-shell admin from what I've read. Currently on a job but if no solution was found I'll fix the code when I get home.

However, I'd like to have the batch file elevate itself (ie triggering a UAC prompt).

Well to be fair my solution does exactly what he said.
 
Ok so look at it like this:
Code:
net file 1>nul 2>nul && echo "You have admin rights" || echo "You don't have admin rights"

The && and || mean:

Command 1 && Command 2 || Command 3
If Command 1 is successful, run command 2. If command 1 fails, run command 3.

The net file is testing for admin, it won't be successful without admin. So if it fails the part after the || runs. If it's successful then it runs the goto :run.
After the || has the powershell command that will self reference the file and launch it with admin privileges.
Next line is for when it fails, just skips to the end of the file so after it launches as admin the original file doesn't run it's code and closes.
Then you have the :run so when the file is relaunched as admin, or you run it as admin it will skip the goto :eof and run the code.
Ah, yes. I see it now. Neat solution.

@GreyWolf Thanks. I was looking for a way to fix the code I posted but mikeroq's solution will do just fine!
 
Not saying your code is bad, myself personally I hate anything that over pushes on the automatic scripts I prefer seeing more on the step by step approach.

It's for the pop-up window directly with the batch or shell.

there is also the runas command to force a admin user profile on a regular profile.

Edit: Great if his solution does the job for you less work for me. :D
 
Back
Top