Quantcast
Channel: Exchange Server Development forum
Viewing all articles
Browse latest Browse all 7132

How to: Get a list of mail users by using the Exchange Management Shell

$
0
0

I'm really sorry, I'm very new VB and I'm trying to learn as I go along. I am trying to run a program that will allow me to get a list of mail users by using the Exchange Management Shell via managed code. I am running the program and I am not getting any errors but I'm not getting any results either. Can someone please help me out. Is there something I am doing wrong??

My Code:

PublicClassfrmEmailValidation

   Dim username AsString = "server\username"

   Dim password = "password"

   Dim Count AsInteger

   Function GetUsersUsingBasicAuth( _

   ByVal LiveIDConnectionUri AsString, ByVal ScehmaUriAsString, _

   ByVal Credentials AsPSCredential, ByVal Count AsInteger)AsCollection(OfPSObject)

       Dim ConnectionInfo AsWSManConnectionInfo = _

           NewWSManConnectionInfo(NewUri(LiveIDConnectionUri), ScehmaUri, Credentials)

       ConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic

       ScehmaUri = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"

       LiveIDConnectionUri = "http://exchangeserver/Powershell?serializationLevel=Full"

       Credentials = New PSCredential(username, password)

     

       Dim RemoteRunspace AsRunspace

       RemoteRunspace = RunspaceFactory.CreateRunspace(ConnectionInfo)

       Return GetUserInformation(Count, RemoteRunspace)

       Count = 5

   EndFunction

   Function GetUserInformation(ByVal CountAsInteger, ByVal RemoteRunspace As Runspace) AsCollection(OfPSObject)

       Dim RemotePowerShell AsPowerShell = PowerShell.Create

       RemotePowerShell.AddCommand("Get-mailboxdatabase")

       RemotePowerShell.AddParameter("ResultSize", Count)

       ' Open the remote runspace on the server.

       RemoteRunspace.Open()

       ' Associate the runspace with the Exchange Management Shell.

       RemotePowerShell.Runspace = RemoteRunspace

       ' Invoke the Exchange Management Shell to run the command.

       Return RemotePowerShell.Invoke

   EndFunction

   PublicSub btnReconnect_Click(ByVal senderAs System.Object,ByVal e As System.EventArgs)

       Dim RemotePowerShell AsPowerShell = PowerShell.Create

       Dim commandResults AsCollection(OfPSObject)

       commandResults = RemotePowerShell.Invoke()

       ForEach objAsPSObject In commandResults

           MessageBox.Show(obj.Properties("Display Name").Value.ToString())

       Next

   EndSub


Viewing all articles
Browse latest Browse all 7132

Trending Articles