Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting Current Item of Higher Scope While Loop
    primarykey
    data
    text
    <p>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.</p> <pre><code># 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)'..."} } } } </code></pre> <p>I know the present model of $<em>.$</em> 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.</p> <p>Any input would be much appreciated. Thanks in advance!</p> <p>I did this after posting: </p> <pre><code> # 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)'..."} } } } </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload