Specific PowerShell Module Not Autoloading
Asked Answered
H

4

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!

Handler answered 22/12, 2015 at 16:49 Comment(13)
Have you tried adding it to your PowerShell profile so the import persists outside of your current session?Clupeid
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.Handler
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.Clupeid
I added some detail above. I need to fix the root issue, I cant use a work around.Handler
@Handler 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.Arachne
Try to remove everything from %LocalAppData%\Microsoft\Windows\PowerShell\CommandAnalysis and start new PowerShell session.Lama
Clearing the CommandAnalysis did not fix the issue.Handler
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)?Beebe
Does this answer your question? Powershell 7 import-module does not persist across sessionsLeavenworth
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)Clarettaclarette
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.Leavenworth
The only real clue I can find that this may not work is that modules using providers may not autoload. 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.Leavenworth
Why don't you install PowerShell 5.1?Beebe
L
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.

Leavenworth answered 12/6, 2019 at 16:51 Comment(0)
G
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
}
Gelation answered 19/8, 2021 at 17:32 Comment(0)
K
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
Kingery answered 28/10, 2022 at 12:46 Comment(1)
If the module doesn't autoload, this will not make it autoload.Clarettaclarette
A
0

Use appcmd it has all that you need to manage IIS and more and doesn't need to install any module, reference here

Powershell example to add app pool:

$appPoolName = "apppoolname"
$appcmd = "C:\Windows\System32\inetsrv\appcmd.exe"
 & $appcmd add apppool  /name:$appPoolName
Amandine answered 23/10, 2023 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.