Import-Module WebAdministration wont load from script but does from command line
Asked Answered
C

4

26

I'm coming onto a project that uses PowerShell to script the build. The build makes use of the WebAdministration module to manage the local IIS instance. When I run the build script the following error is thrown when trying to import WebAdministration.

Error: 06/29/2016 17:28:35: At C:\dev\src\nib-ravendb\build\ConfigureIis.ps1:10 char:1 + Import-Module WebAdministration + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~ [<<==>>] Exception: The specified module 'WebAdministration' was not loaded because no valid module file was fo und in any module directory. ERROR: 1

How ever when I run Import-Module WebAdministration at the PowerShell command line the module is imported and I can use features from it. Subsequently running the build script still fails.

I have IIS 7.5 and PowerShell 4

Does anyone have an idea why this import would be failing in the script but not at the command line, and how to fix it?

Cawley answered 29/6, 2016 at 7:39 Comment(0)
C
4

In the end there was a problem something, possibly chocolatey?, was truncating $env:PSModulePath to the first entry, this is why the script was working if I typed it in but not in the script.

I found it by logging $env:PSModulePath at different points in the scripts that I was running.

I worked around it by reordering the entries in $env:PSModulePath.

Have a look at @Richard's answer for some other good suggestions.

Cawley answered 30/6, 2016 at 22:53 Comment(0)
B
26

For servers you need to install the role Management Tools under Web Server (IIS) to use the WebAdministration module. To see if you have the module available use Get-Module -ListAvailable.

For Windows 7 to 10 you will need to install the feature IIS Management Scripts and tools under Internet Information Services >> Web Management Tools.

You could try manually locating the WebAdministration .psd1 file and then import it. Use $env:psmodulepath to help locate where your modules are stored then run:

Import-Module -Name 'C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration\WebAdministration.psd1' 

If Server 2008 you could try the following but this may not work on 2012 and upwards:

Add-PSSnapin WebAdministration

Note You will need to run the script with administrator rights to be able to load the WebAdministration module with Import-Module or Add-PSSnapin.

Also check that you have PowerShell's execution Policy set to Unrestricted:

Set-ExecutionPolicy unrestricted

You might want to see this Question.

Bearable answered 29/6, 2016 at 7:47 Comment(3)
Thanks for your response @Richard. Unfortunately I have the Web Management Tools installed and WebAdministration come up when I list available modules. I can even use it from the shell. Just when I run the script it can't be imported. Any idea on that?Cawley
@Cawley I have updated my question with some things you could try.Bearable
thanks. The first answer would have likely worked. The other two I had already tried. In the end there was a problem, possibly with chocolatey?, that was truncating $env:PSModulePath to the first entry, this is why the script was working if I typed it in but not in the script. I worked around it by reordering the entries in $env:PSModulePath Thanks heaps for you help.Cawley
A
9

I had the same situation, i've fixed it installing the Windows Feature Web-Scripting-Tools on W2016 Server:

Add-WindowsFeature Web-Scripting-Tools
Achaemenid answered 10/9, 2021 at 19:29 Comment(1)
This should be the accepted answerManhood
C
4

In the end there was a problem something, possibly chocolatey?, was truncating $env:PSModulePath to the first entry, this is why the script was working if I typed it in but not in the script.

I found it by logging $env:PSModulePath at different points in the scripts that I was running.

I worked around it by reordering the entries in $env:PSModulePath.

Have a look at @Richard's answer for some other good suggestions.

Cawley answered 30/6, 2016 at 22:53 Comment(0)
C
2

In my case (Windows 10) I was using Powershell 7 and this simply refused to install the WebAdministration module, despite it being present in Windows Features.

Using a previous version of PS: e.g. Developer PowerShell for VS worked.

Cadman answered 25/5, 2022 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.