下面显示的简单脚本会导致我的计算机出现异常:
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
不幸的是,我不知道可能导致这个问题的原因。有人能帮助我吗?
答案 0 :(得分:1)
Import-Module WebAdministration
# adding sleep here resolves the issue for me
sleep 2
Get-ChildItem IIS:\Sites
答案 1 :(得分: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"
}