PowerShell:命令"Get-ChildItem IIS:Sites"导致错误



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

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

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

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

我知道这是一个老问题,但今天我遇到了同样的问题。在下面的表格中找到了一个解决方案,它对我有效。

德米特里说

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

相关内容

最新更新