{"id":508,"date":"2021-06-29T09:37:03","date_gmt":"2021-06-29T09:37:03","guid":{"rendered":"https:\/\/yer.ac\/blog\/?p=508"},"modified":"2021-06-29T09:38:40","modified_gmt":"2021-06-29T09:38:40","slug":"consuming-a-json-file-for-automatic-params-in-powershell","status":"publish","type":"post","link":"https:\/\/yer.ac\/blog\/2021\/06\/29\/consuming-a-json-file-for-automatic-params-in-powershell\/","title":{"rendered":"Consuming a JSON file for automatic params in PowerShell"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">More of a note for future me&#8230;. Typically in PowerShell when there is a requirement for reading an external file to set parameters, I would read in a file using the <code><strong>Get-Content<\/strong><\/code> and converting it to JSON, perhaps something like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$json = Get-Content 'C:\\file.json' | Out-String | ConvertFrom-Json\r\n# To an object:\n$myobject= New-Object MyObject ($json.ItemA, $json.ItemB, $json.ItemC)\n# Individual mappings\n$ItemA = $json.ItemA\n$ItemB = $json.ItemA\n...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The problem with this is that if you have a particularly large or complex JSON file it could be problematic to map every field. Instead we can make use of the <code><strong>New-Variable<\/strong><\/code> command in a loop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$myJson= (Get-Content File.json | ConvertFrom-Json)\n$myJson.PSObject.Properties | ForEach-Object { \nNew-Variable -Name $_.Name -Value $_.Value -Force\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Which now means a file like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n \"PropertyA\": \"Hello, World\"\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Could be called like <code>$PropertyA<\/code>  within the script.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Of course, it may be best to include a prefix when calling <code>New-Variable<\/code> so properties have better naming conventions- for example $<em>param_PropertyA. <\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>More of a note for future me&#8230;. Typically in PowerShell when there is a requirement for reading an external file to set parameters, I would read in a file using the Get-Content and converting it to JSON, perhaps something like The problem with this is that if you have a particularly large or complex JSON &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_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},"jetpack_post_was_ever_published":false},"categories":[7,41],"tags":[10],"class_list":["post-508","post","type-post","status-publish","format-standard","hentry","category-devops","category-powershell","tag-powershell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Consuming a JSON file for automatic params in PowerShell - yer.ac | Adventures of a developer, and other things.<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/yer.ac\/blog\/2021\/06\/29\/consuming-a-json-file-for-automatic-params-in-powershell\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Consuming a JSON file for automatic params in PowerShell - yer.ac | Adventures of a developer, and other things.\" \/>\n<meta property=\"og:description\" content=\"More of a note for future me&#8230;. Typically in PowerShell when there is a requirement for reading an external file to set parameters, I would read in a file using the Get-Content and converting it to JSON, perhaps something like The problem with this is that if you have a particularly large or complex JSON &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/yer.ac\/blog\/2021\/06\/29\/consuming-a-json-file-for-automatic-params-in-powershell\/\" \/>\n<meta property=\"og:site_name\" content=\"yer.ac | Adventures of a developer, and other things.\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-29T09:37:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-29T09:38:40+00:00\" \/>\n<meta name=\"author\" content=\"yer.ac\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"yer.ac\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/yer.ac\\\/blog\\\/2021\\\/06\\\/29\\\/consuming-a-json-file-for-automatic-params-in-powershell\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/yer.ac\\\/blog\\\/2021\\\/06\\\/29\\\/consuming-a-json-file-for-automatic-params-in-powershell\\\/\"},\"author\":{\"name\":\"yer.ac\",\"@id\":\"https:\\\/\\\/yer.ac\\\/blog\\\/#\\\/schema\\\/person\\\/4638b9d868c7d3747bd3bb01fbc8153d\"},\"headline\":\"Consuming a JSON file for automatic params in PowerShell\",\"datePublished\":\"2021-06-29T09:37:03+00:00\",\"dateModified\":\"2021-06-29T09:38:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/yer.ac\\\/blog\\\/2021\\\/06\\\/29\\\/consuming-a-json-file-for-automatic-params-in-powershell\\\/\"},\"wordCount\":118,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/yer.ac\\\/blog\\\/#\\\/schema\\\/person\\\/4638b9d868c7d3747bd3bb01fbc8153d\"},\"keywords\":[\"PowerShell\"],\"articleSection\":[\"DevOps\",\"PowerShell\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/yer.ac\\\/blog\\\/2021\\\/06\\\/29\\\/consuming-a-json-file-for-automatic-params-in-powershell\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/yer.ac\\\/blog\\\/2021\\\/06\\\/29\\\/consuming-a-json-file-for-automatic-params-in-powershell\\\/\",\"url\":\"https:\\\/\\\/yer.ac\\\/blog\\\/2021\\\/06\\\/29\\\/consuming-a-json-file-for-automatic-params-in-powershell\\\/\",\"name\":\"Consuming a JSON file for automatic params in PowerShell - yer.ac | Adventures of a developer, and other things.\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/yer.ac\\\/blog\\\/#website\"},\"datePublished\":\"2021-06-29T09:37:03+00:00\",\"dateModified\":\"2021-06-29T09:38:40+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/yer.ac\\\/blog\\\/2021\\\/06\\\/29\\\/consuming-a-json-file-for-automatic-params-in-powershell\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/yer.ac\\\/blog\\\/2021\\\/06\\\/29\\\/consuming-a-json-file-for-automatic-params-in-powershell\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/yer.ac\\\/blog\\\/2021\\\/06\\\/29\\\/consuming-a-json-file-for-automatic-params-in-powershell\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/yer.ac\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Consuming a JSON file for automatic params in PowerShell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/yer.ac\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/yer.ac\\\/blog\\\/\",\"name\":\"yer.ac | Adventures of a developer, and other things.\",\"description\":\"Blog to keep track of things I am upto\",\"publisher\":{\"@id\":\"https:\\\/\\\/yer.ac\\\/blog\\\/#\\\/schema\\\/person\\\/4638b9d868c7d3747bd3bb01fbc8153d\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/yer.ac\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/yer.ac\\\/blog\\\/#\\\/schema\\\/person\\\/4638b9d868c7d3747bd3bb01fbc8153d\",\"name\":\"yer.ac\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/67ed010c9cc7986d40647e061c6dcdb06d818776591c7e954055adb629621113?s=96&d=retro&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/67ed010c9cc7986d40647e061c6dcdb06d818776591c7e954055adb629621113?s=96&d=retro&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/67ed010c9cc7986d40647e061c6dcdb06d818776591c7e954055adb629621113?s=96&d=retro&r=pg\",\"caption\":\"yer.ac\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/67ed010c9cc7986d40647e061c6dcdb06d818776591c7e954055adb629621113?s=96&d=retro&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Consuming a JSON file for automatic params in PowerShell - yer.ac | Adventures of a developer, and other things.","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:\/\/yer.ac\/blog\/2021\/06\/29\/consuming-a-json-file-for-automatic-params-in-powershell\/","og_locale":"en_US","og_type":"article","og_title":"Consuming a JSON file for automatic params in PowerShell - yer.ac | Adventures of a developer, and other things.","og_description":"More of a note for future me&#8230;. Typically in PowerShell when there is a requirement for reading an external file to set parameters, I would read in a file using the Get-Content and converting it to JSON, perhaps something like The problem with this is that if you have a particularly large or complex JSON &hellip;","og_url":"https:\/\/yer.ac\/blog\/2021\/06\/29\/consuming-a-json-file-for-automatic-params-in-powershell\/","og_site_name":"yer.ac | Adventures of a developer, and other things.","article_published_time":"2021-06-29T09:37:03+00:00","article_modified_time":"2021-06-29T09:38:40+00:00","author":"yer.ac","twitter_card":"summary_large_image","twitter_misc":{"Written by":"yer.ac","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/yer.ac\/blog\/2021\/06\/29\/consuming-a-json-file-for-automatic-params-in-powershell\/#article","isPartOf":{"@id":"https:\/\/yer.ac\/blog\/2021\/06\/29\/consuming-a-json-file-for-automatic-params-in-powershell\/"},"author":{"name":"yer.ac","@id":"https:\/\/yer.ac\/blog\/#\/schema\/person\/4638b9d868c7d3747bd3bb01fbc8153d"},"headline":"Consuming a JSON file for automatic params in PowerShell","datePublished":"2021-06-29T09:37:03+00:00","dateModified":"2021-06-29T09:38:40+00:00","mainEntityOfPage":{"@id":"https:\/\/yer.ac\/blog\/2021\/06\/29\/consuming-a-json-file-for-automatic-params-in-powershell\/"},"wordCount":118,"commentCount":0,"publisher":{"@id":"https:\/\/yer.ac\/blog\/#\/schema\/person\/4638b9d868c7d3747bd3bb01fbc8153d"},"keywords":["PowerShell"],"articleSection":["DevOps","PowerShell"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/yer.ac\/blog\/2021\/06\/29\/consuming-a-json-file-for-automatic-params-in-powershell\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/yer.ac\/blog\/2021\/06\/29\/consuming-a-json-file-for-automatic-params-in-powershell\/","url":"https:\/\/yer.ac\/blog\/2021\/06\/29\/consuming-a-json-file-for-automatic-params-in-powershell\/","name":"Consuming a JSON file for automatic params in PowerShell - yer.ac | Adventures of a developer, and other things.","isPartOf":{"@id":"https:\/\/yer.ac\/blog\/#website"},"datePublished":"2021-06-29T09:37:03+00:00","dateModified":"2021-06-29T09:38:40+00:00","breadcrumb":{"@id":"https:\/\/yer.ac\/blog\/2021\/06\/29\/consuming-a-json-file-for-automatic-params-in-powershell\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/yer.ac\/blog\/2021\/06\/29\/consuming-a-json-file-for-automatic-params-in-powershell\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/yer.ac\/blog\/2021\/06\/29\/consuming-a-json-file-for-automatic-params-in-powershell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/yer.ac\/blog\/"},{"@type":"ListItem","position":2,"name":"Consuming a JSON file for automatic params in PowerShell"}]},{"@type":"WebSite","@id":"https:\/\/yer.ac\/blog\/#website","url":"https:\/\/yer.ac\/blog\/","name":"yer.ac | Adventures of a developer, and other things.","description":"Blog to keep track of things I am upto","publisher":{"@id":"https:\/\/yer.ac\/blog\/#\/schema\/person\/4638b9d868c7d3747bd3bb01fbc8153d"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/yer.ac\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/yer.ac\/blog\/#\/schema\/person\/4638b9d868c7d3747bd3bb01fbc8153d","name":"yer.ac","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/67ed010c9cc7986d40647e061c6dcdb06d818776591c7e954055adb629621113?s=96&d=retro&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/67ed010c9cc7986d40647e061c6dcdb06d818776591c7e954055adb629621113?s=96&d=retro&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/67ed010c9cc7986d40647e061c6dcdb06d818776591c7e954055adb629621113?s=96&d=retro&r=pg","caption":"yer.ac"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/67ed010c9cc7986d40647e061c6dcdb06d818776591c7e954055adb629621113?s=96&d=retro&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paP5IW-8c","jetpack-related-posts":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/yer.ac\/blog\/wp-json\/wp\/v2\/posts\/508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yer.ac\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yer.ac\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yer.ac\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yer.ac\/blog\/wp-json\/wp\/v2\/comments?post=508"}],"version-history":[{"count":3,"href":"https:\/\/yer.ac\/blog\/wp-json\/wp\/v2\/posts\/508\/revisions"}],"predecessor-version":[{"id":512,"href":"https:\/\/yer.ac\/blog\/wp-json\/wp\/v2\/posts\/508\/revisions\/512"}],"wp:attachment":[{"href":"https:\/\/yer.ac\/blog\/wp-json\/wp\/v2\/media?parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yer.ac\/blog\/wp-json\/wp\/v2\/categories?post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yer.ac\/blog\/wp-json\/wp\/v2\/tags?post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}