I have seen this question here but there not working for me.
I currently have taken apart a psm1 file with 200 functions and separated them into individual files.
Functions are separated into public and private functions
\\nasShare\dbasupport\Powershell\Modules\SQLdbatools\SQLdbatools\public
\\nasShare\dbasupport\Powershell\Modules\SQLdbatools\SQLdbatools\private
This is what my new SQLdbatools.psm1 file looks like
#Get public and private function definition files.
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue )
#Dot source the files
Foreach($import in @($Public + $Private))
{
Try
{
. $import.fullname
}
Catch
{
Write-Error -Message "Failed to import function $($import.fullname): $_"
}
}
Export-ModuleMember -Function $Public.Basename
But the Export-ModuleMember gives me an error that command cant be run from psm1 file
Contents of SQLdbatools.ps1xml file
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>Default</Name>
<ViewSelectedBy>
<TypeName>SQLdbatools.Question</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Width>48</Width>
</TableColumnHeader>
<TableColumnHeader>
<Width>12</Width>
</TableColumnHeader>
<TableColumnHeader>
<Width>5</Width>
</TableColumnHeader>
<TableColumnHeader>
<Label>Owner</Label>
<Width>15</Width>
</TableColumnHeader>
<TableColumnHeader>
<Label>Tags</Label>
<Width>20</Width>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<Wrap />
<TableColumnItems>
<TableColumnItem>
<PropertyName>Title</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Answer_Count</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Score</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.Owner.display_name</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>($_.Tags | Sort-Object) -Join ', '</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>Default</Name>
<ViewSelectedBy>
<TypeName>SQLdbatools.Answer</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Width>50</Width>
</TableColumnHeader>
<TableColumnHeader>
<Label>Owner</Label>
<Width>20</Width>
</TableColumnHeader>
<TableColumnHeader>
<Width>5</Width>
</TableColumnHeader>
<TableColumnHeader>
<Width>11</Width>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<Wrap />
<TableColumnItems>
<TableColumnItem>
<PropertyName>Share_Link</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>$_.Owner.display_name</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Score</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Is_Accepted</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
contents of my SQLdbatools.psd1 file
@{
# Script module or binary module file associated with this manifest.
RootModule = 'SQLdbatools.psm1'
# Version number of this module.
ModuleVersion = '1.0.0'
# Format files (.ps1xml) to be loaded when importing this module
FormatsToProcess = 'SQLdbatools.Format.ps1xml'
# Functions to export from this module
FunctionsToExport = '*'
# Cmdlets to export from this module
CmdletsToExport = '*'
# Variables to export from this module
VariablesToExport = '*'
# Aliases to export from this module
AliasesToExport = '*'
}
I have ran the SQLdbatools.psm1 without
Export-ModuleMember -Function $Public.Basename
it will not fail but the ps1 files don't load. Is there something wrong in the code or am I not setting this Module up correctly.
Error when trying to import module Import-Module SQLdbatools
Import-Module : The specified module 'SQLdbatools' was not loaded because no valid module file was found in any module directory.
At line:1 char:1
+ Import-Module SQLdbatools
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (SQLdbatools:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Export-ModuleMembercmdlet works fine for me within the .psm1 file, and your code works as expected as long as your filenames are in the expected format (e.g., if I want to export a function calledGet-ExampleFunction, in the public directory I'd need to call the fileGet-ExampleFunction.ps1. Additionally, when importing the module, instead of importing withImport-Module SQLdbatools, I'm importing withImport-Module .\SQLdbatoolsso that PowerShell knows to load from the current folder)$env:PSModulePathif you want to import by name.