开发者

PowerShell and Regex frustration

Ok, so I have a file called functions.ps1 that contains only function code: something like this:

##--------------------------------------------------------------------------
##  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
}

Now, the last function in this file (and the one relevant to my issue) is this:

##--------------------------------------------------------------------------
##  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}
}

The idea here is that fr开发者_开发知识库om the console I can dot source the function.ps1 file, and by firing off List-Functions, I'll get a list of all the functions in the functions file.

Except when run, List-Function returns something like this:

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

Everything is cool, except that last bit of code. I know that it's only matching the pattern I've given it, but it's annoying me something furious.

I know my Regex-fu is weak, and I tried altering List-Functions to filter that bit out, but I am having no joy. Can anyone point out what I can do to get this working correctly?

My solution (which is ugly), was to change List-Functions to this:

##--------------------------------------------------------------------------
##  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}
}

I told you it was ugly, but it works ;)


You need to match only lines that begins with the string ##. So change your regex to

$func_1 = "^##  FUNCTION"

Better solution: Consider using modules. Store your functions Some-Function1 and Some-Function2 to file MyModule.psm1. Then:

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

How you can try the regex:

@'
##--------------------------------------------------------------------------
##  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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜