典型例子,当你函数的参数为“mandatory”属性时,Powershell中当用户遗漏参数时将会提醒他。
function Get-Something { param ( [Parameter(Mandatory=$true)] $Path ) "You entered $Path" }
没有参数时结果像这样:
这里有个选择方法:如果用户忽略了-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
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
这个好,有些小工具里面用起来超爽。
你不在,前面有几个小技巧英文实在看不明白,直接跳过了。