2010-08-25 67 views
1

在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中進行參數處理?

+0

我想你已經回答了你自己的問題。 '$ item = $ _'是我該怎麼做的。 – 2010-08-25 16:08:16

+0

是的,它似乎是最好的解決方案 – 2010-08-25 16:36:07

回答

1

我想因爲沒有更好的建議,那裏的參數處理,我回答了我自己的問題。

$item = $_ 
0

我想我可以有一個填料 變量,我剛剛與 當前項目填補不論好壞 和使用範圍

一般於我這樣做,我不知道一個更好的方法。因此,你的代碼將是:

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

啊,你已經在發佈後完成了。代碼很好,恕我直言。 – 2010-08-25 16:11:23

+0

事實上,在類似的情況下也需要同樣的技巧,不僅僅是'switch',處理'$ _':'switch',For-EachObject,$ _作爲事件處理器中事件參數的所有東西,還有一些其他。有時候'$ _'稍微超負荷。 – 2010-08-25 16:19:04

相關問題