PowerShell IIS :\WebAdmin Remote Invocation triggers WSAStartup error, WSAN未初始化

标签 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 都会导致相同的错误
- 直接在目标机器上运行 $command 不会出现错误
- Get-Item "d:\..."不会导致任何错误
- COM 错误不会

最佳答案

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

    $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

关于PowerShell IIS :\WebAdmin Remote Invocation triggers WSAStartup error, WSAN未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9433886/

相关文章:

powershell - 在 Powershell 中,如何传递 IEnumerable 参数?

c# - 从远程服务器读取证书哈希

c++ - 使用 C++ 和 winsock 的蓝牙

azure - 无法使用 PowerShell POST Cosmos DB 存储过程

linker - WSA启动链接错误

windows - 是否可以判断进程中是否调用了 WSAStartup?

powershell - 如何从PowerShell读取PowerShell的调试流?

function - 正确使用参数

c# - x64 上是否没有适用于 PowerShell 的 TFS Snapin?