Getting "Can't find the drive. The drive called 'IIS' does not exist."
Asked Answered
P

6

61

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 ?

Poachy answered 24/6, 2014 at 17:21 Comment(1)
Is this still an issue? If not mark an answer. Xie XieGiesecke
H
102

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
Hartfield answered 24/6, 2014 at 17:54 Comment(11)
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.Poachy
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}Poachy
@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.Hartfield
:TKS for Ur Kindly help.Poachy
Additionally, make sure you are running your script with administrator privileges. That was my mistake...Heraclid
import-module WebAdministration fixed my problem.Unthinking
Is there a way to perform this with the "add roles and features" wizard with IIS? Or is it something seperateCircularize
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!Lentigo
I think there is no need to install Web-Scripting-Tools.Nomarchy
Running as Administrator solved my problem.Cubby
This works on PowerShell 5. For Powershell 6+, the WebAdministration module is not supported. Reference -> github.com/PowerShell/WindowsCompatibility/issues/79Lest
C
55

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

Crier answered 22/5, 2016 at 17:26 Comment(2)
The same goes for PowerShell ISE if you are developping the script.Obscurantism
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.Rogovy
P
11

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.

Polysemy answered 12/11, 2019 at 18:57 Comment(3)
I am on Windows 10 pro, and Import-Module WebAdministration worked.Salesin
For those on windows server 2016 and 2019Polysemy
This did not work for me on Win 10 Pro. There's some other step or feature missing.Feeble
H
4

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.

Handbarrow answered 17/11, 2020 at 11:50 Comment(1)
That solved my day, as with your reply I found that the powershell script behaves differently if running as administratorAlveolate
A
3

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

Anastrophe answered 9/1, 2017 at 22:35 Comment(0)
T
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.

Trapezius answered 27/2, 2023 at 20:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.