{"id":11810,"date":"2018-10-20T14:43:59","date_gmt":"2018-10-20T12:43:59","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/"},"modified":"2024-09-11T10:46:14","modified_gmt":"2024-09-11T08:46:14","slug":"use-powershell-to-access-a-webservice","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/","title":{"rendered":"Use PowerShell To Access a WebService"},"content":{"rendered":"<p>Ten monthes ago, when I started to work for DBI at a customer site, I had to work on groups managing the server accesses. The information can be queried via a simple web site:<\/p>\n<ol>\n<li>enter a server and it provides a list of groups<\/li>\n<li>enter a group and it provides the list of servers in it<\/li>\n<\/ol>\n<p><!--more--><br \/>\nI initially thought I will parse the returned HTML. Then, with my favorite browser and its powerful developer mode, I saw that result is actually XML.<br \/>\nI first made a Perl script. This was quickly developed, but it was not the most convenient for the Windows integration and for graphical capabilities on my customer laptop. So, with advice from my colleague, I moved to <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/\" title=\"PowerShell\" target=\"_blank\" rel=\"noopener noreferrer\">PowerShell<\/a>.<br \/>\nThis script is now 70kbytes which contains lots of tricks and ease that make me liked PowerShell very much. I will share one important aspect in this blog.<\/p>\n<h3>WebService<\/h3>\n<p>First thing, I needed was to actually read data from the URL with the right format. For this, <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.utility\/invoke-webrequest?view=powershell-6\" title=\"Invoke-WebRequest\" target=\"_blank\" rel=\"noopener noreferrer\">Invoke-WebRequest<\/a> Cmdlet looked to be perfect for the job.<br \/>\nThe first issue encountered are the credentials, especially if it is possible for user to enter his credentials each time a call to the WebServervice is need. The parameter is &#8220;-UseDefaultCredentials&#8221;.<br \/>\nAs laptop is integrated in a Windows domain and Web service uses SSO (Single Sign On), parameter will automatically pass currently logged user credentials transparently.<br \/>\nLastly, &#8220;-Uri&#8221; is the most important parameter as it is the location of the service.<br \/>\nCommand will finally look like:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">$Response = Invoke-WebRequest -UseDefaultCredentials -Uri 'https:\/\/ServiceFQDN\/server.cgi'<\/pre>\n<p>$Response variable is of type HtmlWebResponseObject which compose of the following members:<\/p>\n<pre class=\"brush: powershell; highlight: [12,23]; title: ; notranslate\" title=\"\">   TypeName: Microsoft.PowerShell.Commands.HtmlWebResponseObject\n \nName              MemberType Definition\n----              ---------- ----------\nDispose           Method     void Dispose(), void IDisposable.Dispose()\nEquals            Method     bool Equals(System.Object obj)\nGetHashCode       Method     int GetHashCode()\nGetType           Method     type GetType()\nToString          Method     string ToString()\nAllElements       Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection AllElements {get;}\nBaseResponse      Property   System.Net.WebResponse BaseResponse {get;set;}\nContent           Property   string Content {get;}\nForms             Property   Microsoft.PowerShell.Commands.FormObjectCollection Forms {get;}\nHeaders           Property   System.Collections.Generic.Dictionary&#x5B;string,string] Headers {get;}\nImages            Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection Images {get;}\nInputFields       Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection InputFields {get;}\nLinks             Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection Links {get;}\nParsedHtml        Property   mshtml.IHTMLDocument2 ParsedHtml {get;}\nRawContent        Property   string RawContent {get;set;}\nRawContentLength  Property   long RawContentLength {get;}\nRawContentStream  Property   System.IO.MemoryStream RawContentStream {get;}\nScripts           Property   Microsoft.PowerShell.Commands.WebCmdletElementCollection Scripts {get;}\nStatusCode        Property   int StatusCode {get;}\nStatusDescription Property   string StatusDescription {get;}<\/pre>\n<p>The two interesting fields are:<\/p>\n<ol>\n<li>StatusCode which will contain the http status. I expect to be equal to 200<\/li>\n<li>Content which is the actual result of the service. In our case, it will be xml text.<\/li>\n<\/ol>\n<p>If WebService does not behave well, Invoke-WebRequest might raise a System.Net.WebException. To manage this, I used the typical Try\/Catch block.<\/p>\n<p>Final code block will look like:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Try {\n$Response = Invoke-WebRequest -UseDefaultCredentials -Uri 'https:\/\/ServiceFQDN\/server.cgi'\n} Catch &#x5B;System.Net.WebException] {\n$exception = $_.Exception\nWrite-Host $exception.Exception.GetType().FullName + ': ' + $exception.Exception.Message\n}<\/pre>\n<p>It is possible to get the HTTP status code and manage this differently depending on it. To access the value:<br \/>\n<code>$exception.Response.StatusCode<\/code><\/p>\n<h3>Going through content result<\/h3>\n<p>When request complete properly, I simply get the content and cast this to the xml type:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">&#x5B;xml]&#x5B;\/xml] $xmlContent = $Response.Content<\/pre>\n<p>To go through the data, we can use ForEach loop:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">ForEach ($element in $xmlContent.element) {\nWrite-Host 'Working on $element'\n(... do the needed ...)\n}<\/pre>\n<p>On another WebService, it is even possible to choose result in xml of json. &#8220;-contentType&#8221; parameter can be used to specify result format.<\/p>\n<h3>Conclusion<\/h3>\n<p>While browsing customer intranet, I found some other WebService to manage user. As it is part of my day to day activity to add users to multiple groups, scripting that will also make me and my team gains lots of time.<br \/>\nOn my next blogs on PowerShell, I will share what difficulties I found especially while moving from a simple script to a powerful tool with a graphical interface with many cool features:<\/p>\n<ol>\n<li>Create and automatically fill an email<\/li>\n<li>Create an Excel document and fill it<\/li>\n<li>&#8230;<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Ten monthes ago, when I started to work for DBI at a customer site, I had to work on groups managing the server accesses. The information can be queried via a simple web site: enter a server and it provides a list of groups enter a group and it provides the list of servers in [&hellip;]<\/p>\n","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[],"type_dbi":[],"class_list":["post-11810","post","type-post","status-publish","format-standard","hentry","category-application-integration-middleware"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Use PowerShell To Access a WebService - dbi Blog<\/title>\n<meta name=\"description\" content=\"powershell\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Use PowerShell To Access a WebService\" \/>\n<meta property=\"og:description\" content=\"powershell\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-10-20T12:43:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-11T08:46:14+00:00\" \/>\n<meta name=\"author\" content=\"Middleware Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Middleware Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/\"},\"author\":{\"name\":\"Middleware Team\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1\"},\"headline\":\"Use PowerShell To Access a WebService\",\"datePublished\":\"2018-10-20T12:43:59+00:00\",\"dateModified\":\"2024-09-11T08:46:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/\"},\"wordCount\":706,\"commentCount\":0,\"articleSection\":[\"Application integration &amp; Middleware\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/\",\"name\":\"Use PowerShell To Access a WebService - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"datePublished\":\"2018-10-20T12:43:59+00:00\",\"dateModified\":\"2024-09-11T08:46:14+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1\"},\"description\":\"powershell\",\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Use PowerShell To Access a WebService\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1\",\"name\":\"Middleware Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g\",\"caption\":\"Middleware Team\"},\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/middleware-team\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Use PowerShell To Access a WebService - dbi Blog","description":"powershell","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/","og_locale":"en_US","og_type":"article","og_title":"Use PowerShell To Access a WebService","og_description":"powershell","og_url":"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/","og_site_name":"dbi Blog","article_published_time":"2018-10-20T12:43:59+00:00","article_modified_time":"2024-09-11T08:46:14+00:00","author":"Middleware Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Middleware Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/"},"author":{"name":"Middleware Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1"},"headline":"Use PowerShell To Access a WebService","datePublished":"2018-10-20T12:43:59+00:00","dateModified":"2024-09-11T08:46:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/"},"wordCount":706,"commentCount":0,"articleSection":["Application integration &amp; Middleware"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/","url":"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/","name":"Use PowerShell To Access a WebService - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2018-10-20T12:43:59+00:00","dateModified":"2024-09-11T08:46:14+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1"},"description":"powershell","breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Use PowerShell To Access a WebService"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/8d8563acfc6e604cce6507f45bac0ea1","name":"Middleware Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ddcae7ba0f9d1a0e7ae707f0e689e4a9c95bb48ec49c8e6d9cc86d43f4121cb6?s=96&d=mm&r=g","caption":"Middleware Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/middleware-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/11810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=11810"}],"version-history":[{"count":1,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/11810\/revisions"}],"predecessor-version":[{"id":34726,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/11810\/revisions\/34726"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=11810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=11810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=11810"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=11810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}