kareed44
kareed44

Reputation: 91

Specific PowerShell Module Not Autoloading

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.

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!

Upvotes: 9

Views: 4170

Answers (4)

mohamed saeed
mohamed saeed

Reputation: 303

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

Upvotes: 0

codewario
codewario

Reputation: 21468

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.

Upvotes: 0

Seb
Seb

Reputation: 309

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

Upvotes: 0

Lord Helmet
Lord Helmet

Reputation: 190

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
}

Upvotes: 0

Related Questions