0

我编写了一个函数来与 Exchange Shell 进行远程交互。现在我想将它用于普通的 Windows PS。所以我将 Shell Uri 更改WSManConnectionInfohttp://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 服务无法处理该请求。在 WS-Management 目录中找不到资源 URI ( http://schemas.microsoft.com/powershell/Microsoft.PowerShell )。目录包含描述资源或逻辑端点的元数据。

该函数现在看起来与 SO、CodeProject 和 MSDN 上的其他同类函数几乎相同。ShellUri 也是正确的。

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

Dim connectionInfo As New WSManConnectionInfo & _
(False, "SVR2012R2-File", 5985, "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", newCred)

可悲的是,这也不起作用。

我关闭了两个防火墙(来自客户端和成员服务器)并重新启动了我能想到的所有相关服务(RPC、IIS、WS-Management)这应该不是问题,因为脚本可以与 Exchange PS 交互。

是否有可能我需要定义更多的东西或者我需要其他的PSCredentials

提前感谢您的任何共同想法。

4

0 回答 0