在vb.net中管理Office 365的许可证

时间:2016-04-22 05:33:52

标签: asp.net vb.net web-services powershell office365

我正在尝试在vb.net webservice项目中运行Office365 cmdlet。代码为:

Public Function ExcutePowershellCommands() As ObjectModel.Collection(Of PSObject)
    Try            
        Dim userList As ObjectModel.Collection(Of PSObject) = Nothing

        Dim initialSession As InitialSessionState = InitialSessionState.CreateDefault()
        initialSession.ImportPSModule(New String() {"MSOnline"})

        Dim O365Password_secureString As New System.Security.SecureString()
        Dim O365Password As String = "password"
        For Each x As Char In O365Password
            O365Password_secureString.AppendChar(x)
        Next
        Dim credential As New PSCredential("username", O365Password_secureString)

        Dim connectCommand As New Command("Connect-MsolService")
        connectCommand.Parameters.Add((New CommandParameter("Credential", credential)))

        Dim getCommand As New Command("Get-MsolUser")

        Using psRunSpace As Runspace = RunspaceFactory.CreateRunspace(initialSession)

            psRunSpace.Open()


            For Each com As Command In New Command() {connectCommand, getCommand}
                Dim pipe As Pipeline = psRunSpace.CreatePipeline()
                pipe.Commands.Add(com)

                Dim results As ObjectModel.Collection(Of PSObject) = pipe.Invoke()
                Dim [error] As ObjectModel.Collection(Of Object) = pipe.[Error].ReadToEnd()
                If [error].Count > 0 AndAlso com.Equals(connectCommand) Then
                    Throw New ApplicationException("Problem in login! " + [error](0).ToString())
                    Return Nothing
                End If
                If [error].Count > 0 AndAlso com.Equals(getCommand) Then
                    Throw New ApplicationException("Problem in getting data! " + [error](0).ToString())
                    Return Nothing
                Else
                    userList = results
                End If
            Next

            psRunSpace.Close()
        End Using
        Return userList
    Catch generatedExceptionName As Exception
        Throw
    End Try
End Function

运行Connect-MsolService时,收到以下错误消息:

  

https://provisioningapi.microsoftonline.com/provisioningwebservice.svc没有可以接受该消息的端点。这通常是由错误的地址或SOAP操作引起的。有关更多详细信息,请参阅InnerException(如果存在)。

我可以在Windows应用程序中成功运行此功能。我认为IIS中可能有些问题。任何想法?

0 个答案:

没有答案