Jered Odegard
Jered Odegard

Reputation: 223

Getting Current Item of Higher Scope While Loop

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?

Upvotes: 1

Views: 459

Answers (2)

Jered Odegard
Jered Odegard

Reputation: 223

I guess since there are no better suggestions out there for argument processing, I answered my own question.

    $item = $_

Upvotes: 1

Roman Kuzmin
Roman Kuzmin

Reputation: 42063

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

Usually I do this and I do not know a better way. Thus, your code would be:

...
default {
    $filler = $_
    :CurrentSwitch switch ($currentArg)
    {
        "Organizational Unit" { use $filler in here }
        "Compare To" { use $filler in here }
    }
}

Upvotes: 0

Related Questions