如何从Windows命令提示符(CMD)运行Long Powershell脚本 [英] How to Run Long Powershell script from Windows Command Prompt (CMD)

查看:85
本文介绍了如何从Windows命令提示符(CMD)运行Long Powershell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从命令提示符下启动一个长Powershell脚本,名称为"long name here.ps1".但是我也试图确保它作为Powershell中的管理员命令运行.我已在Powershell中设置了所有执行策略,因此我针对Powershell使用了 ss64 set-executionpolicy命令指南使PowerShell工作.但是我正在尝试使用

I tried to launch a long powershell script with the name "long name here.ps1" from command prompt. But I am also trying to ensure that it runs as an administrator command in powershell. I have all execution policies in powershell set accordingly I used the ss64 set-executionpolicy command guide for powershell to get powershell working. But I am trying to use the solution from another stackoverflow question that talks about running commands as administrator. 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. I am currently using the following command:

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}"

我在 https://ss64.com/ps/powershell.html底部,其中包含有关如何以管理员身份运行powershell命令的详细信息.该代码的问题是我的Powershell脚本1.具有参数,而2.具有长名称.我尝试了此命令的许多不同迭代,但均未成功,下面列出了不起作用的迭代:

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. The problem with that code is that my powershell script 1. has arguments, and 2. has a long name. I have tried many different iterations of this command with no success, and the ones that DON'T work are listed below:

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}"

powershell.exe -command "&{ Start-Process 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.

推荐答案

使用 PowerShell.exe -Command 时,无需使用引号.例如,您可以运行以下命令:

When you use PowerShell.exe -Command you don't need to use quotes. For example, you can run the following:

PowerShell.exe -Command Get-Service 'wuauserv'

-Command 之后的所有内容均被解释为命令.还要注意,CMD中的双引号需要使用反斜杠进行转义.因此:

Everything after -Command is interpreted as the command. Note also that double quotes in CMD need escaping with a backslash. 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

这篇关于如何从Windows命令提示符(CMD)运行Long Powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆