tag:blogger.com,1999:blog-9152313422880014915.post3511589211600319636..comments2024-03-05T03:37:01.716-07:00Comments on Nick’s Exchange and Scripting Blog: Managing Exchange 2007 Recipients with C#Nick Smithhttp://www.blogger.com/profile/15312621960101025538noreply@blogger.comBlogger63125tag:blogger.com,1999:blog-9152313422880014915.post-48057155710500001982014-08-29T03:46:26.923-06:002014-08-29T03:46:26.923-06:00I am trying to execute the following exchange mana...I am trying to execute the following exchange management shell cmdlets using C# in order to get total number of mailbox on the server.<br /><br />cmdlets:-<br /><br />Get-mailbox -resultsize unlimited <br />My code snippet is as following<br /><br /> PSCredential credential = new PSCredential("Administrator", securePassword); // the password must be of type SecureString<br /> WSManConnectionInfo connectionInfo = new WSManConnectionInfo(connectTo,schemaURI, credential);<br /> connectionInfo.MaximumConnectionRedirectionCount = 5;<br /> connectionInfo.SkipCACheck = true;<br /> connectionInfo.SkipCNCheck = true;<br /><br /> try<br /> {<br /> Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo);<br /> remoteRunspace.Open();<br /> var command = new Command("Get-mailbox");<br /> command.Parameters.Add("resultsize", "unlimited");<br /> var pipeline = remoteRunspace.CreatePipeline();<br /> pipeline.Commands.Add(command);<br /> // Execute the command<br /> var results = pipeline.Invoke();<br /> MessageBox.Show(results.Count.ToString());<br /> remoteRunspace.Dispose();<br /> }<br /> catch (Exception ex)<br /> {<br /> //Handle error <br /> } <br /><br />The above code gives the desired result i.e. total number of mailboxes.But how can i select some properties of all the mailboxes i.e. how can i execute the following cmdlets<br /><br />cmdlets:<br /><br /><br />Get-mailbox | select-object DisplayName, PrimarySmtpAddress, ForwardingAddress, alias, identity, legacyexchangeDN | where-object {$_.ForwardingAddress -ne $Null}<br /><br />Please guid, how can i execute the above given cmdlets... <br />Thank youAnonymoushttps://www.blogger.com/profile/06845852909456042032noreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-10306117983892082582013-11-01T11:53:25.537-06:002013-11-01T11:53:25.537-06:00How can you do this with impersonation?
I've ...How can you do this with impersonation?<br /><br />I've tried a couple of ways and they all fail with impersonation, but work fine without it.Bnoreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-23950180840696091752012-02-20T13:51:25.729-07:002012-02-20T13:51:25.729-07:00For those who are looking for Impersonation, see t...For those who are looking for Impersonation, see this post<br /><br />http://stackoverflow.com/questions/7527469/error-when-using-impersonation-with-powershell-and-exchange-2007-from-c-sharp<br /><br />AczireAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-44064465127123903632011-02-11T05:00:34.275-07:002011-02-11T05:00:34.275-07:00Hi Nick,
I really like your article. Helped me a ...Hi Nick,<br /><br />I really like your article. Helped me a lot to understand how I should organize powershell calls in my app.<br /><br />I see that you keep <b>ExchangeManagementShellWrapper </b> instance in static field. Do you think if ExchangeManagementShellWrapper is hosted in web application that it will be thread safe?<br />Right now, I create Runspace instance for each web request.<br /><br />Thanks,<br />MiroslavMisha N.https://www.blogger.com/profile/03127582462994643908noreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-57157060275421322942011-02-08T23:40:11.402-07:002011-02-08T23:40:11.402-07:00Hi Nick,
I need some help to integrate Exchange S...Hi Nick,<br /><br />I need some help to integrate Exchange Server 2007 using C#.<br /><br />Currently I am doing on my Testing machine which is 32 bit Windows XP(SP3) with Visual Studio 2010. I am using Windows Powershell 1.0 that already shipped with windows.<br /><br />I am using the sample code that you have mentioned in your second post but unfortunality getting error ""Microsoft.Exchange.Management.PowerShell.Admin " is not installed on my machine. Please advice what all prerequiste i need to install on my local machine to test the integration.<br /><br />Thanks & Regards<br />Himanshu BhargavHimanshu Bhargavnoreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-10990978402134727912010-11-22T11:00:23.951-07:002010-11-22T11:00:23.951-07:00Hi Nick,
this looks great, I'm just battling ...Hi Nick, <br />this looks great, I'm just battling to find out how to use this to generate a new alias on an existing mailbox instead of creating a new mailbox.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-14976873946741115172010-11-15T10:16:03.398-07:002010-11-15T10:16:03.398-07:00There were mant troubles, when I lost my Exchange ...There were mant troubles, when I lost my Exchange info. Luckily with the help of one program I resolved it. This issue might determine as well as mine - <a href="http://www.microsoftexchangeemailrecovery.com" rel="nofollow">microsoft exchange email recovery</a>.Alexhttps://www.blogger.com/profile/02892788277177092618noreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-832835704791483852010-08-11T15:33:30.034-06:002010-08-11T15:33:30.034-06:00Excellent work on this one. Question, how could th...Excellent work on this one. Question, how could the following be called:<br />Get-MailboxStatistics -Identity foo | fl totalItemSizeDarren Younghttps://www.blogger.com/profile/05336814955839605599noreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-46035153780890279292010-04-08T12:13:21.286-06:002010-04-08T12:13:21.286-06:00I have Windows 7 64-bit installed, Exchange 2007 S...I have Windows 7 64-bit installed, Exchange 2007 SP2 installed (with supposed PowerShell 2.0 support) and Visual Studio 2008.<br /><br />I'm working on a ASP.NET 2.0 application.<br /><br />When I try and do the same thing, I get "No powershell snap-in's available for v2" (paraphrased)<br /><br />When I run Exchange Management Powershell tool, and I list the snapins it says that the Microsoft.Exchange.Management.Powershell.Admin snap in is PSVersion 1.0 and the Microsoft.Exchange.Management.Powershell.Support snapin is PSVersion 2.0.<br /><br />WHY IS THE ADMIN STUFF NOT VERSION 2.0?!Unknownhttps://www.blogger.com/profile/06878112566239595334noreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-50365390600664330362010-04-07T06:54:03.480-06:002010-04-07T06:54:03.480-06:00Hi,
For Exchange 2010 you can use same code and j...Hi,<br /><br />For Exchange 2010 you can use same code and just have to change this<br /><br />Microsoft.Exchange.Management.PowerShell.Admin<br /><br />to this<br /><br />Microsoft.Exchange.Management.PowerShell.E2010<br /><br />I have tested it successfully.<br /><br />Regards,Laeeq Qazihttp://www.hostingController.comnoreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-63505549818268621752010-01-27T14:58:11.512-07:002010-01-27T14:58:11.512-07:00I agree with "The Iguana"! how do you ma...I agree with "The Iguana"! how do you manage exchange 2010 with c# code???Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-86117101113404105062009-12-03T12:37:09.809-07:002009-12-03T12:37:09.809-07:00This is a great tool. How about Exchange 2010? The...This is a great tool. How about Exchange 2010? The PowerShell interface is very different... (grrr) - could you provide an equivalent class that uses the Ex2010 PS interface?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-21674290883316169202009-11-05T13:55:30.237-07:002009-11-05T13:55:30.237-07:00Hi Nick,
great post
why must it be a singleton? ...Hi Nick,<br /><br />great post<br /><br />why must it be a singleton? i have tried to do it non-singleton, and it works the same...<br /><br />regards<br />PiluAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-2524697524920366112009-10-07T00:00:50.788-06:002009-10-07T00:00:50.788-06:00Hello !!
I use your class in one of my projects a...Hello !!<br /><br />I use your class in one of my projects and it seems the initialisation fails to work when called in a thread.<br /><br />You can find more info on the problem here : http://stackoverflow.com/questions/1013331/powershell-runspace-in-a-thread<br /><br />Thanks for your class !!!!Narfixhttp://blog.narfix.netnoreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-57170170268374544332009-09-16T08:40:11.419-06:002009-09-16T08:40:11.419-06:00Hi Nick,
Nice Article.
I have a question and need...Hi Nick,<br /><br />Nice Article.<br />I have a question and need your help.<br />I am executing the cmdlets using C# code.<br /><br />rc = RunspaceConfiguration.Create();<br /> info = rc.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapEx);<br />r = RunspaceFactory.CreateRunspace(rc);<br />r.Open();<br />ri = new RunspaceInvoke(r);<br /><br />I am executing ret = ri.Invoke(sCmd, null, out errors);<br />where sCmd = "Get-Queue"<br /><br />I get the following error in my application log<br />RunspaceInvoke.Invoke returned error <br />Invoke error: The Queue Viewer operation on computer "localhost" has failed with an exception. The error message is: Access is denied<br /><br />I am able to Run the cmdlet in Exchange Management Shell and get the available queues.<br /><br />Using the same code I am able to run cmd-lets like get-ADSiteLink.<br />But Get-Queue fails.<br /><br />Can I some way debug/log the powershell activity?<br /><br />Please help me.<br /><br />Regards,<br />SeemaSeemanoreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-91458421622778564962009-09-01T14:22:18.798-06:002009-09-01T14:22:18.798-06:00Does anyone know if we can set default scope or ot...Does anyone know if we can set default scope or other $adminsessionADSettings variables in the runspace before we execute a cmdlet? It would be very helpful to know. Thanks,<br />TylerTylerhttps://www.blogger.com/profile/06694755469696355538noreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-11053132114837148312009-08-31T07:01:27.089-06:002009-08-31T07:01:27.089-06:00Hi Nick,
Very nice article.I have one task like i ...Hi Nick,<br />Very nice article.I have one task like i want to execute a command "get-help add-content -detailed" in c# using runspaces and pipeline and get the help details.<br />After invoking the pipeline i would be getting a PSObject for which i have to create nested loops and get the relevant help details like syntax,examples and related links.<br />My question is do we have an option to get the whole help details as a single string.<br />something like ps.properties["Text"] should give all the help details. Thanks in advanceUnknownhttps://www.blogger.com/profile/03835593939596106534noreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-44886456220476877202009-07-07T02:38:07.999-06:002009-07-07T02:38:07.999-06:00Hello Nick,
Very good article. Thanks.
I have t...Hello Nick,<br /><br />Very good article. Thanks.<br /><br />I have to dismount the database using the sample code you provided. What should be the cmdlet to be fired, so the confirmation is not asked. i have following code snippet<br /><br />Command EMSMntCommand = new Command("Dismount-Database");<br />EMSMntCommand.Parameters.Add("Identity", prop2.Value.ToString());<br /> EMSMntCommand.Parameters.Add("Confirm", "FALSE");<br /><br />Here prop2.value.tostring is "Mailbox Database" //the default database available.<br /><br />When i run the code snippet i am getting exception as<br /><br />"An unhandled exception of type 'System.Management.Automation.ParameterBindingException' occurred in System.Management.Automation.dll"<br /><br />Any help would be appreciated.<br /><br />Thank you so much<br /><br />DKUnknownhttps://www.blogger.com/profile/08580391605533347647noreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-28359408109026658172009-06-10T13:14:01.699-06:002009-06-10T13:14:01.699-06:00I love your code and it works great for everything...I love your code and it works great for everything I need to do. However I was wondering if it was possible to specify creds for this. In our enviroment our daily accounts are limited and we have alt accounts we use for privalaged things. So when debuging my normal account isn't an exchange admin so i get the following error, "Access to the address list service on all Exchange 2007 servers has been denied." however when I run Visual Stuido or my project as exchange admin i'm able to complete is there any way I can hardcode Creds into the connection?Unknownhttps://www.blogger.com/profile/15102181527234800247noreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-4017300387571065292009-06-04T03:35:34.451-06:002009-06-04T03:35:34.451-06:00Nick,
In your code you assume that the Identity pa...Nick,<br />In your code you assume that the Identity parameter represents the sAMAccountName property of the user object. Your assumption is not completely wrong, the parameter must be specified differently though e.g. as DOMAIN\sAMAccountName.<br />Only in this way you can be sure the query for that user will search for the sAMAccountName, and not the name property instead.<br /><br />Greetz,<br /><br />MikeAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-34750743207326455032009-04-20T03:50:00.000-06:002009-04-20T03:50:00.000-06:00Hi Nick,
Great example. But, I am not able to exe...Hi Nick,<br /><br />Great example. But, I am not able to execute simple Get-Mailboxstatistics command though it works find through powershell.<br /><br />Any ideas why ?<br /><br />Thanks<br />JohnAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-85143456381927932612009-02-19T17:35:00.000-07:002009-02-19T17:35:00.000-07:00Nick,This is great! My code is executing in one d...Nick,<BR/>This is great! My code is executing in one domain. I need to create a mailbox in another domain. I'm running as an impersonated domain admin in the domain I'm trying to create the mailbox in. Using Exchange Management Shell I set default scope in $adminsessionADSettings to the domain I want to create the <BR/>mailbox in. I also set the configuration domain controller to a domain controller in that domain. Then I use the enable-mailbox command.<BR/><BR/>My question is: How do I duplicate setting $adminsessionADSettings in C#?<BR/><BR/>Thanks,<BR/>CandiAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-32754959301839782022009-01-20T01:38:00.000-07:002009-01-20T01:38:00.000-07:00Really great this what I wanted to know but still ...Really great this what I wanted to know but still I am facing a issue that I am unable to execute any of the exchange relalted cmdlets .when ever I try to run the code it says<BR/>Unhandled Exception: System.Management.Automation.CommandNotFoundException: The<BR/>term 'Get-excommand' is not recognized as a cmdlet, function, operable program,<BR/>or script file. Verify the term and try again.<BR/> at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String com<BR/>mandName)<BR/> at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(Strin<BR/>g commandName, CommandOrigin commandOrigin)<BR/> at System.Management.Automation.CommandFactory._CreateCommand(String commandN<BR/>ame, CommandOrigin commandOrigin)<BR/> at System.Management.Automation.CommandFactory.CreateCommand(String commandNa<BR/>me, CommandOrigin commandOrigin)<BR/> at System.Management.Automation.Runspaces.Command.CreateCommandProcessor(Exec<BR/>utionContext executionContext, CommandFactory commandFactory, Boolean addToHisto<BR/>ry)<BR/> at System.Management.Automation.Runspaces.LocalPipeline.CreatePipelineProcess<BR/>or()<BR/> at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()<BR/> at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-61700831671190085232009-01-02T20:53:00.000-07:002009-01-02T20:53:00.000-07:00Mahesh,After playing around with the Move-Database...Mahesh,<BR/><BR/>After playing around with the Move-DatabasePath cmdlet I found that you need both the -Confirm:$False and -Force parameters to avoid a confirmaion prompt (there are actually two confirmation prompts when running this command). I was able to get the code to run with the following commands:<BR/><BR/> string strCommand = "Move-DatabasePath";<BR/> string strIdentity = @"EX2007ALL\First Storage Group\Mailbox Database";<BR/> string strEDBFilePath = @"C:\Exchange\Databases\Mailbox Database.edb";<BR/><BR/> Command myCommand = new Command(strCommand);<BR/> myCommand.Parameters.Add("Identity", strIdentity);<BR/> myCommand.Parameters.Add("EdbFilePath", strEDBFilePath);<BR/> myCommand.Parameters.Add("OutVariable", "$Status");<BR/> myCommand.Parameters.Add("Force");<BR/> myCommand.Parameters.Add("Confirm", false);<BR/> results = ems.PipelineInvoke(myCommand);<BR/><BR/>--NickNick Smithhttps://www.blogger.com/profile/15312621960101025538noreply@blogger.comtag:blogger.com,1999:blog-9152313422880014915.post-87817302290605811492008-12-11T23:24:00.000-07:002008-12-11T23:24:00.000-07:00Nicek, this is a fantastic page for people who are...Nicek, this is a fantastic page for people who are struggling with exchange shell commands. Thank you. I have a problem I need to address:<BR/><BR/>I am writing C# code to execute the Move-DatabasePath and Move-StorageGroupPath commands programmatically. They look like this:<BR/><BR/>Move-DatabasePath –Identity ‘MAIL2K7\First Storage Group\Mailbox Database’ –EdbFilePath ‘X:\MailStore\Mailbox Database.edb’ –Confirm:$False<BR/><BR/>Move-StorageGroupPath ‘MAIL2K7\First Storage Group’ –LogFolderPath ‘X:\MailStore’ –SystemFolderPath ‘X:\MailStore’ –ConfigurationOnly –Confirm:$False<BR/> <BR/>The code I have is as follows:<BR/><BR/>Command myCommand = new Command(strCommand);<BR/>myCommand.Parameters.Add("Identity", strIdentity);<BR/>myCommand.Parameters.Add("EdbFilePath", strEDBFilePath);<BR/>myCommand.Parameters.Add("Confirm", false);<BR/>pipeLine.Commands.Add(myCommand);<BR/>ICollection< PSObject > results = pipeLine.Invoke();<BR/><BR/>When I run this, I get an exception: “System.Management.Automation.CmdletInvocationException: Cannot invoke this function because the current host does not implement it. ---> System.Management.Automation.Host.HostException: Cannot invoke this function because the current host does not implement it.”<BR/><BR/>I think it is because of the ‘Confirm’ parameter. These commands ask for confirmation, and I think the pipeline doesn’t accept confirm programmatically.<BR/><BR/>How can I programmatically send the Confirm -> False argument?<BR/><BR/>If you could help me with this, that would be awesome.bloggermhttps://www.blogger.com/profile/06375109573942783487noreply@blogger.com