获取"无法找到驱动器。该驱动器称为“IIS'不存在。"

时间:2014-06-24 17:21:49

标签: powershell iis namespaces

我编写了一个PowerShell脚本来自动部署IIS网站,但是当我将参数传递给脚本时,我收到以下错误:

  

找不到驱动器。名为“IIS”的驱动器不存在。

我的脚本(iss_website_version_update.ps1)如下所示,但请注意它尚未完成:

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]))
        }
    .......

请告诉我为什么命令set-location IIS:\;中的子命令Invoke-Command无法识别?

5 个答案:

答案 0 :(得分:66)

驾车,而非驾驶 r 。该驱动器由WebAdministration模块提供,因此您需要先安装/导入该模块。

如何安装模块取决于您的实际系统以及您是使用GUI还是PowerShell。例如,在Windows Server 2008 R2上,您可以使用以下PowerShell命令安装模块:

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

安装模块后,您可以在脚本中加载它:

Import-Module WebAdministration

答案 1 :(得分:41)

解决在管理模式下运行脚本(或powershell shell / exe)的问题。

答案 2 :(得分:2)

在Windows Server 2008 32位上,我必须明确下载并安装" IIS Powershell Snap-in(x86)"来自微软的网站。

答案 3 :(得分:0)

对于Windows Server 2016和2019上的服务器,我发现它是服务器功能

IIS管理控制台,又名Web-Mgmt-Console

需要打开。如果您处于活动的Powershell会话中,并且已导入WebAdministration模块和命令

Get-PSDrive 

在列表中不返回IIS。运行以下

Remove-Module WebAdministration
Import-Module WebAdministration
Get-PSDrive

然后,您应该看到Powershell IIS驱动器。我怀疑我的问题与运行导入WebAdministration但在Powershell会话中附加PSDrive IIS的脚本有关。除非我先将其删除,否则尝试再次导入WebAdministration不会将PSDrive附加到我的会话上。

答案 4 :(得分:0)

当我尝试在PowerShell ISE中运行脚本时遇到了同样的问题。

当我以管理员身份重新打开PowerShell ISE时,此问题已解决。