powershell - 如何显示使用扩展参数调用的命令行

我发现在 PowerShell (.ps1) 脚本中我可以显示启动脚本的命令行:

Write-Information $MyInvocation.Line -InformationAction Continue

但是,当我在命令行上传递变量时,这些变量在使用上面的命令显示时不会展开。

为了方便起见,我需要扩展它们,因为我需要任何人都能够在没有定义变量的情况下复制粘贴命令行。

我试过类似的方法:

# Display command
$fullCommand = $MyInvocation.MyCommand.Name
$MyInvocation.BoundParameters.Keys | ForEach {
    $fullCommand += " -$($_) $($PSBoundParameters.Item($_))"
}
Write-Information $fullCommand -InformationAction Continue

这不能按预期工作,因为标志没有正确显示。

一个参数 block :

[CmdletBinding(DefaultParameterSetName="noUploadSet")]
param(
    [switch] $SkipGetList,
    [switch] $DisableHistory,
    [string] $RefVersion,
    [datetime] $ComputationDate,
    [string] $RefEnv,
    [string] $NewEnv,
    [Parameter(Mandatory=$true)][string] $Perimeter,
    [string] $ComputationType = "Test",
    [string] $WorkingDir,
    [string] $NewServiceName,
    [Parameter(ParameterSetName="noUploadSet")][switch] $DebugMode,
    [Parameter(ParameterSetName="uploadSet")][switch] $UploadNewService,
    [string] $ScenarioPath,
    [string] $User
)

最佳答案

补充Mathias R. Jessen's helpful answer解决方案:

  • 也支持数组类型的参数
  • 使用更熟悉的语法,通过:
    • 仅在必要时引用参数
    • 使用 : 仅在必要时分隔参数名称和值,即仅用于 [switch] 类型的参数,$false 是通过。
      • 表示 -Switch 参数,其隐含值为$true,只是-Switch而不是-开关:$true

注意:

  • 如果遇到一个值,如果重新调用生成的字符串,它可能起作用 - 例如 [hashtable][ pscustomobject] 实例 - 发出警告。

  • [datetime][datetimeoffset] 实例由它们的文化不变字符串表示表示,因为 PowerShell,在字符串中-f 以外的操作,使用不变区域性进行格式化(请参阅 this answer 了解背景信息)。因此,无论当前的文化是什么,表示都应该有效。

    • 但是,这些表示仅限于 的粒度(例如,12/31/2020 10:57:35);如果您需要保留亚秒级值,请使用 .ToString('o') 字符串化,如 Mathias 的回答所示;这也可能更适合避免默认字符串化的月份优先格式(您也可以选择更短的自定义格式作为 'o' 的替代,往返格式)。

    • 顺便说一句:如果使用日期的字符串表示形式调用编译的 cmdlet(而不是用 PowerShell 编写的代码),则解析会出乎意料 文化-敏感 - 不幸的是,由于向后兼容性问题,这种不一致不会得到解决 - 参见 GitHub issue #6989 .

function Get-Foo {

  [CmdletBinding()]
  param(
      [switch] $SkipGetList,
      [switch] $DisableHistory,
      [datetime] $ComputationDate,
      [string] $RefVersion,
      [string] $WorkingDir,
      [int[]] $Indices
  )

  # Get this function's invocation as a command line 
  # with literal (expanded) values.
  '{0} {1}' -f `
    $MyInvocation.InvocationName, # the function's own name, as invoked
    ($(foreach ($bp in $PSBoundParameters.GetEnumerator()) { # argument list
      $valRep =
        if ($bp.Value -is [switch]) { # switch parameter
          if ($bp.Value) { $sep = '' } # switch parameter name by itself is enough
          else { $sep = ':'; '$false' } # `-switch:$false` required
        }
        else { # Other data types, possibly *arrays* of values.
          $sep = ' '
          foreach ($val in $bp.Value) {
            if ($val -is [bool]) { # a Boolean parameter (rare)
              ('$false', '$true')[$val] # Booleans must be represented this way.
            } else { # all other types: stringify in a culture-invariant manner.
              if (-not ($val.GetType().IsPrimitive -or $val.GetType() -in [string], [datetime], [datetimeoffset], [decimal], [bigint])) {
                Write-Warning "Argument of type [$($val.GetType().FullName)] will likely not round-trip correctly; stringifies to: $val"
              }
              # Single-quote the (stringified) value only if necessary
              # (if it contains argument-mode metacharacters).
              if ($val -match '[ $''"`,;(){}|&<>@#]') { "'{0}'" -f ($val -replace "'", "''") }
              else { "$val" }
            }
          }
        }
      # Synthesize the parameter-value representation.
      '-{0}{1}{2}' -f $bp.Key, $sep, ($valRep -join ', ')
    }) -join ' ') # join all parameter-value representations with spaces

}

# Sample call:
Get-Foo `
  -SkipGetList `
  -DisableHistory:$false `
  -RefVersion 1.0b `
  -WorkingDir "C:\dir A\files'20" `
  -ComputationDate (Get-Date) `
  -Indices (1..3)

上面的代码产生了以下单行字符串(为了便于阅读,这里只注释并分成多行):

Get-Foo `
  -SkipGetList `  # switch syntax was preserved
  -DisableHistory:$false # negated switch (rare) was preserved
  -RefVersion 1.0b `  # string parameter NOT quoted, because not needed
  -WorkingDir 'C:\dir A\files''20' ` # quoting needed, ' escaped as ''
  -ComputationDate '12/31/2020 10:40:50' ` # culture-invariant date string
  -Indices 1, 2, 3  # array preserved

https://stackoverflow.com/questions/65520262/

相关文章:

hash - 如何从 elixir 中的 map 创建 `hash` 或 `md5`?

python - 如何从 Python 字典中的键中删除尾随空格?

java - JDBC 向数据库中插入变量

typescript - TypeScript 如何推断回调参数类型

r - 无法为 R 中的列表替换 NA 的拼写错误

scala - 基于Map从List[String]转换为List[Int]

swift - 如何在 SwiftUI 上启用核心数据加密?

node.js - 如何动态地从 Firestore 数组中删除多个元素?

python - Pandas:如何用另一个值替换 int 子字符串

angular - 引用错误 : Can't find variable: globalThis