4

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

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

不幸的是,我不知道是什么导致了这个问题。有人可以帮我吗?

4

2 回答 2

1
Import-Module WebAdministration 
# adding sleep here resolves the issue for me
sleep 2
Get-ChildItem IIS:\Sites
于 2014-02-05T11:00:34.613 回答
1

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

德米特里 说

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" 
}
于 2017-04-20T11:04:27.913 回答