C# 远程PowerShell连接脚本适用于exchange PS,但不适用于windows PS

C# 远程PowerShell连接脚本适用于exchange PS,但不适用于windows PS,c#,vb.net,powershell,remote-access,runspace,C#,Vb.net,Powershell,Remote Access,Runspace,我编写了一个与ExchangeShell远程交互的函数。现在我想将它用于普通的Windows PS。因此我将WSManConnectionInfo中的Shell Uri更改为http://schemas.microsoft.com/powershell/Microsoft.PowerShell 该函数现在如下所示: Public Function remotePowerShell(ByVal script as String) Dim newCred As PSCredential =

我编写了一个与ExchangeShell远程交互的函数。现在我想将它用于普通的Windows PS。因此我将
WSManConnectionInfo
中的Shell Uri更改为
http://schemas.microsoft.com/powershell/Microsoft.PowerShell

该函数现在如下所示:

Public Function remotePowerShell(ByVal script as String)

    Dim newCred As PSCredential = DirectCast(Nothing, PSCredential)
    Dim connectionInfo As New WSManConnectionInfo(New Uri("http://SVR2012R2-File/powershell?serializationLevel=Full"), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", newCred)
    Dim runspace As Runspace = RunspaceFactory.CreateRunspace(connectionInfo)
    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default
    Dim powershell As PowerShell = powershell.Create()

    Dim command As New PSCommand()

    command.AddScript(script)
    powershell.Commands = command

    Try

        runspace.Open()
        powershell.Runspace = runspace
        Dim psresult As New System.Collections.ObjectModel.Collection(Of PSObject)
        psresult = powershell.Invoke()
        For Each i In psresult
            MsgBox(vbNewLine & i.ToString)
        Next i

    Catch ex As Exception

        MsgBox(Err.Number & vbNewLine & ex.Message)

    Finally

        runspace.Dispose()
        runspace = Nothing

        powershell.Dispose()
        powershell = Nothing

    End Try

End Function
如果现在运行此函数,则会出现以下错误:

WS-Management服务无法处理该请求。资源URI ()没有 在WS-Management目录中找到。目录包含元数据 描述资源或逻辑终结点的

该函数现在看起来与其他同类函数非常相似,比如CodeProject和MSDN。ShellUri也是正确的

然后我尝试了另一种形式的
WSManConnectionInfo

Dim connectionInfo As New WSManConnectionInfo & _
(False, "SVR2012R2-File", 5985, "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", newCred)
可悲的是,这也不起作用

我关闭了两个防火墙(从客户端和Memberserver),并重新启动了我能想到的所有相关服务(RPC、IIS、WS-Management) 无论如何,这应该不是问题,因为脚本用于与Exchange PS交互

我是否需要定义更多内容,或者是否需要其他
PSCredentials


提前感谢大家分享的想法

Http不起作用,因为该网站是由exchange Server(2010+)上的exchange安装例程安装的。@Martin那么我应该使用哪个其他Shell Uri?根据类似()的多篇文章,这是正确的一篇。