I am working on an automation solution for our user creation. I am creating a WCF service to create users using PS scripting from within my vb.net service application. I can create users/mailboxes just fine and even update the O365 mailbox (once the sync
has happened), but I can't seem to make the sync run by submitting a PS command from within my code (samples to follow).
I use my credentials and I know that works, because I can run the command from PS using a remote connection from my DEV box to the server and it works fine. Please have a look at the code below.
Any help or advice would be greatly appreciated.
Sincerely,
Glen
Dim rsp As Runspace = GetExchangeSRVRunspace ()
Using pshell = PowerShell.Create()
Dim cmd = New PSCommand
With cmd
'> Create Mailbox
.AddCommand("New-RemoteMailbox")
.AddParameter("Name", NewEmployeeInfo.FullName)
.AddParameter("Alias", NewEmployeeInfo.UserName)
.AddParameter("OnPremisesOrganizationalUnit", NewEmployeeInfo.OrganizationalUnit)
.AddParameter("UserPrincipalName", NewEmployeeInfo.UserPrincipalName)
.AddParameter("SamAccountName", NewEmployeeInfo.SamAccountName)
.AddParameter("FirstName", NewEmployeeInfo.FirstName)
If NewEmployeeInfo.Initials <> String.Empty AndAlso NewEmployeeInfo.Initials.Length < 7 Then
.AddParameter("Initials", NewEmployeeInfo.Initials)
End If
.AddParameter("LastName", NewEmployeeInfo.LastName)
.AddParameter("Password", GetSecureString("Temppassword"))
.AddParameter("ResetPasswordOnNextLogon", False)
'.Add("ManagedFolderMailboxPolicyAllowed")
'.AddParameter("OutVariable", "$OutInfo")
'> Set Custom Attribute 1
.AddCommand("Set-Mailbox")
.AddParameter("Identity", NewEmployeeInfo.UserName)
.AddParameter("CustomAttribute1", NewEmployeeInfo.FullName)
End With
pshell.Commands = cmd
pshell.Runspace = rsp
pshell.Invoke()
Threading.Thread.Sleep(2000)
'> Run Sync with O365 Server
cmd = New PSCommand
With cmd
Dim sblock = System.Management.Automation.ScriptBlock.Create("{&""c:\Program Files\Microsoft Azure AD Sync\Bin\DirectorySyncClientCmd.exe""}")
.AddCommand("Invoke-Command")
.AddParameter("ComputerName", "MMExchangeSRV ")
.AddParameter("ScriptBlock", sblock)
.AddParameter("Credential", New System.Management.Automation.PSCredential("my.username@millermartin.com", GetSecureString("mypassword")))
End With
pshell.Commands = cmd
pshell.Runspace = rsp
pshell.Invoke()
End Using
#Region " GetExchangeSRVRunspace "
Private Function GetEXCHG4Runspace() As Runspace
Dim sess As Object
Dim rsp As Runspace = RunspaceFactory.CreateRunspace()
rsp.Open()
Using pshell = PowerShell.Create()
Dim pCmd As New PSCommand
With pCmd
.AddCommand("New-PSSession")
.AddParameter("ConfigurationName", "Microsoft.Exchange")
.AddParameter("ConnectionUri", New Uri("http://mmexchangesrv/PowerShell/"))
.AddParameter("Authentication", "Kerberos")
.AddParameter("Credential", New System.Management.Automation.PSCredential(AdminID, AdminPword))
End With
pshell.Commands = pCmd
pshell.Runspace = rsp
sess = pshell.Invoke()(0)
pshell.Commands.Clear()
pCmd = New PSCommand()
With pCmd
.AddCommand("Set-ExecutionPolicy")
.AddParameter("Scope", "Process")
.AddParameter("ExecutionPolicy", "Unrestricted")
End With
pshell.Commands = pCmd
pshell.Runspace = rsp
pshell.Invoke()
pshell.Commands.Clear()
pCmd = New PSCommand
With pCmd
.AddCommand("Import-PSSession")
.AddParameter("Session", sess)
End With
pshell.Commands = pCmd
pshell.Runspace = rsp
pshell.Invoke()
End Using
Return rsp
End Function