58

I wrote a PowerShell script to deploy IIS Website automatically, but when I pass parameters to the script I get the following error:

Cannot find the drive. The drive called 'IIS' does not exist.

My script (iss_website_version_update.ps1) is as below, but note that it is not finished yet:

param(
[array]$iishostlist=$(throw "Parameter missing: -name iishostlist"),
[array]$websiteName=$(throw "Parameter missing: -name websiteName")
)

For($i=0;$i -lt $iishostlist.Count; $i++){
For($j=0;$j -lt  $websiteName.Count; $j++){
    $start = get-date
    $tempSession = new-pssession  -ComputerName  $($iishostlist[$i])
    Invoke-Command -Session $tempSession -ScriptBlock {
        C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -command Import-Module WebAdministration;set-location IIS:\;(Stop-Website $($websiteName[$j]))
        }
    .......

Please let me know why the sub-command set-location IIS:\; in the command Invoke-Command is not be recognized ?

Jeroen
  • 60,696
  • 40
  • 206
  • 339
Jianxiang Xia
  • 715
  • 1
  • 6
  • 11

6 Answers6

99

The drive is provided by the WebAdministration module, so you need to install/import that module first.

How you install the module depends on your actual system and whether you use GUI or PowerShell. On a Windows Server 2008 R2 for instance you'd install the module with the following PowerShell commands:

Import-Module ServerManager
Add-WindowsFeature Web-Scripting-Tools

After the module is installed you can load it in your script like this:

Import-Module WebAdministration
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • I had installed the IIS-snapin.And the command includes three sub-commands:"Import-Module WebAdministration;set-location IIS:\;(Stop-Website $($websiteName[$j]))" that was splitted with semicolon.Now the question is "set-location IIS:\" can not be recognized. – Jianxiang Xia Jun 25 '14 at 00:28
  • That means if U key in the following command,U will get the same error: Invoke-Command -ComputerName xx.xx.xx.xx -ScriptBlock {C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command Import-Module WebAdministration;set-location IIS:\;Stop-Website testsite} – Jianxiang Xia Jun 25 '14 at 03:10
  • @user3772170: You're running the commands on a remote host, so the module must be installed on that host. Also, your code is already running in PowerShell, so there's no need for `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command` in the scriptblock. Remove that. – Ansgar Wiechers Jun 25 '14 at 07:27
  • :TKS for Ur Kindly help. – Jianxiang Xia Jun 27 '14 at 06:54
  • 4
    Additionally, make sure you are running your script with administrator privileges. That was my mistake... – Arnaud Nov 24 '15 at 00:09
  • 1
    import-module WebAdministration fixed my problem. – Teoman shipahi Mar 07 '16 at 17:37
  • Is there a way to perform this with the "add roles and features" wizard with IIS? Or is it something seperate – lvthillo Mar 15 '17 at 07:07
  • I was already running PS version 5.1 on Win Server 2012, with web-administration module imported and it worked as expected, until a new script that required IIS:\ drive failed. I had to remove the module then follow your steps to resolve the issue. No restart required. Great! – Desktop Jul 16 '19 at 09:06
  • I think there is no need to install `Web-Scripting-Tools`. – Will Huang Jul 12 '20 at 15:09
  • Running as Administrator solved my problem. – Jesse Chisholm Mar 11 '21 at 02:45
  • 1
    This works on PowerShell 5. For Powershell 6+, the WebAdministration module is not supported. Reference -> https://github.com/PowerShell/WindowsCompatibility/issues/79 – Hem Bhagat Jul 27 '22 at 11:33
54

To resolve running the script (or powershell shell/exe) in Admin mode.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
azuneca
  • 1,053
  • 13
  • 15
  • 4
    The same goes for PowerShell ISE if you are developping the script. – PhilDulac Feb 15 '17 at 15:52
  • It seems it's also important to make sure the PowerShell version is what you expect in command-line vs PowerShell ISE usage, since PowerShell 7 may not work. I was extremely confused until I realized that the default version for "pwsh" on command-line is version 7 even though I was testing in ISE that was version 5. – mk12 Jul 10 '23 at 12:39
10

For those on windows server 2016 and 2019 I found that it was the Server Feature

IIS Management Console aka Web-Mgmt-Console

that needed to be turned on. If you are in an active powershell session and have import the WebAdministration module and the command

Get-PSDrive 

does not return IIS in the list. Run the following

Remove-Module WebAdministration
Import-Module WebAdministration
Get-PSDrive

Then you should see the powershell IIS drive. I suspect my issue had to do with running scripts that imported WebAdministration but were attaching the PSDrive IIS in my powershell session. Attempts to import WebAdministration again would not attaching the PSDrive to my session unless I removed it first.

McFrank
  • 331
  • 3
  • 11
3

On Windows Server 2008 32-bit, I had to explicitly download and install "IIS Powershell Snap-in (x86)" from Microsoft's website.

mhenry1384
  • 7,538
  • 5
  • 55
  • 74
3

I was facing the same problem while trying to run a script in PowerShell ISE.

The problem was fixed when I reopened PowerShell ISE as Administrator.

Jaideep Dhumal
  • 903
  • 6
  • 6
  • 1
    That solved my day, as with your reply I found that the powershell script behaves differently if running as administrator – Zac Jul 15 '22 at 14:32
0

I encountered this issue today when trying to run a script in PowerShell 7; however, it worked fine in PowerShell 5 (the "default" edition of PowerShell still). So if you're seeing this issue, it may be worth a shot at just using an older P$ version.

Tom Warner
  • 3,193
  • 3
  • 17
  • 24