Powershell - create an -all switch

Im looking for a better way to create an -all switch. For instance

. .\function1
. .\function2
. .\function3

Function TestSwitches {
Param(
[Parameter(Mandatory=$false)]
[switch]$useFunction1,
[switch]$useFunction2,
[ switch]$useFunction3
)
Begin{ ]
Process{
if($useFunction1.ispresent){
$function1results=function1 stuff
}
if($useFunction2.ispresent){
$function2results=function2 stuff
]
if($useFunction3.ispresent){
$function3results=function3 stuff
}
}
End{
Return $function1results,$function2results,$function3results
}

Is there a way to create an [switch]$all and have the script run all the swtiches?

I’ve used if($all.ispresent){$useFunction1 = $true
$useFunction2 = $true
$useFunction3 = $true
}

But, that gets a little cumbersome everytime you add a function.
Is there a more elegant approach?

Thanks.

Stephen,
Welcome to the forum. :wave:t4:

How often do you add new functions? About how many functions are we talking?

If all of your function would have a common but very unique name component - something like prefix or suffix like “BigSteveNo1” - even just in their alias - you could use

Get-Command -Noun '*BigSteveNo1*' |
    ForEach-Object {
        & $_.Name
    }

Of course that only works if all of these function take the same parameters or do not need parameters.

Regardless of all that:
When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Olaf,

Thanks for the suggestion. Ill see if I can make that work.

Im going to have quite a few functions when Im done. Perhaps 50?

Ill definitely use the preformatted code next time I post.

Thank you.

Steve

Wow, that sounds pretty unusual … would you like to elaborate a little more detailed what these functions are about and why you need to have them called from a single function? … and how would you do that when you only want to call 20 or 30 of them? … would you provide 20 or 30 switch parameters to the call function then?

Mostly gathering Active Directory information.

How many users, how many stale users, how many computers, how many stale computer accounts, how many gpos, linked gpos, unlinked gpos., etc.

Could run everything at once and output everything to csv or, use a switch to only choose what you want.

Still trying to figure out how to call 20 of them, etc.

~WRD0001.jpg

Is that a question?

What is that?

No, sorry – just a statement. Im still trying to figure out all of that…

Thanks!

Stephen,

if (($useFunction1.ispresent) -or ($AllFlag.ispresent))

You may also want to read up on parametersets, as you can prevent the user from selecting individual functions and using the all parameter.

Darwin

1 Like

Darwin,

Thanks!. I hadn’t thought about that solution. That’s awesome.

You could do this with a bit of understanding about how commands work.

Basically:

  • A function/script knows itself:
    $myInvocation.MyCommand
  • A command can get it’s own metadata [Management.Automation.CommandMetaData]$MyInvocation.MyCommand
  • CommandMetaData has the set of parameters
  • You can filter those parameters by type
  • You can then set $psBoundParameters and each variable to the value of all.
Function TestSwitches {
    Param(
    [Parameter(Mandatory=$false)]
    [switch]$useFunction1,
    [switch]$useFunction2,
    [switch]$useFunction3,
    [switch]$All
    )
    Begin{ }
    Process {
        if ($All) {
            foreach (
                $param in ([Management.Automation.CommandMetaData]$MyInvocation.MyCommand).Parameters.Values
            ) {
                if ($param.Name -eq 'All' -or $param.ParameterType -ne [switch]) {
                    continue
                }
                $PSBoundParameters[$param.Name] = $All
                $ExecutionContext.SessionState.PSVariable.Set($param.Name, $all)
            }
        }

        if($useFunction1.ispresent){
            $function1results="stuff"
        }
        if($useFunction2.ispresent){
            $function2results="morestuff"
        }
        if($useFunction3.ispresent){
            $function3results="evenMoreStuff"
        }
    }
    End{
        Return $function1results,$function2results,$function3results
    }
}

TestSwitches -All