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:
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/
So, using the above example, it would seem I need to enclose my Powershell script like this:
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 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!