{"id":196,"date":"2021-11-26T20:53:23","date_gmt":"2021-11-26T19:53:23","guid":{"rendered":"https:\/\/www.lambrecht.de\/?p=196"},"modified":"2022-07-04T13:05:39","modified_gmt":"2022-07-04T11:05:39","slug":"splitting-a-string-the-weird-way","status":"publish","type":"post","link":"https:\/\/www.lambrecht.de\/?p=196","title":{"rendered":"Splitting a string the weird way"},"content":{"rendered":"\n<p>I love using JSON, as Powershell can really good handle it from scratch and you need it a lot if handling Rest-APIs.<br>So basically when I stumbled across an issue to seperate s specific string into values, I wondered if JSON could help me.<br>The string basically is looking like: &#8222;CLIENTNAME=aaaaaa&amp;CLIENTADDRESS=10.10.10.10&#8220; (the more versatile users under you might recognize the CS-URI-QUERY of Citrix Workspace App).<\/p>\n\n\n\n<p><strong>So whats the challenge?<\/strong><br>I want to parse this string and even if there is more information inside like &#8222;Blahblah=uusudfu&#8220;, I only want Clientname and Address information.<\/p>\n\n\n\n<p><strong>This is my solution with using JSON:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$inputvalue=\"CLIENTNAME=aaaaaa&amp;CLIENTADDRESS=10.10.10.10\"\n\n$Result=\"{`\"$($inputvalue -replace '=','\":\"' -replace '&amp;','\",\"')`\"}\" | ConvertFrom-Json\n$Result<\/code><\/pre>\n\n\n\n<p><strong>What is the output of this?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>IP          ClientName\n--          ----------\n10.10.10.10 aaaaaa<\/code><\/pre>\n\n\n\n<p>Here we have a very nice PS-Object.<\/p>\n\n\n\n<p><strong>Now, what did I do? <\/strong><br>I added to the string the brackets around it to make it a JSON and then replaced all delimiters by the approriate values. The Output looks like this after the replaces and the adding ot {} and before the ConvertFrom-Json:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"CLIENTNAME\":\"aaaaaa\",\"CLIENTADDRESS\":\"10.10.10.10\"}<\/code><\/pre>\n\n\n\n<p><strong>kind of weird way, but this code has for my usage a lot of advantages:<\/strong><\/p>\n\n\n\n<p>1. The value-pairs can be in any order, so &#8222;CLIENTNAME=aaaaaa&amp;CLIENTADDRESS=10.10.10.10&#8220; or &#8222;CLIENTADDRESS=10.10.10.10&amp;CLIENTNAME=aaaaaa&#8220;<br>delivers the same output<\/p>\n\n\n\n<p>2. Any additional values that are in the string will be dropped, eg:<br>&#8222;CLIENTNAME=aaaaaa&amp;CLIENTADDRESS=10.10.10.10&amp;USERNAME=jsdfhdjkfhjs&amp;TEST=hgjhggj&#8220;<\/p>\n\n\n\n<p>Whats very import: the data has to have the same look, as additional &#8222;&#8220; around a value will make this scriptblock break!<\/p>\n\n\n\n<p><strong>Update:<\/strong><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/twitter.com\/IISResetMe\" target=\"_blank\">Mathias R. Jessen<\/a> posted me an even better way on Twitter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$inputvalue=\"CLIENTNAME=aaaaaa&amp;CLIENTADDRESS=10.10.10.10\"\n\n$Result=$inputvalue -replace '&amp;',\"`n\" | ConvertFrom-StringData\n$Result<\/code><\/pre>\n\n\n\n<p>This is now really a very short code for doing this job. Thanks Mathias, this is how the powershell community works together!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I love using JSON, as Powershell can really good handle it from scratch and you need it a lot if handling Rest-APIs.So basically when I stumbled across an issue to seperate s specific string into values, I wondered if JSON &hellip; <a href=\"https:\/\/www.lambrecht.de\/?p=196\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[21,27],"class_list":["post-196","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-powershell","tag-scripting"],"_links":{"self":[{"href":"https:\/\/www.lambrecht.de\/index.php?rest_route=\/wp\/v2\/posts\/196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lambrecht.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lambrecht.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lambrecht.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lambrecht.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=196"}],"version-history":[{"count":0,"href":"https:\/\/www.lambrecht.de\/index.php?rest_route=\/wp\/v2\/posts\/196\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.lambrecht.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lambrecht.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lambrecht.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}