{"id":1056,"date":"2021-04-08T11:33:33","date_gmt":"2021-04-08T10:33:33","guid":{"rendered":"http:\/\/www.vatland.no\/?p=1056"},"modified":"2021-04-11T06:39:24","modified_gmt":"2021-04-11T05:39:24","slug":"ps-2-x-convertfrom-json","status":"publish","type":"post","link":"https:\/\/www.vatland.no\/index.php\/ps-2-x-convertfrom-json\/","title":{"rendered":"PS 2.x convertfrom-json"},"content":{"rendered":"\n<p>Powershell 2 does not have a convertfrom-json function and I like to use json files for storing configuration. Searching the web gives almost zero results. Converting a multilevel json structure to hashtable would be very complicated. Since my config files are only one level the easiest way of doing it was using regex.  Input is json structure and output is an hashtable.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nfunction convertfrom-json-onelevel {\n    Param (\n        &#x5B;string&#x5B;]] $json\n    )\n\n    $hashtable = @{ }\n    $t = $json | Select-String -Pattern &#039;(&#x5B;&quot;])(?:(?=(\\\\?))\\2.)*?\\1&#039; -AllMatches\n    $hashtable = @{ }\n    (0..((($t.matches).count - 1) \/ 2)) | % {\n        $key = &#x5B;regex]::Unescape($t.Matches&#x5B;$_ * 2].Value)\n        $key = $key.TrimEnd(&#039;&quot;&#039;)\n        $key = $key.TrimStart(&#039;&quot;&#039;)   \n        $value = &#x5B;regex]::Unescape($t.Matches&#x5B;$_ * 2 + 1].Value)\n        $value = $value.TrimEnd(&#039;&quot;&#039;)\n        $value = $value.TrimStart(&#039;&quot;&#039;)   \n        $hashtable.add($key, $value)\n    }\n    return $hashtable\n}\n<\/pre><\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Powershell 2 does not have a convertfrom-json function and I like to use json files for storing configuration. Searching the web gives almost zero results. Converting a multilevel json structure to hashtable would be very complicated. Since my config files are only one level the easiest way of doing it was using regex. Input is &hellip; <a href=\"https:\/\/www.vatland.no\/index.php\/ps-2-x-convertfrom-json\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PS 2.x convertfrom-json<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2,48],"tags":[99,97],"class_list":["post-1056","post","type-post","status-publish","format-standard","hentry","category-development","category-powershell","tag-convertfrom-json","tag-json"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":false,"jetpack-related-posts":[{"id":968,"url":"https:\/\/www.vatland.no\/index.php\/powershell-2-0-json-to-hash\/","url_meta":{"origin":1056,"position":0},"title":"Powershell 2.0 Json to Hash","author":"Atle","date":"November 11, 2020","format":false,"excerpt":"Resonse from webrequests are normally in Json format and converting back and forth is no problem using convertto\/from-json. Only this time some computers did run Powershell 2.0 and did not have any \"new\" .NET framework installed. So I ran to a halt at this Json converting. Since my data did\u2026","rel":"","context":"In &quot;Development&quot;","block_context":{"text":"Development","link":"https:\/\/www.vatland.no\/index.php\/category\/development\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":957,"url":"https:\/\/www.vatland.no\/index.php\/hash-tables-in-powershell\/","url_meta":{"origin":1056,"position":1},"title":"Hash tables in powershell","author":"Atle","date":"March 23, 2020","format":false,"excerpt":"We all have the need to store data in some kind of arrays. I use hashtables a lot. Preferred use is as a lookup table, I can use 'contains' instead of looping through each item or reference an object by name instead of index number. Lookup table for licenses is\u2026","rel":"","context":"In &quot;CSP&quot;","block_context":{"text":"CSP","link":"https:\/\/www.vatland.no\/index.php\/category\/csp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":934,"url":"https:\/\/www.vatland.no\/index.php\/simple-powershell-mru-list\/","url_meta":{"origin":1056,"position":2},"title":"Simple Powershell MRU list","author":"Atle","date":"December 23, 2019","format":false,"excerpt":"When using using my secret server powershell functions I got tired of constantly searching for secret ID's. I had to do a new search just because I could not remember the ID's. So I added some kind of MRU to my get-secretID function. This code block creates to classes mruitem\u2026","rel":"","context":"In &quot;Powershell&quot;","block_context":{"text":"Powershell","link":"https:\/\/www.vatland.no\/index.php\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":804,"url":"https:\/\/www.vatland.no\/index.php\/use-powershell-to-get-leakedcredentials-from-azure-using-graph\/","url_meta":{"origin":1056,"position":3},"title":"Use Powershell to get LeakedCredentials from Azure using Graph","author":"Atle","date":"November 1, 2018","format":false,"excerpt":"Leaked credentials listed from Azure using powershell and Microsoft Graph\u00a0We need one Azure AD Premium X license to get this log. Would it be nice to list all leakedcredentials using powershell?(or riskysignins or identiyriskevents). All of this could be achieved using powershell and REST api at Microsoft Graph. I have\u2026","rel":"","context":"In \"Powershell\"","block_context":{"text":"Powershell","link":"https:\/\/www.vatland.no\/index.php\/tag\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.vatland.no\/wp-content\/uploads\/2018\/11\/Leaked.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.vatland.no\/wp-content\/uploads\/2018\/11\/Leaked.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.vatland.no\/wp-content\/uploads\/2018\/11\/Leaked.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":950,"url":"https:\/\/www.vatland.no\/index.php\/secretservertycotic-plugin-for-powershell-secretsmanagement-module\/","url_meta":{"origin":1056,"position":4},"title":"SecretServer(Thycotic) plugin for powershell SecretsManagement module.","author":"Atle","date":"February 18, 2020","format":false,"excerpt":"With the new powershell module SecretsManagement it is possible to add plugins. The new module is used to get\/add\/remove credentials. I created a plugin for secretserver to this module. To get started you will have to install secretsmanagement: Install-Module -Name Microsoft.Powershell.SecretsManagement -AllowPrerelease If -AllowPrerelase is not an option you would\u2026","rel":"","context":"In &quot;Powershell&quot;","block_context":{"text":"Powershell","link":"https:\/\/www.vatland.no\/index.php\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":911,"url":"https:\/\/www.vatland.no\/index.php\/use-powershell-to-get-external-ip-address\/","url_meta":{"origin":1056,"position":5},"title":"Use powershell to get external IP address","author":"Atle","date":"September 23, 2019","format":false,"excerpt":"How can you get your external IP address from powershell? I use a simple script to query an external public web service. The service I'm using is hosted by ipinfo.io . I have created a small function that is placed in my powershell library. All my modules are loaded by\u2026","rel":"","context":"In &quot;Development&quot;","block_context":{"text":"Development","link":"https:\/\/www.vatland.no\/index.php\/category\/development\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/posts\/1056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/comments?post=1056"}],"version-history":[{"count":6,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/posts\/1056\/revisions"}],"predecessor-version":[{"id":1066,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/posts\/1056\/revisions\/1066"}],"wp:attachment":[{"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/media?parent=1056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/categories?post=1056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/tags?post=1056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}