{"id":136,"date":"2020-01-09T09:34:03","date_gmt":"2020-01-09T14:34:03","guid":{"rendered":"http:\/\/ayling.tech\/blog\/?p=136"},"modified":"2023-01-04T16:33:55","modified_gmt":"2023-01-04T21:33:55","slug":"powershell-and-exchange-with-c","status":"publish","type":"post","link":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/","title":{"rendered":"PowerShell and Exchange with C#"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When writing a program that needs to interact with Exchange (whether on-premises or off-premises\/online), the easiest way is to use PowerShell commands.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is done by adding the following above your namespace declaration:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">using System.Management.Automation;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The key thing that I have found to be a problem for C# programs using PowerShell is that occasionally you will receive an error message that says &#8220;A connection to the directory on which to process the request was unavailable. This is likely a transient condition.&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is caused by trying to query PowerShell too often. If you are using PowerShell to query Exchange, you have a connection string that you will use &#8211; something like these two commands:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$Global:exchangeUri = \"http:\/\/powershell.onyourdomain\";$session = New-PSSession -Name $Global:sessionName -ConfigurationName Microsoft.Exchange -ConnectionUri $Global:exchangeUri -Authentication Kerberos;\nImport-PSSession $session<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you attempt to do this every time you want to update some information in Exchange, or every time you want to query something in Exchange, you will quickly experience the transient condition error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To avoid this, make a connection once per session. I have done this, in the code below, using an object named &#8220;Exchange&#8221; (because I&#8217;m imaginative like that):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class Exchange\n{\n    public Exchange(bool onPrem, string userEmail = \"NoEmail\", string password = \"NoPassword\")\n    {\n        OnPrem = onPrem;\n        if (!OnPrem)\n        {\n            if (password == \"NoPassword\" || userEmail == \"NoEmail\")\n                throw new ArgumentException(\"If Online Exchange, we must have a username and password passed\");\n            CreateCredential(password, userEmail);\n        }\n    }\n    Runspace _LocalRunSpace { get; set; }\n    Runspace LocalRunSpace\n    {\n        get\n        {\n            if (_LocalRunSpace == null)\n            {\n                _LocalRunSpace = RunspaceFactory.CreateRunspace();\n                _LocalRunSpace.Open();\n            }\n            return _LocalRunSpace;\n        }\n    }\n    private PSCredential Credential;\n    private PSCredential CreateCredential(string password, string emailAddress)\n    {\n        SecureString secpassword = new SecureString();\n        foreach (char c in password)\n            secpassword.AppendChar(c);\n        return Credential = new PSCredential(emailAddress, secpassword);\n    }\n    private bool OnPrem { get; set; }\n private PowerShell _powershell { get; set; }\nprivate PowerShell PowerShellInstance\n{\n     get\n     {\n          if (_powershell == null)\n          {\n             _powershell = PowerShell.Create();\n             _powershell.Runspace = LocalRunSpace;\n             if (OnPrem)\n             {\n                  _powershell.AddScript(exchangeScript);\n                  Collection results = _powershell.Invoke();\n                  foreach (PSObject result in results)\n                      Console.WriteLine(result.ToString());\n              }\n              else\n              {\n                  var newSession = _powershell.AddCommand(\"New-PSSession\")\n                         .AddParameter(\"ConfigurationName\", \"Microsoft.Exchange\")\n                           .AddParameter(\"ConnectionUri\", \"https:\/\/ps.outlook.com\/powershell\/\")\n                           .AddParameter(\"Credential\", Credential)\n                           .AddParameter(\"Authentication\", \"Basic\")\n                           .AddParameter(\"AllowRedirection\", true)\n                           .Invoke();\n                  var session = newSession[0];\n                  _powershell.Commands.Clear();\n                  Collection results = _powershell\n                           .AddCommand(\"Import-PSSession\")\n                           .AddParameter(\"Session\", session)\n                           .Invoke();\n                  <code>foreach (PSObject result in results)                           <\/code>\n                         <code>Console.WriteLine(result.ToString());                              <\/code>\n              <code>}<\/code>\n           <code>}<\/code>\n     <code>return _powershell;<\/code>\n     <code>}<\/code>\n<code>}<\/code>   <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can then use a method like the following, to run commands against the existing PowerShell connection. When I need both online exchange, and on-prem exchange, I create two instances of the Exchange object, and each will persist for as long as they are needed:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public string RunCommand(string command)\n{\n     try\n     {\n          PowerShellInstance.Commands.Clear();\n          PowerShellInstance.AddScript(command);          \n          PowerShellInstance.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);\n          Collection results;\n          try { results = PowerShellInstance.Invoke(); }\n          catch (System.Threading.ThreadAbortException threadError)\n          {\n                Console.WriteLine(\"PowerShell script ran into thread error:\\n\" + threadError.Message);\n                return string.Empty;\n           }\n <code>          string dataToReturn = string.Empty;<\/code>\n           <code>foreach (PSObject result in results)<\/code>\n                <code>dataToReturn += string.Format(result.ToString(), \"; \");              <\/code>\n           <code>return dataToReturn;<\/code>\n      <code>}<\/code>\n      <code>catch { return string.Empty; }<\/code>\n<code>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I hope this is helpful. If you have any questions or comments, please use the comments section below!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"shortbooksLink\">I write short stories and o\u00feer fiction &#8211; check \u00feem out at <a href=\"https:\/\/shortbooks.online\" title=\"\">shortbooks.online<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Like many creative people on \u00fee internet, I have a Patreon account. If you would like to support my creative writing (on <a href=\"https:\/\/shortbooks.online\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/shortbooks.online<\/a>) or my blogging efforts, please take a look at <a href=\"https:\/\/www.patreon.com\/gavinayling\" target=\"_blank\" rel=\"noreferrer noopener\">my Patreon page<\/a>.<\/p>\n\n\n\n<a href=\"https:\/\/www.patreon.com\/bePatron?u=58356065\" data-patreon-widget-type=\"become-patron-button\">Become a Patron!<\/a><script async=\"\" src=\"https:\/\/c6.patreon.com\/becomePatronButton.bundle.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>When writing a program that needs to interact with Exchange (whether on-premises or off-premises\/online), the easiest way is to use PowerShell commands. This is done by adding the following above your namespace declaration: using System.Management.Automation; The key thing that I have found to be a problem for C# programs using PowerShell is that occasionally you will receive an error message that says &#8220;A connection to the directory on which to process the request was unavailable. This is likely a transient&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":127,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","footnotes":""},"categories":[14],"tags":[15,36,34,35],"class_list":["post-136","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code","tag-c","tag-object","tag-powershell","tag-transient-condition"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"When writing a program that needs to interact with Exchange (whether on-premises or off-premises\/online), the easiest way is to use PowerShell commands. This is done by adding the following above your namespace declaration: using System.Management.Automation; The key thing that I have found to be a problem for C# programs using PowerShell is that occasionally you\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"A\u00deling\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_GB\" \/>\n\t\t<meta property=\"og:site_name\" content=\"A\u00deling&#039;s Thoughts - Exploring the boundaries of possibility\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"PowerShell and Exchange with C# - A\u00deling&#039;s Thoughts\" \/>\n\t\t<meta property=\"og:description\" content=\"When writing a program that needs to interact with Exchange (whether on-premises or off-premises\/online), the easiest way is to use PowerShell commands. This is done by adding the following above your namespace declaration: using System.Management.Automation; The key thing that I have found to be a problem for C# programs using PowerShell is that occasionally you\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2020-01-09T14:34:03+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2023-01-04T21:33:55+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codecopyblog\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@GavinAyling\" \/>\n\t\t<meta name=\"twitter:title\" content=\"PowerShell and Exchange with C# - A\u00deling&#039;s Thoughts\" \/>\n\t\t<meta name=\"twitter:description\" content=\"When writing a program that needs to interact with Exchange (whether on-premises or off-premises\/online), the easiest way is to use PowerShell commands. This is done by adding the following above your namespace declaration: using System.Management.Automation; The key thing that I have found to be a problem for C# programs using PowerShell is that occasionally you\" \/>\n\t\t<meta name=\"twitter:creator\" content=\"@GavinAyling\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/2020\\\/powershell-and-exchange-with-c\\\/#blogposting\",\"name\":\"PowerShell and Exchange with C# - A\\u00deling's Thoughts\",\"headline\":\"PowerShell and Exchange with C#\",\"author\":{\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/author\\\/gavinayling-tech\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/12\\\/Code.png\",\"width\":768,\"height\":486},\"datePublished\":\"2020-01-09T09:34:03-05:00\",\"dateModified\":\"2023-01-04T16:33:55-05:00\",\"inLanguage\":\"en-GB\",\"commentCount\":8,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/2020\\\/powershell-and-exchange-with-c\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/2020\\\/powershell-and-exchange-with-c\\\/#webpage\"},\"articleSection\":\"Code, C#, object, PowerShell, transient condition\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/2020\\\/powershell-and-exchange-with-c\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/shortbooks.online\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/category\\\/code\\\/#listItem\",\"name\":\"Code\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/category\\\/code\\\/#listItem\",\"position\":2,\"name\":\"Code\",\"item\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/category\\\/code\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/2020\\\/powershell-and-exchange-with-c\\\/#listItem\",\"name\":\"PowerShell and Exchange with C#\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/2020\\\/powershell-and-exchange-with-c\\\/#listItem\",\"position\":3,\"name\":\"PowerShell and Exchange with C#\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/category\\\/code\\\/#listItem\",\"name\":\"Code\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/#person\",\"name\":\"A\\u00deling\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/2020\\\/powershell-and-exchange-with-c\\\/#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ad909f588608abc0d84ee3456d38272e0835b3a5af4db8e6d168d91ac390960c?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"A\\u00deling\"},\"sameAs\":[\"https:\\\/\\\/twitter.com\\\/GavinAyling\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/author\\\/gavinayling-tech\\\/#author\",\"url\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/author\\\/gavinayling-tech\\\/\",\"name\":\"A\\u00deling\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/2020\\\/powershell-and-exchange-with-c\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ad909f588608abc0d84ee3456d38272e0835b3a5af4db8e6d168d91ac390960c?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"A\\u00deling\"},\"sameAs\":[\"https:\\\/\\\/twitter.com\\\/GavinAyling\"]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/2020\\\/powershell-and-exchange-with-c\\\/#webpage\",\"url\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/2020\\\/powershell-and-exchange-with-c\\\/\",\"name\":\"PowerShell and Exchange with C# - A\\u00deling's Thoughts\",\"description\":\"When writing a program that needs to interact with Exchange (whether on-premises or off-premises\\\/online), the easiest way is to use PowerShell commands. This is done by adding the following above your namespace declaration: using System.Management.Automation; The key thing that I have found to be a problem for C# programs using PowerShell is that occasionally you\",\"inLanguage\":\"en-GB\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/2020\\\/powershell-and-exchange-with-c\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/author\\\/gavinayling-tech\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/author\\\/gavinayling-tech\\\/#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/12\\\/Code.png\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/2020\\\/powershell-and-exchange-with-c\\\/#mainImage\",\"width\":768,\"height\":486},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/2020\\\/powershell-and-exchange-with-c\\\/#mainImage\"},\"datePublished\":\"2020-01-09T09:34:03-05:00\",\"dateModified\":\"2023-01-04T16:33:55-05:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/\",\"name\":\"Thoughts from Gavin A\\u00deling\",\"description\":\"Exploring the boundaries of possibility\",\"inLanguage\":\"en-GB\",\"publisher\":{\"@id\":\"https:\\\/\\\/shortbooks.online\\\/blog\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"PowerShell and Exchange with C# - A\u00deling's Thoughts","description":"When writing a program that needs to interact with Exchange (whether on-premises or off-premises\/online), the easiest way is to use PowerShell commands. This is done by adding the following above your namespace declaration: using System.Management.Automation; The key thing that I have found to be a problem for C# programs using PowerShell is that occasionally you","canonical_url":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/#blogposting","name":"PowerShell and Exchange with C# - A\u00deling's Thoughts","headline":"PowerShell and Exchange with C#","author":{"@id":"https:\/\/shortbooks.online\/blog\/author\/gavinayling-tech\/#author"},"publisher":{"@id":"https:\/\/shortbooks.online\/blog\/#person"},"image":{"@type":"ImageObject","url":"https:\/\/shortbooks.online\/blog\/wp-content\/uploads\/2019\/12\/Code.png","width":768,"height":486},"datePublished":"2020-01-09T09:34:03-05:00","dateModified":"2023-01-04T16:33:55-05:00","inLanguage":"en-GB","commentCount":8,"mainEntityOfPage":{"@id":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/#webpage"},"isPartOf":{"@id":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/#webpage"},"articleSection":"Code, C#, object, PowerShell, transient condition"},{"@type":"BreadcrumbList","@id":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/shortbooks.online\/blog#listItem","position":1,"name":"Home","item":"https:\/\/shortbooks.online\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/shortbooks.online\/blog\/category\/code\/#listItem","name":"Code"}},{"@type":"ListItem","@id":"https:\/\/shortbooks.online\/blog\/category\/code\/#listItem","position":2,"name":"Code","item":"https:\/\/shortbooks.online\/blog\/category\/code\/","nextItem":{"@type":"ListItem","@id":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/#listItem","name":"PowerShell and Exchange with C#"},"previousItem":{"@type":"ListItem","@id":"https:\/\/shortbooks.online\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/#listItem","position":3,"name":"PowerShell and Exchange with C#","previousItem":{"@type":"ListItem","@id":"https:\/\/shortbooks.online\/blog\/category\/code\/#listItem","name":"Code"}}]},{"@type":"Person","@id":"https:\/\/shortbooks.online\/blog\/#person","name":"A\u00deling","image":{"@type":"ImageObject","@id":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/ad909f588608abc0d84ee3456d38272e0835b3a5af4db8e6d168d91ac390960c?s=96&d=mm&r=g","width":96,"height":96,"caption":"A\u00deling"},"sameAs":["https:\/\/twitter.com\/GavinAyling"]},{"@type":"Person","@id":"https:\/\/shortbooks.online\/blog\/author\/gavinayling-tech\/#author","url":"https:\/\/shortbooks.online\/blog\/author\/gavinayling-tech\/","name":"A\u00deling","image":{"@type":"ImageObject","@id":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/ad909f588608abc0d84ee3456d38272e0835b3a5af4db8e6d168d91ac390960c?s=96&d=mm&r=g","width":96,"height":96,"caption":"A\u00deling"},"sameAs":["https:\/\/twitter.com\/GavinAyling"]},{"@type":"WebPage","@id":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/#webpage","url":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/","name":"PowerShell and Exchange with C# - A\u00deling's Thoughts","description":"When writing a program that needs to interact with Exchange (whether on-premises or off-premises\/online), the easiest way is to use PowerShell commands. This is done by adding the following above your namespace declaration: using System.Management.Automation; The key thing that I have found to be a problem for C# programs using PowerShell is that occasionally you","inLanguage":"en-GB","isPartOf":{"@id":"https:\/\/shortbooks.online\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/#breadcrumblist"},"author":{"@id":"https:\/\/shortbooks.online\/blog\/author\/gavinayling-tech\/#author"},"creator":{"@id":"https:\/\/shortbooks.online\/blog\/author\/gavinayling-tech\/#author"},"image":{"@type":"ImageObject","url":"https:\/\/shortbooks.online\/blog\/wp-content\/uploads\/2019\/12\/Code.png","@id":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/#mainImage","width":768,"height":486},"primaryImageOfPage":{"@id":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/#mainImage"},"datePublished":"2020-01-09T09:34:03-05:00","dateModified":"2023-01-04T16:33:55-05:00"},{"@type":"WebSite","@id":"https:\/\/shortbooks.online\/blog\/#website","url":"https:\/\/shortbooks.online\/blog\/","name":"Thoughts from Gavin A\u00deling","description":"Exploring the boundaries of possibility","inLanguage":"en-GB","publisher":{"@id":"https:\/\/shortbooks.online\/blog\/#person"}}]},"og:locale":"en_GB","og:site_name":"A\u00deling's Thoughts - Exploring the boundaries of possibility","og:type":"article","og:title":"PowerShell and Exchange with C# - A\u00deling's Thoughts","og:description":"When writing a program that needs to interact with Exchange (whether on-premises or off-premises\/online), the easiest way is to use PowerShell commands. This is done by adding the following above your namespace declaration: using System.Management.Automation; The key thing that I have found to be a problem for C# programs using PowerShell is that occasionally you","og:url":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/","article:published_time":"2020-01-09T14:34:03+00:00","article:modified_time":"2023-01-04T21:33:55+00:00","article:publisher":"https:\/\/www.facebook.com\/codecopyblog","twitter:card":"summary","twitter:site":"@GavinAyling","twitter:title":"PowerShell and Exchange with C# - A\u00deling's Thoughts","twitter:description":"When writing a program that needs to interact with Exchange (whether on-premises or off-premises\/online), the easiest way is to use PowerShell commands. This is done by adding the following above your namespace declaration: using System.Management.Automation; The key thing that I have found to be a problem for C# programs using PowerShell is that occasionally you","twitter:creator":"@GavinAyling"},"aioseo_meta_data":{"post_id":"136","title":null,"description":null,"keywords":[],"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[],"defaultGraph":"","defaultPostTypeGraph":""},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2022-01-10 19:03:27","updated":"2025-06-04 03:52:45","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/shortbooks.online\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/shortbooks.online\/blog\/category\/code\/\" title=\"Code\">Code<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tPowerShell and Exchange with C#\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/shortbooks.online\/blog"},{"label":"Code","link":"https:\/\/shortbooks.online\/blog\/category\/code\/"},{"label":"PowerShell and Exchange with C#","link":"https:\/\/shortbooks.online\/blog\/2020\/powershell-and-exchange-with-c\/"}],"_links":{"self":[{"href":"https:\/\/shortbooks.online\/blog\/wp-json\/wp\/v2\/posts\/136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shortbooks.online\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shortbooks.online\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shortbooks.online\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shortbooks.online\/blog\/wp-json\/wp\/v2\/comments?post=136"}],"version-history":[{"count":1,"href":"https:\/\/shortbooks.online\/blog\/wp-json\/wp\/v2\/posts\/136\/revisions"}],"predecessor-version":[{"id":983,"href":"https:\/\/shortbooks.online\/blog\/wp-json\/wp\/v2\/posts\/136\/revisions\/983"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/shortbooks.online\/blog\/wp-json\/wp\/v2\/media\/127"}],"wp:attachment":[{"href":"https:\/\/shortbooks.online\/blog\/wp-json\/wp\/v2\/media?parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shortbooks.online\/blog\/wp-json\/wp\/v2\/categories?post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shortbooks.online\/blog\/wp-json\/wp\/v2\/tags?post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}