【问题标题】:Getting "Can't find the drive. The drive called 'IIS' does not exist."得到“找不到驱动器。名为‘IIS’的驱动器不存在。”
【发布时间】:2014-08-15 01:45:57
【问题描述】:

我编写了一个 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]))
        }
    .......

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

【问题讨论】:

  • 这仍然是个问题吗?如果没有标记答案。谢邪

标签: powershell iis namespaces


【解决方案1】:

驱动由WebAdministration 模块提供,因此您需要先安装/导入该模块。

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

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

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

Import-Module WebAdministration

【讨论】:

  • 我已经安装了 IIS-snapin。该命令包括三个子命令:"Import-Module WebAdministration;set-location IIS:\;(Stop-Website $($websiteName[$j] ))”用分号分割。现在的问题是“set-location IIS:\”无法识别。
  • 这意味着如果 U 键在以下命令中,U 将得到相同的错误: 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}
  • @user3772170:您正在远程主机上运行命令,因此该模块必须安装在该主机上。此外,您的代码已经在 PowerShell 中运行,因此脚本块中不需要 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command。删除它。
  • 此外,请确保您以管理员权限运行脚本。那是我的错……
  • import-module WebAdministration 解决了我的问题。
【解决方案2】:

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

【讨论】:

  • 如果您正在开发脚本,PowerShell ISE 也是如此。
【解决方案3】:

对于那些在 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 附加到我的会话中。

【讨论】:

  • 我使用的是 Windows 10 专业版,并且 Import-Module WebAdministration 工作。
  • 对于那些在 windows server 2016 和 2019 上的用户
  • 这在 Win 10 Pro 上对我不起作用。缺少其他一些步骤或功能。
【解决方案4】:

在 Windows Server 2008 32 位上,我必须从 Microsoft 网站明确下载并安装“IIS Powershell 管理单元 (x86)”。

【讨论】:

    【解决方案5】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      相关资源
      最近更新 更多