0

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
3
  • 1
    The Export-ModuleMember cmdlet 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 called Get-ExampleFunction, in the public directory I'd need to call the file Get-ExampleFunction.ps1. Additionally, when importing the module, instead of importing with Import-Module SQLdbatools, I'm importing with Import-Module .\SQLdbatools so that PowerShell knows to load from the current folder) Commented Jul 1, 2022 at 17:24
  • Either use the full path to the module definition file (eg: C:\PATH_TO\SQLdbatools.psd1) or make sure it is at one of the location defined in $env:PSModulePath if you want to import by name. Commented Jul 1, 2022 at 17:25
  • yes. I needed to Import-module .\SQLdbatools and misspelled the ps1xml file. Once I fixed that it worked Commented Jul 1, 2022 at 18:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.