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 from 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 toBetter solution: Consider using modules. Store your functions
Some-Function1andSome-Function2to fileMyModule.psm1. Then:How you can try the regex: