8

Im using PowerShell 4 on Windows Server 2012 R2.

A specific module, WebAdministration, does not get auto loaded when calling a Cmdlet that comes from this module. All other modules I have tried auto load successfully. I can load this module manually using Import-Module and it behaves as expected.

  • The PSModulePath environment variable contains the path with the module. Other modules from this path auto load.
  • The module is not custom. It is a built in IIS feature. The feature is enabled.
  • AutoLoading is enabled. $PSModuleAutoLoadingPreference is set to "All"
  • Get-Command "Get-WebBinding" doesn't work, but Get-Command | where {$_.Name -eq "Get-WebBinding"} does.
  • Get-Module -ListAvailable | where { $_.Name -eq "WebAdministration"} returns the module with the correct path.

PSModulePath = %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\ WebAdministration Module Path = C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration



Output from simple test


PS C:\Users\Administrator> $PSModuleAutoLoadingPreference = "All"

PS C:\Users\Administrator> Get-WebBinding Get-WebBinding : The term 'Get-WebBinding' is not recognized as the name of a cmdlet, function, script file, 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 + Get-WebBinding + ~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Get-WebBinding:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\Administrator> Import-Module WebAdministration

PS C:\Users\Administrator> Get-WebBinding

protocol bindingInformation sslFlags -------- ------------------ -------- http *:8082: 0 http *:8081: 0




Any suggestions on why the auto loading isn't working would be greatly appreciated. Thanks!

kareed44
  • 81
  • 1
  • 3
  • Have you tried adding it to your PowerShell profile so the import persists outside of your current session? – dfundako Dec 22 '15 at 16:58
  • 1
    I could resort to that workaround but I'm more interested in the root cause. The auto load functionality should work with this module but it doesn't in my environment. – kareed44 Dec 22 '15 at 17:17
  • Don't know. Hard to diagnose without your specific paths, variables, or environment info. You have a solution to solve your problem with one line of code in your original post. – dfundako Dec 22 '15 at 17:35
  • I added some detail above. I need to fix the root issue, I cant use a work around. – kareed44 Dec 22 '15 at 17:59
  • 1
    @kareed44 while I agree that this is annoying and it makes sense to find a root cause, I don't think you should be relying on module auto-loading as anything but a convenience. – briantist Dec 22 '15 at 22:02
  • 1
    Try to remove everything from `%LocalAppData%\Microsoft\Windows\PowerShell\CommandAnalysis` and start new PowerShell session. – user4003407 Dec 22 '15 at 22:16
  • Clearing the CommandAnalysis did not fix the issue. – kareed44 Dec 23 '15 at 18:01
  • Windows PowerShell 4.0 is a legacy product, why don't you upgrade to Windows PowerShell 5.1 or install PowerShell 7.1.4 side-by-side (the .zip distribution is stand alone and doesn't require an update of .Net for limited use)? – Dennis Sep 16 '21 at 18:39
  • Does this answer your question? [Powershell 7 import-module does not persist across sessions](https://stackoverflow.com/questions/71942245/powershell-7-import-module-does-not-persist-across-sessions) – codewario Apr 20 '22 at 17:55
  • Do you find the module with `Get-Module WebAdministration -ListAvailable`? Does `Get-Module WebAdministration -ListAvailable -Refresh` make any difference to the issue? If you import the module and do `(Get-Module WebAdministration).ModuleBase`, is it in a versioned folder, and if so, if you copy the module contents to the folder above, does it fix the issue? (So you have `Modules\WebAdministration\WebAdministration.psd1`, etc) – FSCKur Feb 01 '23 at 13:43
  • I can't reproduce this on Windows Server 2016 with PowerShell 5.1, the `WebAdministration` module autoloads for me when referencing a cmdlet inside of it for the first time. I'm not sure if PS 4.0 requires `FunctionsToExport` to be defined in the module's `psd1` file for autoloading to work with compiled module assemblies, but that is noticeably absent for `WebAdministration` when checking today. I do not have a server with PS 4.0 to confirm the behavior on that version of PS. – codewario Feb 03 '23 at 15:22
  • The only real clue I can find that this may not work is that [modules using providers may not autoload](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules?view=powershell-7.3#module-auto-loading). `WebAdministration` provides the `IIS:` PSDrive. However, as indicated in my previous comment, I am able to autoload the `WebAdministration` module on WS 2016 with PS 5.1 installed, which goes against this statement. My hypothesis is this limitation might not be relevant in PS 5.1+, but I can't say for certain since I don't have a PS 4.0 env to confirm. – codewario Feb 03 '23 at 15:35

3 Answers3

0

Try reinstalling the module to see if that makes a difference.

If that doesn't work, while it's annoying that the autoload isn't functioning, you can import the module before use and expect it to work.

Import-Module WebAdministration
Get-WebBinding

Or if you need a one-liner:

Import-Module WebAdministration; Get-WebBinding

The only real clue I can find on why this may not work is that modules using providers may not autoload. WebAdministration provides the IIS: PSDrive. However, as I've indicated in a previous comment, I am able to autoload the WebAdministration module on WS 2016 with PS 5.1 installed, which goes against this statement. My hypothesis is this limitation might not be relevant in PS 5.1+, but I can't say for certain since I don't have a PS 4.0 env to test with.

codewario
  • 19,553
  • 20
  • 90
  • 159
0

Here's how I chose to load modules when I start up ISE each time. This gives me the option to load certain modules. I know this isn't what you asked for, but this does automatically load modules, and be sure to note how these modules are called.

Create the following file:

Path: C:\Users\<username>\Documents\WindowsPowershell
File: Microsoft.PowerShellISE_profileX.PS1

In the file, I use this code, but modify as needed:

$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Connect to Office 365?",0,"Office 365",4)
if ($intAnswer -eq 6){
    #YES - Go to Cloud     
    #$a.popup("You answered yes.")
    Set-Location H:\sandbox
    #.  .\Start3.ps1
    . .\Auto-Connector.ps1
    . .\Refresh-PSSession.ps1
    . .\ScriptLoaders.ps1
    . .\ESDSCRIPTS3.ps1

}else{
    Set-Location H:\sandbox
    Import-Module ActiveDirectory
}
Lord Helmet
  • 150
  • 14
0

I would consider using the #Requires statement at the top of the script file after you have imported it for the profile that the script will be running under. The script will likely not run unless it can find the module that the script requires. You then do not need to use the 'import-module' cmdlet as its already handled for you. You can read more about the requires statements here. for example:

#Requires -Modules WebAdministration
Seb
  • 179
  • 2
  • 6