Is it possible to exclude one 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 only one function. It will be easier for me to exclude this function than to list all the included functions. Anyway to achieve this?

+5
source share
3 answers

My fallback on function exclusion is to use the name of the verb-name for the functions I want to export, and use the initial caps for everything else.

Then Export-ModuleMember -function *-*take care of it.

+16
source

script, , ( , 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
    }
}

, v2 ISE Function Explorer. , , ISE. , . , , .

, , Export-ModuleMember!

$functions | ?{ $_.FunctionName -ne "your-excluded-function" }

PowerShell v3, .

+4

, PowerShell V3, ravikanth ( V2 ), PSParser:

Add-Type -Path "${env:ProgramFiles(x86)}\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll"

Function Get-PSFunctionNames([string]$Path) {
    $ast = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$null, [ref]$null)
    $functionDefAsts = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true)
    $functionDefAsts | ForEach-Object { $_.Name }
}

Export-ModuleMember -Function '*'

, , :

Export-ModuleMember -Function ( (Get-PSFunctionNames $PSCommandPath) | Where { $_ -ne 'MyPrivateFunction' } )

, PowerShell V3 , 3 $PSCommandPath.

0
source

All Articles