2013-02-13 35 views
3
如下图所示

简单的脚本导致我的电脑上的异常:的PowerShell:命令 “GET-ChildItem IIS:网站” 将导致错误

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 

但是,如果我加写在脚本的开始-Host正常工作:

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

不幸的是,我不知道什么可能导致此问题。有人能帮助我吗?

+0

幸运的是在PowerShell 2.0中工作正常:)它可以在3.0中的错误 – RinoTom 2013-02-14 18:59:23

回答

1
Import-Module WebAdministration 
# adding sleep here resolves the issue for me 
sleep 2 
Get-ChildItem IIS:\Sites 
1

我知道这是一个老问题,但今天我面临同样的问题。找到了以下形式的解决方案,它为我工作。

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" 
}