最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Create .bat file to run powershell 'as user' and then execute .ps1 file - Stack Overflow

programmeradmin4浏览0评论

I need to create a .bat file that can run a powershell script using a specific account.

This command will run powershell as the user:

runas /user:domain\user "C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe"

The user is prompted to enter the account password and powershell launches.

Is there a way to have it automatically run a ps1 file once authentication is completed?

C:\Temp\script.ps1 

Thanks.

** Edit **

As per the comments, I have changed to powershell.exe.

This command isn't working.

runas /user:domain\user "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file C:\Temp\script.ps1

It returns a RUNAS error and doesn't ask for the user's password.

I need to create a .bat file that can run a powershell script using a specific account.

This command will run powershell as the user:

runas /user:domain\user "C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe"

The user is prompted to enter the account password and powershell launches.

Is there a way to have it automatically run a ps1 file once authentication is completed?

C:\Temp\script.ps1 

Thanks.

** Edit **

As per the comments, I have changed to powershell.exe.

This command isn't working.

runas /user:domain\user "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file C:\Temp\script.ps1

It returns a RUNAS error and doesn't ask for the user's password.

Share Improve this question edited 11 hours ago chatmandu asked 12 hours ago chatmanduchatmandu 194 bronze badges 4
  • 2 powershell /? will explain the commandline options needed to directly launch a script when starting powershell.exe (don't use the ISE for that). – Halfix Commented 12 hours ago
  • 1 It appears to me as if the command you have submitted does not do what you say it does! PowerShell ISE is not PowerShell, and therefore seems irrelevant to your particular circumstances. Please therefore explain to us if you really are trying to run a PowerShell script, or open a script within PowerShell ISE, and run a it. We have an Edit fatility for such things. – Compo Commented 12 hours ago
  • runas /user:domain\user "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file C:\Temp\script.ps1 isn't working. It is generating a RUNAS error. – chatmandu Commented 11 hours ago
  • Your later edit doesn't follow the guidance in my answer; therefore your updated command predictably fails; once you follow the guidance, your call will work as expected. If you have additional questions, provide feedback on the answer. – mklement0 Commented 1 hour ago
Add a comment  | 

1 Answer 1

Reset to default 1
  • In order to execute PowerShell commands from a command line launched from outside PowerShell, you must use powershell.exe, the Windows PowerShell CLI.

    • By contrast, powershell_ise.exe is the executable of the obsolescent Windows PowerShell ISE. While it offers a CLI (startup parameters) too, they only support opening script files for editing, not for execution.
  • A command line passed to runas.exe for execution as a different user must be specified as a single string (argument); that is, enclose both the target executable and its arguments in "..."

Therefore (full path to powershell.exe omitted for brevity):

runas /user:domain\user "powershell.exe -File C:\Temp\script.ps1"

Note:

  • If necessary, place -ExecutionPolicy RemoteSigned before -File in order to ensure that execution of the script is permitted, namely in case the execution policy in effect for the target user prevents script execution, but note that this process-level override won't work if the execution policy is controlled via GPOs (Group Policy Objects); see this answer for details.

  • Should the command line to pass require embedded " chars. (e.g. for a *.ps1 path that contains spaces), escape them as \".

发布评论

评论列表(0)

  1. 暂无评论