PowerShell IIS:\ WebAdmin远程调用触发WSAStartup错误,WSANOTINITIALISED

时间:2012-02-24 16:00:14

标签: powershell wsastartup

我正在使用PSRemoting与WebAdministration模块获取有关各种网站的信息,并且它正在运行。但是,在调用命令期间,我收到了一个恼人的非致命COM异常,并想知道是否有其他人已经解决了它。这是一个最小的实现:

cls
$command = {
    param($alias)
    Import-Module 'WebAdministration'
    $binding = Get-WebBinding -HostHeader $alias
    $binding
}

$server = 'server'
$args = @('alias')
$session = New-PSSession -ComputerName $server
Write-Host ("Invoking")
try {
    Invoke-Command -Session $session -ScriptBlock $command -ArgumentList $args
    Write-Host ("Invoked")
} catch {
    Write-Host ("Caught $_")
} finally {
    Write-Host ("Removing")
    Remove-PSSession -Session $session
    Write-Host ("Removed")
}

以下是结果:

Invoking

protocol           : http
bindingInformation : 10.x.x.x:80:alias
...
Schema             : Microsoft.IIs.PowerShell.Framework.ConfigurationElementSchema

An unhandled COM interop exception occurred: Either the application has not called WSAStartup, or WSAStartup failed. (Exception from HRESULT: 0x800
7276D)
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : COMException

Invoked
Removing
Removed

我观察到在抛出错误之前返回了结果。

有趣的细节:
  - Get-Website,Get-Item“IIS:\ ...”,Get-WebBinding都会导致同样的错误   - 直接在目标机器上运行$命令,结果没有错误   - Get-Item“d:\ ...”不会导致任何错误   - COM错误不

2 个答案:

答案 0 :(得分:2)

我能够使用以下方法解决此问题:

    $iisIpAddresses = Invoke-Command -Session $session -scriptblock {
    if (!(Get-Module WebAdministration)) 
    {
        Import-Module WebAdministration
    }
    $iisBindings = Get-WebBinding
    [String[]]$iisBindings = $iisBindings | Select bindingInformation
    $iisBindings
}

Remove-PSSession $session

答案 1 :(得分:0)

这是埋藏在PowerShell .NET和winsock实现的深处。它低于我可以校准的任何东西,所以我在我的远程调用中添加了“-ErrorAction SilentlyContinue”。它没有修复任何东西,但一切正常。我猜这个答案已经足够了。