Typically, when you mark a function parameter as "mandatory", PowerShell will prompt the user when the user omits the parameter:- function Get-Something
- {
- param
- (
- [Parameter(Mandatory=$true)]
- $Path
- )
-
- "You entered $Path"
- }
复制代码 The result looks like this:
PS > Get-Something
cmdlet Get-Something at command pipeline position 1
Supply values for the following parameters:
Path:
|
Here is an alternative: if the user omits -Path, the function opens an OpenFile dialog:- 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"
- }
复制代码 http://powershell.com/cs/blogs/tips/archive/2014/02/06/mandatory-parameter-with-a-dialog.aspx |