远程PowerShell连接脚本适用于交换PS但不适用于Windows PS

时间:2016-03-10 14:03:09

标签: c# vb.net powershell remote-access runspace

我已经编写了一个与Exchange Shell远程交互的函数。现在我想将它用于普通的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   (http://schemas.microsoft.com/powershell/Microsoft.PowerShell)不是   在WS-Management目录中找到。目录包含元数据   描述资源或逻辑端点。

该功能现在看起来与SO,CodeProject和MSDN上的其他类似功能相同。 ShellUri也是正确的。

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

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

可悲的是,这也没有奏效。

我关闭了两个防火墙(来自Client和Memberserver)并重新启动了我能想到的所有相关服务(RPC,IIS,WS-Management) 这不应该是一个问题,因为脚本适用于与Exchange PS的交互。

我是否可能需要定义更多内容,还是需要其他PSCredentials

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

0 个答案:

没有答案