0

I've found two great tricks and I'd really like to get them to work together, but how to do so is totally eluding me.

One I just found is this: https://stackoverflow.com/a/2611394/12943 which is a batch file that starts powershell and feeds itself to powershell which is awesome.

the other trick is this one: https://stackoverflow.com/a/18739839/12943 Which lets PowerShell elevate itself if necessary (Feeding the script to itself once elevated)

I think it might work if the first line of the batch file that looks like this:

@findstr/v "^@f.*&" "%~f0"|powershell -&goto:eof

instead looked something like this:

@findstr/v "^@f.*&" "%~f0"|powershell "start-process powershell -verb runas" -&goto:eof

but that doesn't work and I can't quite get it right (I think the piping in that case goes to the original powershell process but not the one you runas).

The two methods obviously won't easily work together because they use similar mechanisms, but I can't help think that there is a clean solution that will both let me easily run a powershell script by typing the name into a command window AND self-elevate--both without any prior setup or extra software (since this is the first step of some system-configuration scripts on a system that cannot be attached to the internet).

I can (and will) do this by simply using the batch file solution and requiring the user to elevate manually but it seems like there should be an easier way.

Note that the batch file seems to also get around the executionpolicy which is somewhat curious but in this case preferred.

Community
  • 1
  • 1
Bill K
  • 62,186
  • 18
  • 105
  • 157
  • If you are making a system configuration script which is a PowerShell script, why aren't you just making it a PowerShell script (`ps1` file) instead of a `bat` file trying to run the contents in PowerShell? – Robert Westerlund Apr 08 '14 at 00:08
  • @Robert Mostly the need to run Set-ExecutionPolicy unrestricted so the scripts can run in the first place. Currently I do this from a batch file and I'd just like to combine the batch file and config script. – Bill K Apr 08 '14 at 20:34

1 Answers1

0

perhaps you should autoelevate the batch instead of your ps script. running powershell.exe from elevated cmd opens an elevate ps console. you can find auto elevating batch here https://stackoverflow.com/a/12264592/381149 .

Community
  • 1
  • 1
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
  • yes, that's how I've been runing for the last year (the page you referenced is probably one of the two I referenced above). I'm trying to add in the ability to run it from a batch file because at times the auto-elevating powershell script is annoying. For one thing a seperate batch file must still be used to Set-ExecutionPolicy unrestricted, for another it is running from a network mount and I'd like to just say //server/share/setup--I guess I'm trying to make it more foolproof in general. – Bill K Apr 08 '14 at 20:27