2013-02-13 57 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" 
}