0

I'm simply wanting to create a script that can connect to my remote IIS server and update the physical location of a website I've got deployed.

In my script, I've been trying to just connect to my server without doing anything else. So far I have the following:

function UpdateIIS { 
  Import-Module -Name 'IISAdministration'
  Get-IISSite   
}

When I run this, I'm getting the following error:

Import-Module : The specified module 'IISAdministration' was not loaded because no valid module file was found in any module directory.

I've tried a variation on Import-Module where I remove the -Name flag, but I still get the same error.

What's the simplest way for me to connect to my server and list my IIS sites?

halfer
  • 19,824
  • 17
  • 99
  • 186
N0xus
  • 2,674
  • 12
  • 65
  • 126
  • Powershell uses the environmental variable PSMODULEPATH. If a cmdlet is not found it is due to the PSMODULEPATH. Usually the error says cmdlet not found. Import-Module is usually used with executables. Probably in your case you are using an older version of Powershell and newer cmdlets. Or the version of Net is wrong. Powershell is written in c# which uses Net Library. Any PS Import-Module has to able to work with the Net Library that was used to build the c# app. You may need to publich the c# application and install with the setup.exe create with publish. And install runtime Core. – jdweng Mar 28 '23 at 11:12

1 Answers1

0

Import-Module : The specified module 'IISAdministration' was not loaded because no valid module file was found in any module directory.

For this error, you need to make sure the custom module is installed on your system firstly. Please open a PowerShell console and type: Get-module -ListAvailable.

enter image description here

This will get all installed modules in the system and their installation paths. In my PC, the IISAdministration module is installed in "C:\Windows\system32\WindowsPowerShell\v1.0\Modules".

Then you need to verify that the path to your custom module has been added to the "PSModulePath" variable. Right click on "This PC" >> Properties >> Advanced System Settings >> Environment Variables >> Select "PSModulePath" and click the "Edit" button

enter image description here

Append your PowerShell module path to this variable. In my PC it is "C:\Windows\system32\WindowsPowerShell\v1.0\Modules".

enter image description here

Finally restart the PowerShell console and try again.


Add: You can try running the following command to establish a remote PowerShell session: Enter-PSSession.

Then enter the name or IP address of the remote IIS server, ComputerName: <ServerName>.

Import the WebAdministration module: Import-Module WebAdministration.

Run the following command to change the site path (replace sitename and physical path):

Set-ItemProperty IIS:\Sites\sitename -name physicalPath -value "D:\test"

You can refer to this thread: Changing the physical path of an IIS website on a remote machine via Powershell, and this article about how to run remote commands using powershell.

YurongDai
  • 1,362
  • 1
  • 2
  • 7