0

I'm trying to run a powershell script from cmd with elevated privileges, but I need to pass a parameter to the powershell script. This is the code of the batch file I'm using:

echo off
cls
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -Verb runas -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dp0\MigrationTool.ps1 -ConfigFile ConfigFile.xml""'}"

When I use the code above, the script does nothing.

If I run the script without the -ConfigFile parameter, the script runs, then it prompts me to type the config file. Although I get prompted and then I just type in the config file, the script still doesn't work.

I did a little bit of digging on the Internet but couldn't find any solution.

Thanks.

2
  • 1
    Why do you run cmd to run PowerShell to run PowerShell? May be there is a less complex solution to your problem. You might check out the runas tool. Commented Jan 12, 2021 at 6:52
  • The problem might be caused by the double "". cmd and powershell escape differently. Commented Jan 12, 2021 at 8:35

2 Answers 2

1

Thanks for your help, but I got a solution for my problem. Please see below what somebody else recommended:

echo off cls

PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath PowerShell.exe -Verb RunAs -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "%~dp0MigrationTool.ps1" -ConfigFile "%~dp0ConfigFile.xml"'"

Thanks for all your help.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm usually doing it this way:

@echo off

REM Calling script without parameters
REM powershell.exe -ExecutionPolicy Bypass -File E:\scripts\some-ps-script.ps1

REM Calling script with option 'Parameter'
powershell.exe -ExecutionPolicy Bypass -File "E:\scripts\some-ps-script.ps1" -Parameter

This should work in your case:

powershell.exe -ExecutionPolicy Bypass -File "E:\scripts\some-ps-script.ps1 -ConfigFile E:\scripts\ConfigFile.xml"

2 Comments

It's not elevated = as admin ;)
Sure, you're absolutely right. I totally overlooked "cmd with elevated privileges".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.