【问题标题】:Cmd doesn't pass arguments to powershell scriptCmd 不会将参数传递给 powershell 脚本
【发布时间】: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] 和 PowerShell PSVersion 5.1.19041.1 中工作正常,并使用以下脚本主体 $MyInvocation.BoundParameters
  • MyScript.ps1的内容是什么,具体是param
  • 嗯,我只是注意到它在另一台机器上运行良好。我无法访问原始机器,因此无法检查那里正在运行哪些版本。

标签: powershell cmd


【解决方案1】:

因为它们在命令请求之外,也就是说,你的脚本调用。

试试这个方法...

# Contents of hello.ps1
Get-Content -Path 'D:\Scripts\hello.ps1'
# Results
<#
[CmdletBinding(SupportsShouldProcess)]

Param
(
    [Parameter(ParameterSetName="Domain",Mandatory=$true)]
    [string]$ScriptParam1,
    [Parameter(ParameterSetName="Domain",Mandatory=$true)]
    [string]$ScriptParam2,
    [Parameter(ParameterSetName="Domain",Mandatory=$true)]
    [string]$ScriptParam3
)



$host
$PSversionTable
Write-Host 'Hello World'
Get-Date
#>
powershell.exe -NoExit -NoLogo -NoProfile -ExecutionPolicy ByPass "D:\Scripts\hello.ps1 -ScriptParam1 param1 -ScriptParam2 param2 -ScriptParam3 param3"
# Results
<#
Name             : ConsoleHost
Version          : 5.1.19041.1
...

Key   : PSVersion
Value : 5.1.19041.1
...

Hello World
...
#>

powershell.exe -NoExit -NoLogo -NoProfile -ExecutionPolicy ByPass "D:\Scripts\hello.ps1 -ScriptParam1 param1 -ScriptParam2 param2 -ScriptParam3 param3"
# Results
<#
C:\>ver

Microsoft Windows [Version 10.0.19041.388]

C:\>powershell.exe -NoExit -NoLogo -NoProfile -ExecutionPolicy ByPass "D:\Scripts\hello.ps1 -ScriptParam1 param1 -ScriptParam2 param2 -ScriptParam3 param3"


Name             : ConsoleHost
Version          : 5.1.19041.1
...

Key   : PSVersion
Value : 5.1.19041.1
...

Hello World
...


C:\>ver

Microsoft Windows [Version 10.0.19041.388]

C:\>powershell.exe -NoExit -NoLogo -NoProfile -ExecutionPolicy ByPass "D:\Scripts\hello.ps1"

cmdlet hello.ps1 at command pipeline position 1
Supply values for the following parameters:
ScriptParam1: 1
ScriptParam2: 2
ScriptParam3: 3


Name             : ConsoleHost
Version          : 5.1.19041.1
...

Key   : PSVersion
Value : 5.1.19041.1
...

Hello World
...
#>

当您已经在 PowerShell 实例中时,与从 cmd.exe 启动实例时的解析方式略有不同。 Cmd.exe 并首先阅读它,而 cmd.exe 不知道那些 -ScriptParam* 是什么,因此它们被忽略了。

按照所示示例。

【讨论】:

  • 嗯,我在另一台机器上测试了相同的代码,它运行得非常好。不知道是什么导致了我的另一台机器上的问题。 cmd版本有区别吗?
  • 不,cmd.exe 多年来一直如此。没有真正的变化。那么,你有没有在那些窒息机器上尝试过我上面的建议?
  • 还没有。需要几天才能再次访问这些内容。我会回来报告的。
  • 不用担心,理解。
  • 好吧测试它并且它工作。令人惊讶的是,我的版本现在也能正常工作,我不知道为什么以前不能......
猜你喜欢
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 2015-06-07
  • 2017-05-08
  • 2019-07-07
  • 2014-07-22
相关资源
最近更新 更多