【问题标题】:PowerShell: command "Get-ChildItem IIS:\Sites" causes an errorPowerShell:命令“Get-ChildItem IIS:\Sites”导致错误
【发布时间】:2013-02-13 20:50:00
【问题描述】:

下面显示的简单脚本导致我的计算机出现异常:

Import-Module WebAdministration
Get-ChildItem IIS:\Sites

结果:

powershell -NonInteractive .\test.ps1
Get-ChildItem : Could not load file or assembly 'Microsoft.PowerShell.Commands.Management' or one of its dependencies. The system cannot
find the file specified.
At C:\...\test.ps1:3 char:1
+ Get-ChildItem IIS:\Sites
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.GetChildItemCommand

但是,如果我在脚本开头添加 Write-Host,它就可以正常工作:

Write-Host '!!!'
Import-Module WebAdministration
Get-ChildItem IIS:\Sites

很遗憾,我不知道是什么原因导致了这个问题。有人可以帮我吗?

【问题讨论】:

  • 幸运的是它在 PowerShell 2.0 中运行良好 :) 它可能是 3.0 中的一个错误

标签: powershell powershell-3.0 web-administration


【解决方案1】:
Import-Module WebAdministration 
# adding sleep here resolves the issue for me
sleep 2
Get-ChildItem IIS:\Sites

【讨论】:

    【解决方案2】:

    我知道这是一个老问题,但今天我遇到了同样的问题。找到了以下形式的解决方案,它对我有用。

    Dmitry said

    try 
    { 
        $serviceSite = Get-Item "IIS:\sites\$siteName" -ErrorAction "SilentlyContinue" 
    } 
    catch [System.IO.FileNotFoundException] 
    { 
    # Try again (workaround for System.IO.FileNotFoundException issue) 
        $serviceSite = Get-Item "IIS:\sites\$siteName" -ErrorAction "SilentlyContinue" 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多