Reputation: 3868
I have a module in which I am trying to export a function/alias. The item I am trying to export is a script file foo.ps1 which does not declare a named function, but I want to export it as available in the session as being able to invoke foo. Is this supported? If so, how do I configure the Export-ModuleMember call?
Here is a snippet from the very top of the file:
[CmdletBinding(DefaultParameterSetName='default')]
param(
[Parameter(Position=0,Mandatory=$false,HelpMessage="You must specify which task to execute.")]
[ValidateSet('install','update', 'uninstall', 'outdated', 'init', 'help', '?')]
....
There are no function definitions. Rather the function name is the file name.
Upvotes: 1
Views: 1465
Reputation: 2888
try this
PS II> # UNTESTED
PS II> $excmd = gcm G:\inventory.ps1
PS II> set-item function:$($excmd.name.replace('.','_')) -val $excmd.scriptblock
PS II> inventory_ps1
Upvotes: 0
Reputation: 3868
I was able to load the script contents into a ScriptBlock and create a function out of it.
[string]$content = Get-Content $here\foo.ps1 -Delimiter ([Environment]::NewLine)
$block = [ScriptBlock]::Create($content)
Invoke-Expression "function foo { $block }"
Export-ModuleMember -function foo
This works, but is far from ideal.
Upvotes: 1
Reputation: 126912
Not tested, but try to dot-source the script in the module psm1 file and the use Export-ModuleMember
to export its function(s).
Upvotes: 0