exchange-server-2013 https://www.e-learn.cn/tag/exchange-server-2013 zh-hans Create exchange mailbox in c# https://www.e-learn.cn/topic/2446840 <span>Create exchange mailbox in c#</span> <span><span lang="" about="/user/73" typeof="schema:Person" property="schema:name" datatype="">≡放荡痞女</span></span> <span>2019-12-13 05:36:16</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I want to create Mailbox in exchange server 2013 using c# .</p> <p>I tried lots of codes but each one gets an error that there is no obvious solution to solve it. </p> <p>my code is </p> <pre><code>public static Boolean CreateUser(string FirstName, string LastName, string Alias,string PassWord, string DomainName, string OrganizationalUnit) { string Name = FirstName + " " + LastName; string PrincipalName = FirstName + "." + LastName + "@" + DomainName; Boolean success = false; string consolePath = @"C:\Program Files\Microsoft\Exchange Server\V15\bin\exshell.psc1"; PSConsoleLoadException pSConsoleLoadException = null; RunspaceConfiguration rsConfig = RunspaceConfiguration.Create(consolePath, out pSConsoleLoadException); SecureString spassword = new SecureString(); spassword.Clear(); foreach (char c in PassWord) { spassword.AppendChar(c); } PSSnapInException snapInException = null; Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig); myRunSpace.Open(); Pipeline pipeLine = myRunSpace.CreatePipeline(); Command myCommand = new Command("New-MailBox"); myCommand.Parameters.Add("Name", Name); myCommand.Parameters.Add("Alias", Alias); myCommand.Parameters.Add("UserPrincipalName", PrincipalName); myCommand.Parameters.Add("Confirm", true); myCommand.Parameters.Add("SamAccountName", Alias); myCommand.Parameters.Add("FirstName", FirstName); myCommand.Parameters.Add("LastName", LastName); myCommand.Parameters.Add("Password", spassword); myCommand.Parameters.Add("ResetPasswordOnNextLogon", false); myCommand.Parameters.Add("OrganizationalUnit", OrganizationalUnit); pipeLine.Commands.Add(myCommand); pipeLine.Invoke(); // got an error here myRunSpace.Dispose(); } </code></pre> <p>and call it :</p> <pre><code>Boolean Success = CreateUser("firstname", "lastName", "aliasName", "AAaa12345", "mydomain.com", "mydomain.com/Users"); </code></pre> <p>which I get this error :</p> <pre><code>Additional information: The term 'New-MailBox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. </code></pre> <p>and another code that I test is:</p> <pre><code>string userName = "administrator"; string password = "mypass"; System.Security.SecureString securePassword = new System.Security.SecureString(); foreach (char c in password) { securePassword.AppendChar(c); } PSCredential credential = new PSCredential(userName, securePassword); WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://{my server IP}/POWERSHELL/Microsoft.Exchange"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential); connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; connectionInfo.SkipCACheck = true; connectionInfo.SkipCNCheck = true; connectionInfo.MaximumConnectionRedirectionCount = 2; using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo)) { runspace.Open(); using (PowerShell powershell = PowerShell.Create()) { powershell.Runspace = runspace; //Create the command and add a parameter powershell.AddCommand("Get-Mailbox"); powershell.AddParameter("RecipientTypeDetails", "UserMailbox"); //Invoke the command and store the results in a PSObject collection Collection&lt;PSObject&gt; results = powershell.Invoke(); //Iterate through the results and write the DisplayName and PrimarySMTP //address for each mailbox foreach (PSObject result in results) { Console.WriteLine( string.Format("Name: { 0}, PrimarySmtpAddress: { 1}", result.Properties["DisplayName"].Value.ToString(), result.Properties["PrimarySmtpAddress"].Value.ToString() )); } } } </code></pre> <p>and I get this error</p> <pre><code>Additional information: Connecting to remote server {Server IP Address} failed with the following error message : [ClientAccessServer=WIN-FRP2TC5SKRG,BackEndServer=,RequestId=460bc5fe-f809-4454-8472-ada97eacb9fb,TimeStamp=4/6/2016 6:23:28 AM] Access is denied. For more information, see the about_Remote_Troubleshooting Help topic. </code></pre> <p>I think I gave every permission which is needed to my administrator user and firewall is off but it doesn't work yet. </p> <p>Any help or hint !! thanks</p> <br /><h3>回答1:</h3><br /><p>Try this:</p> <pre><code>//Secure String string pwd = "Password"; char[] cpwd = pwd.ToCharArray(); SecureString ss = new SecureString(); foreach (char c in cpwd) ss.AppendChar(c); //URI Uri connectTo = new Uri("http://exchserver.domain.local/PowerShell"); string schemaURI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"; //PS Credentials PSCredential credential = new PSCredential("Domain\\administrator", ss); WSManConnectionInfo connectionInfo = new WSManConnectionInfo(connectTo, schemaURI, credential); connectionInfo.MaximumConnectionRedirectionCount = 5; connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo); remoteRunspace.Open(); PowerShell ps = PowerShell.Create(); ps.Runspace = remoteRunspace; ps.Commands.AddCommand("Get-Mailbox"); ps.Commands.AddParameter("Identity","user@domain.local"); foreach (PSObject result in ps.Invoke()) { Console.WriteLine("{0,-25}{1}", result.Members["DisplayName"].Value, result.Members["PrimarySMTPAddress"].Value); } </code></pre> <br /><br /><br /><h3>回答2:</h3><br /><p>I unchecked "prefer 32-bit" and changed platform target to x64, the problem was solved.</p> <p>with the following code : </p> <pre><code> public class ExchangeShellExecuter { public Collection&lt;PSObject&gt; ExecuteCommand(Command command) { RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create(); PSSnapInException PSException = null; PSSnapInInfo info = runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException); Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf); runspace.Open(); Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.Add(command); Collection&lt;PSObject&gt; result = pipeline.Invoke(); return result ; } } public class ExchangeShellCommand { public Command NewMailBox(string userLogonName,string firstName,string lastName,string password ,string displayName,string organizationUnit = "mydomain.com/Users", string database = "Mailbox Database 1338667540", bool resetPasswordOnNextLogon = false) { try { SecureString securePwd = ExchangeShellHelper.StringToSecureString(password); Command command = new Command("New-Mailbox"); var name = firstName + " " + lastName; command.Parameters.Add("FirstName", firstName); command.Parameters.Add("LastName", lastName); command.Parameters.Add("Name", name); command.Parameters.Add("Alias", userLogonName); command.Parameters.Add("database", database); command.Parameters.Add("Password", securePwd); command.Parameters.Add("DisplayName", displayName); command.Parameters.Add("UserPrincipalName", userLogonName+ "@mydomain.com"); command.Parameters.Add("OrganizationalUnit", organizationUnit); //command.Parameters.Add("ResetPasswordOnNextLogon", resetPasswordOnNextLogon); return command; } catch (Exception) { throw; } } public Command AddEmail(string email, string newEmail) { try { Command command = new Command("Set-mailbox"); command.Parameters.Add("Identity", email); command.Parameters.Add("EmailAddresses", newEmail); command.Parameters.Add("EmailAddressPolicyEnabled", false); return command; } catch (Exception) { throw; } // } public Command SetDefaultEmail(string userEmail, string emailToSetAsDefault) { try { Command command = new Command("Set-mailbox"); command.Parameters.Add("Identity", userEmail); command.Parameters.Add("PrimarySmtpAddress", emailToSetAsDefault); return command; } catch (Exception) { throw; } //PrimarySmtpAddress } } </code></pre> <p>and run with : </p> <pre><code> var addEmailCommand = new ExchangeShellCommand().AddEmail("unos4@mydomain.com","unos.bm65@yahoo.com"); var res2 = new ExchangeShellExecuter().ExecuteCommand(addEmailCommand); var emailDefaultCommand = new ExchangeShellCommand().AddSetDefaultEmail("unos4@mydomain.com", "unos.bm65@yahoo.com"); var res3 = new ExchangeShellExecuter().ExecuteCommand(emailDefaultCommand); </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/36449219/create-exchange-mailbox-in-c-sharp</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c" hreflang="zh-hans">c#</a></div> <div class="field--item"><a href="/tag/powershell" hreflang="zh-hans">powershell</a></div> <div class="field--item"><a href="/tag/exchange-server-2013" hreflang="zh-hans">exchange-server-2013</a></div> </div> </div> Thu, 12 Dec 2019 21:36:16 +0000 ≡放荡痞女 2446840 at https://www.e-learn.cn Cannot retrieve any room list from Exchg 2013 with c# https://www.e-learn.cn/topic/2355974 <span>Cannot retrieve any room list from Exchg 2013 with c#</span> <span><span lang="" about="/user/16" typeof="schema:Person" property="schema:name" datatype="">筅森魡賤</span></span> <span>2019-12-12 05:09:58</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>My exchange services works. I can see all rooms by Outlook and Can see all rooms through Powershell. But with this snippet I cannot retrieve any room</p> <pre><code> ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1); service.UseDefaultCredentials = true; service.Url = new Uri("https://my server/ews/exchange.asmx"); service.AutodiscoverUrl("username@myserver.com", RedirectionCallback); EmailAddressCollection myRoomLists = service.GetRoomLists(); // Display the room lists. foreach (EmailAddress address in myRoomLists) { Console.WriteLine("Email Address: {0} Mailbox Type: {1}", address.Address, address.MailboxType); } </code></pre> <p>The list is empty!</p> <br /><h3>回答1:</h3><br /><p>It sounds like your Exchange administrator has not configured any room lists. EWS depends on the presence of room lists in the GAL to work. See https://technet.microsoft.com/en-us/library/jj215781(v=exchg.150).aspx for details.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>See here for more info. You need to iterate through the room lists within the collection you got from GetRoomLists(), and then iterate through the conference rooms in each room list using service.GetRooms(myRoomList).</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/31006063/cannot-retrieve-any-room-list-from-exchg-2013-with-c-sharp</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/exchange-server" hreflang="zh-hans">exchange-server</a></div> <div class="field--item"><a href="/tag/exchangewebservices" hreflang="zh-hans">exchangewebservices</a></div> <div class="field--item"><a href="/tag/exchange-server-2013" hreflang="zh-hans">exchange-server-2013</a></div> </div> </div> Wed, 11 Dec 2019 21:09:58 +0000 筅森魡賤 2355974 at https://www.e-learn.cn how to get exact user name from get-mobiledevicestatistics in powershell https://www.e-learn.cn/topic/2165812 <span>how to get exact user name from get-mobiledevicestatistics in powershell</span> <span><span lang="" about="/user/226" typeof="schema:Person" property="schema:name" datatype="">泄露秘密</span></span> <span>2019-12-11 01:45:26</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have a small script to get all user mobile devices info from exchange 2013 server.</p> <pre><code>Get-Mailbox -ResultSize Unlimited | ForEach {Get-MobileDeviceStatistics -Mailbox:$_.Identity} | Select-Object @{label="User" ; expression={$_.Identity}},DeviceOS, lastsuccesssync </code></pre> <p>I just want to get an exact user name instead of a path in AD. How can I do it in <code>expression={?}</code></p> <p>Here is another script to do it, it gives me the user name, but all devices belongs to user are not in separated lines, they all in one line...</p> <pre><code>$EASMailboxes = Get-CASMailbox -Filter {HasActiveSyncDevicePartnership -eq $True -and DisplayName -notlike "CAS_{*"} | Get-Mailbox $EASMailboxes | Select-Object DisplayName, PrimarySMTPAddress, @{Name="Mobile Devices";Expression={(Get-MobileDeviceStatistics -Mailbox $_.Identity).DeviceOS}} | Out-GridView </code></pre> <br /><h3>回答1:</h3><br /><p>I don't have the environment to test this but is this not what you are looking for ?</p> <pre class="lang-powershell prettyprint-override"><code>Get-Mailbox -ResultSize Unlimited | ForEach { $user = $_.SamAccountName Get-MobileDeviceStatistics -Mailbox:$_.Identity | Select-Object @{label="User" ; expression={$user}},DeviceOS, lastsuccesssync } </code></pre> <p>That should output the user for every device they own on its own line. You could then easily export this to <code>Export-CSV</code> or some such thing that way.</p> <p>We save the <code>$user</code> so it is available later in the pipe. Could also have used <code>Add-Member</code> but the result would have been the same. </p> <br /><br /><br /><h3>回答2:</h3><br /><p>If you have the identity field, which looks like this </p> <pre><code>domain.com/Users/OU/UserName/ExchangeActiveSyncDevices/iPhone </code></pre> <p>Then to split on the <code>/</code> and get the third result, you simply request:</p> <pre><code>$_.Identity.Split("/")[3] &gt;UserName </code></pre> <p>PowerShell begins indexing with number zero, so to request the fourth entry in the list, we request index number 3. </p> <p><strong>Update</strong></p> <p>OP mentioned that the OU level might vary, meaning that he couldn't count on a fixed position to request the Index. In order to accomodte that scenario, try this method, which will look for the index of ExchangeActiveSyncDevices and then pick the index position before that.</p> <pre><code>$_.Identity.Split('/')[($_.Identity.Split('/').Indexof('ExchangeActiveSyncDevices')-1)] </code></pre> <br /><br /><br /><h3>回答3:</h3><br /><pre><code>Get-Mailbox -resultsize unlimited|foreach {Get-MobileDeviceStatistics -Mailbox:$_.identity} |Select-Object @{l="user";e={$_.Identity.parent.parent.name}}, lastsuccesssync on e2k13 </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/29475711/how-to-get-exact-user-name-from-get-mobiledevicestatistics-in-powershell</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/powershell" hreflang="zh-hans">powershell</a></div> <div class="field--item"><a href="/tag/exchange-server" hreflang="zh-hans">exchange-server</a></div> <div class="field--item"><a href="/tag/exchange-server-2013" hreflang="zh-hans">exchange-server-2013</a></div> </div> </div> Tue, 10 Dec 2019 17:45:26 +0000 泄露秘密 2165812 at https://www.e-learn.cn