Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I exclude a single function from Export-ModuleMember?

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?

like image 847
Anthony Mastrean Avatar asked Jun 26 '12 02:06

Anthony Mastrean


2 Answers

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.

like image 65
Mike Shepard Avatar answered Oct 04 '22 01:10

Mike Shepard


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.

like image 42
ravikanth Avatar answered Oct 03 '22 23:10

ravikanth