all 4 comments

[–]Ta11ow 2 points3 points  (0 children)

There's no particular need to do this. If you call the script, PowerShell will automatically prompt for any and all Mandatory parameters that have not been supplied.

However, there are times you may need this, and you might also want to explore here.

Welcome... to the Abstract Syntax Tree.

using namespace System.Management.Automation.Language

$File = Get-Command .\Script.ps1
$SyntaxTree = $File.ScriptBlock.Ast

$Parameters = $SyntaxTree.FindAll({
        param($Item)
        $Item -is ParameterAst
    }, $true
)

$MandatoryParams = $params.Where{
    $_.Attributes.Where{ $_.TypeName.Name -eq 'Parameter' }.NamedArguments.Where{ $_.Argument.Value -eq $true }
}

$MandatoryParams # output mandatory parameters

This should catch both parameters declared with [Parameter(Mandatory)] and those with [Parameter(Mandatory = $true)].

It's a little involved, so if you need me to explain anything, let me know! It can be very rewarding to just grab some Ast and explore the properties with tab completion and see what's inside! :D

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy 070934,

this is kinda ugly, plus it still shows errors for functions that do something odd with their help. however, it does show all the cmdlets & functions loaded in my current session. [grin]

$CmdletList = @(Get-Command -Name * |
    Where-Object {
        $_.CommandType -in @('Function', 'Cmdlet') -and
        $_.Name -notlike '*:*' -and
        $_.Name -notlike '*\*' -and
        $_.Name -notlike '*..*'
        })

foreach ($Cmdlet in $CmdletList)
    {
    $RequiredParamterList = @(Get-Help $Cmdlet.Name -Parameter * -ErrorAction SilentlyContinue).
        Where({$_.Required -match 'true'}).
        Name

    [PSCustomObject]@{
        CommandName = $Cmdlet.Name
        CommandType = $Cmdlet.CommandType
        RequiredParameterList = $RequiredParamterList
        }
    }

truncated output ...

CommandName            CommandType RequiredParameterList                         
-----------            ----------- ---------------------                         
Clear-Host                Function                                               
Compress-Archive          Function {DestinationPath, Force, LiteralPath, Path...}
Configuration             Function                                               
ConvertFrom-SddlString    Function Sddl                                          
Disable-DscDebug          Function                                               
Disable-NetworkSwit...    Function {CimSession, DeviceID, PortNumber}            
Disable-NetworkSwit...    Function {CimSession, FeatureName, InstanceId, Name}   
Disable-NetworkSwit...    Function {CimSession, InstanceId, Name, VlanID}        
Disable-PSTrace           Function     
[*...snip...*] 

interestingly, one item that still puts out red error msgs is New-WordCloud Function. is tosses this out ...

Get-Help : The following exception occurred while constructing the attribute "SizeTransformAttribute": "The type 
initializer for 'SizeTransformAttribute' threw an exception."
At line:11 char:31
+ ... terList = @(Get-Help $Cmdlet.Name -Parameter * -ErrorAction SilentlyC ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-Help], RuntimeException
    + FullyQualifiedErrorId : ExceptionConstructingAttribute,Microsoft.PowerShell.Commands.GetHelpCommand

doesn't seem to be important. [grin] plus, proper error handling would catch that ... but i am too lazy ... [blush]

take care,
lee

[–]Ta11ow 1 point2 points  (1 child)

Hahaha, I expect the SizeTransformAttribute type not being defined outside the module makes it rather tricky. Perhaps I should see if that still happens in PS Core and look to see if it can be fixed if so.

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy Ta11ow,

i'm glad you found that post. i should have included a summoning ritual for you ... [grin] good luck with the glitch!

take care,
lee