I have a large set of functions defined in a PowerShell script module. I want to use Export-ModuleMember -Function *
, but I want to exclude just one function. It'll be easier for me to exclude this one function than to list all the included functions. Is there anyway to achieve this?
My stock answer about excluding functions is to use verb-noun naming for functions I want to export, and use initial caps for everything else.
Then, Export-ModuleMember -function *-*
takes care of it.
Find all functions in a script and then filter based on what you want to exclude (assuming PowerShell v2):
$errors = $null
$functions = [system.management.automation.psparser]::Tokenize($psISE.CurrentFile.Editor.Text, [ref]$errors) `
| ?{(($_.Content -Eq "Function") -or ($_.Content -eq "Filter")) -and $_.Type -eq "Keyword" } `
| Select-Object @{"Name"="FunctionName"; "Expression"={
$psISE.CurrentFile.Editor.Select($_.StartLine,$_.EndColumn+1,$_.StartLine,$psISE.CurrentFile.Editor.GetLineLength($_.StartLine))
$psISE.CurrentFile.Editor.SelectedText
}
}
This is the technique I used for v2 to create a ISE Function Explorer. However, I don't see a reason why this won't work with plain text outside ISE. You need to workaround the caret line details though. This is just an example on how to achieve what you want.
Now, filter what is not required and pipe this to Export-ModuleMember
!
$functions | ?{ $_.FunctionName -ne "your-excluded-function" }
If you are using PowerShell v3, the parser makes it a lot easier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With