【发布时间】:2018-05-10 23:11:42
【问题描述】:
我尝试从命令提示符启动一个名为“long name here.ps1”的长 powershell 脚本。但我也试图确保它在 powershell 中作为管理员命令运行。我在 powershell 中设置了所有执行策略,因此我使用了ss64 set-executionpolicy command guide for powershell 来让 powershell 工作。但我正在尝试使用来自another stackoverflow question 的解决方案,该解决方案涉及以管理员身份运行命令。我正在运行一个批处理脚本,该脚本需要以管理员身份执行 powershell 脚本 (.ps1),我不介意 UAC 提示用户还是输入密码。我目前正在使用以下命令:
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. 有一个长名称。我已经尝试了这个命令的许多不同的迭代,但都没有成功,下面列出了那些不起作用的:
powershell.exe -command "&{ Start-Process 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}"
另外,我完全不知道如何将参数发送到实际脚本。
【问题讨论】:
-
This link 可能是相关的
-
我没有达到我的命令的最大长度。问题是 powershell 的 -file 选项。它无法识别我的文件名,因此根本不运行它。
-
我知道任务计划程序有 256 个字符的限制,但我通过以与 OP 尝试运行他的文件相同的方式将 291 个字符参数传递给 PowerShell 脚本来测试它,它工作正常。如果有限制,那将是不合理的。
标签: powershell cmd command-prompt command-line-arguments windows-nt