简体   繁体   中英

PowerShell: command “Get-ChildItem IIS:\Sites” causes an error

Simple script shown below causes an exception on my computer:

Import-Module WebAdministration
Get-ChildItem IIS:\Sites

Results:

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

But if I add Write-Host at the beginning of the script it works fine:

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

Unfortunately, I have no idea what may cause this problem. Could somebody help me?

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

I know it is an old question but today I faced the same issue. Found a solution in the following form and it worked for me.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM