在switch语句中有一个嵌套的switch语句。我想知道是否有可能在嵌套的switch语句的范围内抓取未嵌套的当前项目。我知道我可以通过适当难度的for循环来完成这个任务,但我希望能够通过switch语句完成这项工作,但我缺少一些优雅。下面我尝试为我的一个脚本构建一些参数处理。在循环中获取更高范围的当前项目
# 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)'..."}
}
}
}
我知道$ 。$本模型不工作,但我期待的东西,可以参考当前项目。我想我可以有一个填充变量,我只需填写当前项目而不考虑条件,并在范围内使用它,但也许有更优雅的东西。我也尝试给我的switch语句添加标签,并尝试'ListArgs。$ _'无济于事。
任何输入将不胜感激。提前致谢!
我这样做发布后:
# 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)'..."}
}
}
}
,并能够得到它的工作,但在$任何输入_或环标签,将不胜感激。还是有更好的方法在PowerShell中进行参数处理?
我想你已经回答了你自己的问题。 '$ item = $ _'是我该怎么做的。 – 2010-08-25 16:08:16
是的,它似乎是最好的解决方案 – 2010-08-25 16:36:07