0

Exchange Shell とリモートで対話する関数を作成しました。今度は通常の Windows PS に使用したいと思います。そこで、シェル 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 サービスは要求を処理できません。リソース 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)

悲しいことに、これもうまくいきませんでした。

両方のファイアウォールを (クライアントとメンバー サーバーから) オフにし、考えられるすべての関連サービス (RPC、IIS、WS-Management) を再起動しました。いずれにせよ、スクリプトは Exchange PS とのやり取りで機能するため、これは問題になりません。

もっと何かを定義する必要がある可能性はありますか、それとも他のものが必要PSCredentialsですか?

共有された考えを前もって感謝します。

4

0 に答える 0