【发布时间】:2020-11-26 20:29:19
【问题描述】:
我有一个我想从命令行运行的 powershell 脚本(cmd,而不是 powershell):
powershell.exe -NoLogo -NoProfile -ExecutionPolicy ByPass -File "MyScript.ps1" -ScriptParam1 2020 -ScriptParam2 "bla" -ScriptParam3 "blub"
当我将此代码粘贴到 powershell 中时,一切正常,但是当我将其粘贴到 cmd 中时,powershell 启动并提示我输入脚本参数,因为它们是强制性的。为什么 cmd 无法识别这些参数?
这是 MyScript 的内容
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[int] $ScriptParam1,
[Parameter(Mandatory)]
[string] $ScriptParam2,
[Parameter(Mandatory)]
[string] $ScriptParam3
)
$PSVersionTable
【问题讨论】:
-
对我来说效果很好。也可以删除 -File 和参数名称。 powershell.exe -NoLogo -NoProfile -ExecutionPolicy ByPass MyScript.ps1 2020 "bla" "blub"
-
在 Microsoft Windows
[Version 10.0.19041.388]和 PowerShellPSVersion 5.1.19041.1中工作正常,并使用以下脚本主体$MyInvocation.BoundParameters -
MyScript.ps1的内容是什么,具体是param -
嗯,我只是注意到它在另一台机器上运行良好。我无法访问原始机器,因此无法检查那里正在运行哪些版本。
标签: powershell cmd