powershellwsastartup

PowerShell IIS:\ WebAdmin Remote Invocation triggers WSAStartup error, WSANOTINITIALISED


I'm using PSRemoting with the WebAdministration module to get info about various sites, and it's working. I am, however, receiving an annoying non-fatal COM exception during invocation of the command, and wondering if anyone else has resolved it. Here's a minimal implementation:

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

And here are the results:

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

I observe the result is returned prior to the error being thrown.

Amusing details:
- Get-Website, Get-Item "IIS:\...", Get-WebBinding all result in the same error
- Running $command directly on the target machine as written results in no error
- Get-Item "d:\..." does not result in any error
- The COM error doesn't


Solution

  • This is buried somewhere deep in the bowels of PowerShell's implementation of .NET and winsock. It's below anything I can calibrate, so I've added " -ErrorAction SilentlyContinue" to my remote invoke. It doesn't fix anything, but everything works correctly. That's answer enough for now, I guess.