2011-02-23 29 views
0

好了,我有一個文件名爲functions.ps1只包含功能代碼:是這樣的:PowerShell和正則表達式無奈

##-------------------------------------------------------------------------- 
## FUNCTION.......: some-Function1 
## PURPOSE........: 
## EXAMPLE........: 
## REQUIREMENTS...: PowerShell 2.0 
## NOTES..........: 
##-------------------------------------------------------------------------- 
Function Some-Function1 
{ 
Code goes here 
} 
##-------------------------------------------------------------------------- 
## FUNCTION.......: some-Function2 
## PURPOSE........: 
## EXAMPLE........: 
## REQUIREMENTS...: PowerShell 2.0 
## NOTES..........: 
##-------------------------------------------------------------------------- 
Function Some-Function2 
{ 
Code goes here 
} 

現在,此文件中的最後一個函數(和在一個有關我的問題)是這樣的:

##-------------------------------------------------------------------------- 
## FUNCTION.......: List-Functions 
## PURPOSE........: 
## EXAMPLE........: 
## REQUIREMENTS...: PowerShell 2.0 
## NOTES..........: 
##-------------------------------------------------------------------------- 
function List-Functions 
{ 
    $func_1 = "## FUNCTION" 
    $func_2 = Get-Content \\Server\scripts\functions.ps1 
    $func_2 | select-string -pattern $func_1 | foreach {write-host $_.line} 
} 

這裏的想法是,從控制檯,我可以點源function.ps1文件,併發射了列表的功能,我會在所有的功能列表功能文件。

運行時除外,列表,函數返回是這樣的:

## FUNCTION.......: some-Function1 
## FUNCTION.......: some-Function2 
## FUNCTION.......: List-Functions 
$func_1 = "## FUNCTION" 

一切都很酷,但該代碼最後一位。我知道這只是與我給出的模式相匹配,但這讓我很惱火。

我知道我的Regex-fu很弱,我嘗試改變列表函數來過濾掉這一點,但我沒有快樂。任何人都可以指出我能做些什麼才能正確地工作?

我的解決方案(這是醜陋的),是改變列表的功能,這一點:

##-------------------------------------------------------------------------- 
## FUNCTION.......: List-Functions 
## PURPOSE........: 
## EXAMPLE........: 
## REQUIREMENTS...: PowerShell 2.0 
## NOTES..........: 
##-------------------------------------------------------------------------- 
function List-Functions 
{ 
    $char = [char] '#' 
    $func_1 = $char + $char + " FUNCTION" 
    $func_2 = Get-Content \\server\scripts\functions.ps1 
    $func_2 | select-string -pattern $func_1 | foreach {write-host $_.line} 
} 

我告訴你這是醜陋,但它的工作原理;)

回答

3

您只需匹配線以字符串##開頭。所以,你的正則表達式改爲

$func_1 = "^## FUNCTION" 

更好的解決方案:考慮使用模塊。將您的功能Some-Function1Some-Function2存儲到文件MyModule.psm1。然後:

PS> Import-Module d:\MyModule.psm1 
# or with -DisableNameChecking to suppres warning messages 
PS> Import-Module d:\MyModule.psm1 -DisableNameChecking 

PS> Get-Command -module MyModule | Select -expand Name 
Some-Function1 
Some-Function2 

您如何嘗試正則表達式:

@' 
##-------------------------------------------------------------------------- 
## FUNCTION.......: some-Function1 
##-------------------------------------------------------------------------- 
Function Some-Function1 
{ 
Code goes here 
} 
##-------------------------------------------------------------------------- 
## FUNCTION.......: some-Function2 
##-------------------------------------------------------------------------- 
Function Some-Function2 
{ 
Code goes here 
} 
##-------------------------------------------------------------------------- 
## FUNCTION.......: List-Functions 
##-------------------------------------------------------------------------- 
function List-Functions 
{ 
    Get-Content d:\temp\sotest.ps1 | 
     select-string -pattern "^## FUNCTION" | 
     Select -exp Line 
} 
'@ | Set-Content d:\temp\sotest.ps1 
. d:\temp\sotest.ps1 
List-Functions 
+0

,您可以通過'GET-Command'通過'CommandType'參數過濾命令的輸出(別名'型')例如'gcm -m MyModule -type函數'。 – 2011-02-23 06:00:45

+0

@stej 這是我最初嘗試的,它不工作。我認爲這只是我,很高興知道我至少可以在POSH正則表達式中正確使用錨字符! – JoeG 2011-02-23 20:17:37

+0

當我用克拉運行它時,它在控制檯沒有任何返回。有一些POSH不喜歡那個正則表達式的語法... – JoeG 2011-02-23 20:18:40