Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1.         private Runspace runspace;
  2.  
  3.         private void RunSharedMailboxScript()
  4.         {
  5.             string organizationalUnit = GetOrganizationalUnitName(sharedMailboxOrganizationalUnitCombobox.SelectedIndex);
  6.             string mailboxUPN = "$mailbox_alias@afconsult.com";
  7.  
  8.             string newMailBoxScript = $@"New-Mailbox -Name {sharedMailboxNameTextbox.Text} -Alias {sharedMailboxAliasTextbox.Text} -OrganizationalUnit {organizationalUnit} -UserPrincipalName {mailboxUPN} -Shared";
  9.             string addADGroupMemberScript = $@"Add-ADGroupMember 'Office 365 Users' -Members {sharedMailboxAliasTextbox.Text}";
  10.             string setADUserScript = $@"Set-ADUser $mailbox_alias -Description '{sharedMailboxIncidentNumberTextbox.Text} - {DateTime.Now.ToString()}'";
  11.  
  12.             RunConnectToExchangeScript();
  13.  
  14.             RunScript(newMailBoxScript);
  15.             RunScript(addADGroupMemberScript);
  16.             RunScript(setADUserScript);
  17.  
  18.             CloseExchangeScript();
  19.         }
  20.  
  21.         private void RunConnectToExchangeScript()
  22.         {
  23.             string exchangeHostname = "http://afsesthex10.af.se/powershell/";
  24.             // Get credentials
  25.             string userName = connectToExchangeUsernameBox.Text;
  26.             SecureString password = connectToExchangePasswordbox.SecurePassword;
  27.             PSCredential Credential = new PSCredential(userName, password);
  28.  
  29.             // Set the connection info
  30.             WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(exchangeHostname), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", Credential);
  31.             {
  32.                 connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
  33.             }
  34.  
  35.             runspace = RunspaceFactory.CreateRunspace();
  36.             PowerShell powershell = PowerShell.Create();
  37.             PSCommand command = new PSCommand();
  38.             command.AddCommand("New-PSSession");
  39.             command.AddParameter("ConfigurationName", "Microsoft.Exchange");
  40.             command.AddParameter("ConnectionUri", exchangeHostname);
  41.             command.AddParameter("Credential", Credential);
  42.             command.AddParameter("Authentication", "Kerberos");
  43.  
  44.             // -AllowRedirection
  45.             powershell.Commands = command;
  46.  
  47.             // Open the runspace
  48.             runspace.Open();
  49.  
  50.             // Associate the runspace with powershell
  51.             powershell.Runspace = runspace;
  52.  
  53.             // Invoke the powershell to obtain results
  54.             Collection<PSSession> result = powershell.Invoke<PSSession>();
  55.             foreach (ErrorRecord current in powershell.Streams.Error)
  56.                 Console.WriteLine(String.Format($"The following Error happen when opening the remote Runspace: {0} | InnerException: {1}", current.Exception, current.Exception.InnerException));
  57.             Console.ReadLine();
  58.             if (result.Count != 1)
  59.                 throw new Exception("Unexpected number of runspace connections returned.");
  60.            
  61.         }
  62.  
  63.         private void CloseExchangeScript()
  64.         {
  65.             runspace.Close();
  66.         }
  67.  
  68.         private void RunScript(string script)
  69.         {
  70.             Pipeline pipeline = runspace.CreatePipeline();
  71.             pipeline.Commands.AddScript(script);
  72.             pipeline.Invoke();
  73.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement