【问题标题】:Pre-fill powershell parameters with modify option使用修改选项预填充 powershell 参数
【发布时间】:2015-03-09 20:11:00
【问题描述】:

我有一个类似这样的 powershell 脚本:

[CmdletBinding()]
Param(
   [Parameter(Mandatory=$True)]
   [string]$age="8"
   )

process
{
    Write-Host "Your age is $age"
}

我如何修改这个脚本,以便 shell 询问我的年龄并在它旁边放 8,但允许用户在按 enter 之前更改值?

【问题讨论】:

    标签: powershell powershell-2.0 powershell-ise powershell-4.0


    【解决方案1】:

    不确定这是否是您要求的,但我会这样做。

     [CmdletBinding()]
        Param(
           [Parameter(Mandatory=$True)]
           [string]$age="8"
           )
    
        process
        {
        $yourage = "8" + $age 
            Write-Host "Your age is $yourage"
        }
    

    如果年龄 = 29 返回:你的年龄是 829

    【讨论】:

    • 我明白你的意思。但我希望 powershell 将年龄提示显示为age:5,然后允许我替换它或在提示本身上使用它。无法让它工作......
    • 听起来你需要Read-InputBoxDialogRead-InputBoxDialog -Message "Enter Your Age" -WindowTitle "Example" -DefaultText "5"
    • 嗯...我不认为我需要 windows 对话框,但这是唯一的解决方案还是他们是另一种方式?我应该以其他方式实现目标吗?
    • 这是我能想到的完成你需要的最好方法,除非你可以用Read-HostWrite-Host做同样的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多