【问题标题】:Calling a powershell cmdlet from ps script从 ps 脚本调用 powershell cmdlet
【发布时间】:2013-01-09 02:43:42
【问题描述】:

我有一个具有以下定义的 cmdlet:

[CmdletBinding(DefaultParameterSetName="Path",
               SupportsShouldProcess=$TRUE)]
param(
  [parameter(Mandatory=$TRUE,Position=0)]
    [String] $Pattern,
  [parameter(Mandatory=$TRUE,Position=1)]
    [String] [AllowEmptyString()] $Replacement,
  [parameter(Mandatory=$TRUE,ParameterSetName="Path",
    Position=2,ValueFromPipeline=$TRUE)]
    [String[]] $Path,
  [parameter(Mandatory=$TRUE,ParameterSetName="LiteralPath",
    Position=2)]
    [String[]] $LiteralPath,
    [Switch] $CaseSensitive,
    [Switch] $Multiline,
    [Switch] $UnixText,
    [Switch] $Overwrite,
    [Switch] $Force,
    [String] $Encoding="ASCII"
)

我将 cmdlet .ps1 文件与调用 cmdlet 的 powershell 脚本文件放在同一文件夹中,如下所示:

Invoke-Expression -Command .\Replace-FileString.ps1 "9595" "NewPort" "c:\temp" - 覆盖

但是,当我执行我的 ps 脚本时,我收到以下错误:

Invoke-Expression:找不到位置参数 接受参数“9595”。 我怎样才能让它工作? 谢谢。

【问题讨论】:

    标签: powershell powershell-cmdlet


    【解决方案1】:

    试试:

    Invoke-Expression -Command '.\Replace-FileString.ps1 "9595" "NewPort" "c:\temp" -Overwrite'
    

    您的命令包含使用引号的参数,因此 PS 认为您的命令已经结束并且这些是新参数(不是 -Command 参数的一部分)。

    【讨论】:

      猜你喜欢
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 2017-06-01
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      • 2015-07-07
      相关资源
      最近更新 更多