I have a nested switch statement inside a switch statement. I am wondering if it is possible to grab the un-nested current item within the scope of the nested switch statement. I know I can accomplish this with a for loop with moderate difficulty but I am hoping there is some elegance I am missing to accomplishing this with switch statements. Below I am try to build some argument processing for one of my scripts.
# Process Args
$currentArg
$recurseOn = $false
:ListArgs switch -regex ($args)
{
"-ou?" { $currentArg = "Organizational Unit"; write-host "Noticed ou switch"}
"-li?k?e?" { $currentArg = "Compare To"; write-host "Noticed like switch"}
"^-re?c?u?r?s?e?$" { $recurseOn = $true; write-host "Recurse switched on..."}
default {
:CurrentSwitch switch ($currentArg)
{
"Organizational Unit" { $OU = $_.$_ ; write-host "OU changed to '$($OU)'..."}
"Compare To" { $Match = $_.$_ ; write-host "Lookup changed to '$($Match)'..."}
}
}
}
I know the present model of $.$ does not work but I am looking for something that could refer to the current item. I am thinking I could have a filler variable that I just fill with the current item regardless of condition and use that within the scope, but maybe there is something more elegant. I have also tried labeling my switch statements and attempted 'ListArgs.$_' to no avail.
Any input would be much appreciated. Thanks in advance!
I did this after posting:
# Process Args
$currentArg
$recurseOn = $false
:ListArgs switch -regex ($args)
{
"-ou?" { $currentArg = "Organizational Unit"; write-host "Noticed ou switch"}
"-li?k?e?" { $currentArg = "Compare To"; write-host "Noticed like switch"}
"^-re?c?u?r?s?e?$" { $recurseOn = $true; write-host "Recurse switched on..."}
default {
$item = $_
:CurrentSwitch switch ($currentArg)
{
"Organizational Unit" { $OU = $item; write-host "OU changed to '$($OU)'..."}
"Compare To" { $Match = $item; write-host "Lookup changed to '$($Match)'..."}
}
}
}
And was able to get it working, but any input on $_ or loop labels would be appreciated. Or is there a better way to do arg processing in PowerShell?