小编San*_*oli的帖子

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

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

Write-Information $MyInvocation.Line -InformationAction Continue
Run Code Online (Sandbox Code Playgroud)

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

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

我尝试过类似的东西:

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

这无法按预期工作,因为标志未正确显示。

参数块:

[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
)
Run Code Online (Sandbox Code Playgroud)

powershell

3
推荐指数
2
解决办法
592
查看次数

标签 统计

powershell ×1