1

When I import my module I can't access the exposed members.

Placed my module in C:\Program Files\WindowsPowerShell\Modules.

When I import my module in powershell by: Import-Module StuiterModule -Verbose

and then enter Invoke-Reboot it gives the following error:

Invoke-Reboot : The term 'Invoke-Reboot' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or operable program. CHeck the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Invoke-Reboot
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Invoke-Reboot:String [], CommandNotFoundExeption
    + FullyQualifiedErrorId : CommandNotFoundException

Does anyone have an idea what I'm doing wrong?


Update

When I put -Force behind the Import Module everything works. Why is that and how can I fix this?


Code:

StuiterModule.psm1

$Public = @( Get-ChildItem -Path "$PSScriptRoot\Public\*.ps1" )
$Private = @( Get-ChildItem -Path "$PSScriptRoot\Private\*.ps1" )

@($Public + $Private) | ForEach-Object {
    Try {
        . $_.FullName
    } Catch {
        Write-Error -Message "Failed to import function $($_.FullName): $_"
    }
}

Export-ModuleMember -Function $Public.BaseName

StuiterModule.psd1

#
# Module manifest for module 'StuiterModule'
#
# Generated by: StuiterSlurf
#
# Generated on: 29-5-2018
#

@{
# Script module or binary module file associated with this manifest.
RootModule = 'StuiterModule.psm1'

# Version number of this module.
ModuleVersion = '1.0.0'

# ID used to uniquely identify this module
GUID = '0254592e-b712-4d70-844c-6e38cec20ee5'

# Author of this module
Author = 'StuiterSlurf'

# Copyright statement for this module
Copyright = '(c) 2018 StuiterSlurf. All rights reserved.'

# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '5.0'

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = 'Invoke-Reboot', 'Start-Program', 'Update-Program'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
    PSData = @{}
}
}

Public/Invoke-Reboot.ps1

# Reboot system

Function Invoke-Reboot {
[cmdletbinding()]
Param()

Write-Verbose "Starting reboot"
Restart-Computer
} 
Community
  • 1
  • 1
StuiterSlurf
  • 2,464
  • 4
  • 34
  • 55
  • If I include the module with the -Force parameter I can do anything. Without it I can't call any functions – StuiterSlurf May 29 '18 at 14:30
  • If I understand you correctly you mean that I need to define the methods in `FunctionsToExport`. Because I defined them over there as you can read I gues correctly? – StuiterSlurf May 29 '18 at 14:41
  • 1
    Looks like your function is named `Invoke-RebootRpcServices` not `Invoke-Reboot`. – BenH May 29 '18 at 14:46
  • 3
    See what `Get-Module StuiterModule` gives you before loading. If you've already loaded a previous version and you have not incremented the `ModuleVersion`, the `-Force` is necessary to make PowerShell load your new version (or rather, your changes). – Jeroen Mostert May 29 '18 at 14:54
  • @BenH My bad that was for testing – StuiterSlurf May 29 '18 at 15:06
  • 2
    @StuiterSlurf The combination of that change, with the behavior that Jereon Mostert described is likely the cause to your issue. If you imported the module with the old function name, but did not increment the version number. When you changed the function name, you would need to use `-Force` to reimport the module with the new module name. Otherwise PowerShell already thinks you have the current module loaded, because they are the same version number. – BenH May 29 '18 at 15:16

0 Answers0