简体   繁体   English

如何从Windows命令提示符(CMD)运行Long Powershell脚本

[英]How to Run Long Powershell script from Windows Command Prompt (CMD)

I tried to launch a long powershell script with the name "long name here.ps1" from command prompt. 我试图从命令提示符启动名称为“ long name here.ps1”的长PowerShell脚本。 But I am also trying to ensure that it runs as an administrator command in powershell. 但是我也试图确保它作为Powershell中的管理员命令运行。 I have all execution policies in powershell set accordingly I used the ss64 set-executionpolicy command guide for powershell to get powershell working. 我在powershell中设置了所有执行策略,因此我使用了ss64 set-executionpolicy命令指南来使Powershell正常工作。 But I am trying to use the solution from another stackoverflow question that talks about running commands as administrator. 但是我试图使用另一个stackoverflow问题中的解决方案,该问题涉及以管理员身份运行命令。 I am running a batch script that needs to execute a powershell script (.ps1) as admin, and I don't mind if the user is prompted by UAC or for the password. 我正在运行一个批处理脚本,该脚本需要以admin身份执行Powershell脚本(.ps1),并且我不介意用户是由UAC提示还是输入密码。 I am currently using the following command: 我当前正在使用以下命令:

powershell.exe -command "&{ Start-Process powershell -ArgumentList '-noprofile -file "C:\\long name here.ps1"' -verb RunAs}" powershell.exe-命令“&{启动进程powershell -ArgumentList'-noprofile -file“ C:\\ long name here.ps1”'-verb RunAs}”

I found this command at https://ss64.com/ps/powershell.html at the bottom where there are details on how to run a powershell command as administrator. 我在底部的https://ss64.com/ps/powershell.html上找到了此命令,那里有有关如何以管理员身份运行powershell命令的详细信息。 The problem with that code is that my powershell script 1. has arguments, and 2. has a long name. 该代码的问题在于我的Powershell脚本1.具有参数,而2.具有长名称。 I have tried many different iterations of this command with no success, and the ones that DON'T work are listed below: 我尝试了此命令的许多不同迭代,但均未成功,下面列出了不起作用的迭代:

powershell.exe -command "&{ Start-Process powershell -ArgumentList '-noprofile -file C:\\long` name` here.ps1' -verb RunAs}" powershell.exe-命令“&{启动进程powershell -ArgumentList'-noprofile -file C:\\ long` name` here.ps1'-verb RunAs}”

powershell.exe -command "&{ Start-Process powershell -ArgumentList '-noprofile -file:"C:\\long name here.ps1' -verb RunAs}" powershell.exe-命令“&{启动进程powershell -ArgumentList'-noprofile -file:“ C:\\ long name here.ps1'-verb RunAs}

Also, I am completely lost as to how to send arguments to the actual script. 另外,我完全不知道如何将参数发送到实际脚本。

If I'm reading your question correctly - powershell wont find the file as it stops reading the path name when it encounters a blank space? 如果我正确阅读了您的问题-Powershell找不到文件,因为当遇到空格时它将停止读取路径名?

The example given here specifies that; 此处给出的示例指定: powershell commands to be run from command prompt as an administrator should have the following syntax: 以管理员身份从命令提示符运行的powershell命令应具有以下语法:

powershell.exe -noprofile -command "&{ start-process powershell -ArgumentList '-noprofile -file MyScript.ps1' -verb RunAs}"

Couple of ways to achieve what you're looking for. 有几种方法可以实现您想要的。 But the easiest method would be to escape the quotes using a ` character. 但是最简单的方法是使用`字符对引号进行转义。 So something similar to; 所以类似

powershell.exe -noprofile -command "&{ start-process powershell -ArgumentList '-noprofile -file `"C:\long file name.ps1`"' -verb RunAs}"

Also might be worth checking out other answers here 也可能值得在这里查看其他答案

Use a Freeware Third Party Utility 使用免费的第三方实用程序

If a freeware third-party executable is permissible, you can use a short tool I wrote called elevate32.exe (32-bit) and elevate64.exe (64-bit) to launch powershell.exe as administrator with the -File parameter and the script arguments you want to use: 如果一个免费的第三方可执行文件是允许的,你可以用很短的工具,我编写的名为elevate32.exe (32位)和elevate64.exe (64位),以推出powershell.exe为管理员提供了-File参数和您要使用的脚本参数:

elevate64 -- powershell.exe -File "<path>\<long script name>.ps1" -Arg "<long script argument>"

You can get the tool from www.westmesatech.com (copyrighted freeware, free to use anywhere, no installation needed). 您可以从www.westmesatech.com获得该工具(受版权保护的免费软件,可以在任何地方免费使用,无需安装)。

Use a WSH Script 使用WSH脚本

If you can't use an external executable, you can also do this (although it does not handle quoting in as robust a manner as the elevate tool's -- parameter) using a Windows Script Host (WSH) script, elevate.js : 如果您不能使用外部可执行文件,则也可以使用Windows脚本宿主(WSH)脚本elevate.js来执行此操作(尽管它不能像elevate.js工具的--参数那样可靠地处理引用):

var args = WScript.Arguments;
if ( args.Length >= 1 ) {
    var exec = args.Item(0);
    var cmdLine = "";
    for (var i = 1; i < WScript.Arguments.Length; i++ ) {
      cmdLine += cmdLine == "" ? '"' + args.Item(i) + '"' : ' "' + args.Item(i) + '"';
    }
    var shellApp = new ActiveXObject("Shell.Application");
    shellApp.ShellExecute(exec, cmdLine, "", "runas");
}

You can call as follows: 您可以致电如下:

wscript.exe "d:\path\elevate.js" powershell.exe -File "C:\long path\script name.ps1" "long script argument"

Self-Elevate your PowerShell Script 自提升您的PowerShell脚本

Another option is to write a self-elevating PowerShell script. 另一个选择是编写一个自提升的PowerShell脚本。 You can check for elevation in the script; 您可以在脚本中检查海拔; if not elevated, it can launch itself elevated and run any command you need. 如果没有提升,它可以自行提升并运行您需要的任何命令。 Example: 例:

$isElevated = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if ( -not $isElevated ) {
  Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs
  exit
}

& "d:\long path name\script name.ps1" "Long Argument 1" "Long Argument 2"

When you use PowerShell.exe -Command you don't need to use quotes. 使用PowerShell.exe -Command时,不需要使用引号。 For example, you can run the following: 例如,您可以运行以下命令:

PowerShell.exe -Command Get-Service 'wuauserv'

Everything after -Command is interpreted as the command. -Command之后的-Command均解释为命令。 Note also that double quotes in CMD need escaping with a backslash. 还请注意,CMD中的双引号需要使用反斜杠转义。 Therefore: 因此:

powershell.exe -Command Start-Process PowerShell -ArgumentList '-NoProfile -File \"C:\long name here.ps1\"' -Verb RunAs

If your file has arguments: 如果您的文件包含参数:

powershell.exe -Command Start-Process PowerShell -ArgumentList '-NoProfile -File \"C:\long name here.ps1\" \"Arg1\" \"Arg2\"' -Verb RunAs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Windows CMD 运行 PowerShell 命令的命令 - Command to run PowerShell command from Windows CMD 从Windows命令提示符执行PowerShell脚本 - Execute a PowerShell script from a Windows command prompt 从命令提示符运行 PowerShell 命令(无 ps1 脚本) - Run PowerShell command from command prompt (no ps1 script) 如何从 Windows 命令提示符调用提升的 PowerShell 以执行命令和 PowerShell 脚本文件? - How do I call an elevated PowerShell from Windows command prompt to execute commands and a PowerShell script file? 从 Powershell 中的 CMD 提示符运行命令 - Running a command from the CMD prompt in Powershell 在命令提示符下从 WebClient.DownloadString 运行 Powershell 脚本 - Run Powershell script from WebClient.DownloadString on the Command Prompt PowerShell脚本从任务调度程序运行时未显示命令提示符 - Command prompt not showing when PowerShell script run from task schduler 无法从 Windows PowerShell 命令提示符运行 Java - Cannot run Java from the Windows PowerShell command prompt 如果从命令提示符运行,powershell脚本将在“ if”子句中退出 - powershell script exits in “if” clause if run from command prompt Windows 8.1无法从cmd或PowerShell提示符启动Powershell - “此应用程序无法在您的PC上运行” - Windows 8.1 can't start Powershell from a cmd or powershell prompt - “This app can't run on your PC”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM