Powershell使用Dialog设定必选参数 2


典型例子,当你函数的参数为“mandatory”属性时,Powershell中当用户遗漏参数时将会提醒他。

function Get-Something
{
      param
      (
            [Parameter(Mandatory=$true)]
            $Path
      )
      "You entered $Path"
}

没有参数时结果像这样:

123123

这里有个选择方法:如果用户忽略了-Path参数,这个函数将调用Forms中的OpenFile对话框来提示用户输入参数:

function Get-Something
{
      param
      (
            $Path = $(
              Add-Type -AssemblyName System.Windows.Forms
              $dlg = New-Object -TypeName  System.Windows.Forms.OpenFileDialog
              if ($dlg.ShowDialog() -eq 'OK') { $dlg.FileName } else { throw 'No Path submitted'}
            )
      )

      "You entered $Path"
}

原文地址:Mandatory Parameter with a Dialog

本文链接: https://www.pstips.net/mandatory-parameter-with-a-dialog.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

2 条评论 “Powershell使用Dialog设定必选参数