{"id":544,"date":"2018-09-24T08:00:44","date_gmt":"2018-09-24T14:00:44","guid":{"rendered":"https:\/\/practicalpowershell.com\/?p=544"},"modified":"2020-03-14T13:57:08","modified_gmt":"2020-03-14T19:57:08","slug":"block-coding-code-iterations","status":"publish","type":"post","link":"https:\/\/practicalpowershell.com\/block-coding-code-iterations\/","title":{"rendered":"Block Coding \/ Code Iterations"},"content":{"rendered":"<p>When building a complex script in PowerShell, remember to bite off only what you can chew successfully. This means that you probably can&#8217;t code an entire script one setting, so don&#8217;t. A complex script will take time, it needs to be coded in chunks and in many iterations. Why? Experience will tell you that mistakes will be made, compromises will occur, error correcting will be skipped. Let&#8217;s walk through a sample scenario.<BR><br \/>\n<u>Example<\/u><BR><br \/>\nYour IT Manager comes to you with a request to create a script that the help desk or someone else in the IT department can use to clean up certain emails in one or more mailboxes. The script needs to have a menu and separate items to search and report, delete emails and possible soft delete emails. This one lends to the process of compartmentalization. We can break down the script into parts for the menu and for each function that the script needs to perform.<BR><br \/>\n<em>Menu<\/em><br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\n$Menu = {<br \/>\nWrite-Host &quot; **********************&quot; -ForegroundColor Cyan<br \/>\nWrite-Host &quot; Mailbox Cleanup Script&quot; -ForegroundColor Cyan<br \/>\nWrite-Host &quot; **********************&quot; -ForegroundColor Cyan<br \/>\nWrite-Host &quot; &quot;<br \/>\nWrite-Host &quot; 1) Find Emails&quot;<br \/>\nWrite-Host &quot; 2) Report on emails found &#8211; notify IT Manager&quot;<br \/>\nWrite-Host &quot; 3) Soft delete emails&quot;<br \/>\nWrite-Host &quot; 4) Hard delete emails&quot;<br \/>\nWrite-host &quot; &quot;<br \/>\nWrite-Host &quot; 99) Exit&quot; -ForegroundColor Red<br \/>\nWrite-Host &quot; &quot;<br \/>\nWrite-Host &quot; Select an option.. [1-99]? &quot; -ForegroundColor White -NoNewLine<br \/>\n}<br \/>\n[\/sourcecode]<br \/>\n<u>Visual menu:<\/u><br \/>\n<a href=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Menu-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Menu-1.jpg?resize=430%2C216&#038;ssl=1\" alt=\"\" width=\"430\" height=\"216\" class=\"aligncenter size-full wp-image-545\" srcset=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Menu-1.jpg?w=430&amp;ssl=1 430w, https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Menu-1.jpg?resize=300%2C151&amp;ssl=1 300w\" sizes=\"auto, (max-width: 430px) 100vw, 430px\" \/><\/a><br \/>\nThat&#8217;s one block of self-contained code.<BR><br \/>\n<u>Calling Menu Options<\/u><BR><br \/>\nBelow shows a sample of a called function:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nFunction FindEmails {<br \/>\n    &lt;CODE BLOCK&gt;<br \/>\n} # End Function &#039;FindEmails&#039;<br \/>\n[\/sourcecode]<br \/>\nAfter coding it, this is when the concept of iterations come into place. First, get a basic working model. Can it find emails in a test mailbox? If so, then move on to adding error<br \/>\nhandling in case a mailbox cannot be contact or no emails can be found.<BR><br \/>\n<u>Sample Error Handling:<\/u><br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nTry {<br \/>\n    $Removal = Get-Mailbox $User | Search-Mailbox -SearchQuery $MessageQuery \u2013DeleteContent -ErrorAction STOP<br \/>\n} Catch {<br \/>\n    Write-host &#039;The attempt to remove the email message failed.&#039; -ForegroundColor Yellow<br \/>\n}<br \/>\n[\/sourcecode]<br \/>\nOnce that is good, add comments to the function. Finally run through more testing with test mailboxes and live data if at all possible to better gauge the code&#8217;s success.<br \/>\nRepeat this same process for each function needed in the script. Lastly, add a comment block at the top to give meaning to the script for yourself, your manager and the IT Department. We can do it like so:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\n#############################################<br \/>\n# SCRIPT DETAILS<br \/>\n# Good description of the script<br \/>\n#<br \/>\n# SCRIPT VERSION HISTORY<br \/>\n# Current Version : 1.0<br \/>\n# Change Log : 1.0 &#8211; First iteration<br \/>\n#<br \/>\n# OTHER SCRIPT INFORMATION<br \/>\n# Wish list : New Cool features<br \/>\n# Rights Required : Local admin on server<br \/>\n# Exchange Version : 2019<br \/>\n# Author : Damian Scoles<br \/>\n# My Blog : http:\/\/justaucguy.wordpress.com<br \/>\n# Disclaimer : You are on your own.<br \/>\n#<br \/>\n#. \\ScriptName.PS1<br \/>\n#############################################<br \/>\n[\/sourcecode]<br \/>\nA finished script should then be assembled to see if each function works as expected. Once testing confirms the script is successful, a demo or live data test should be performed with your IT manager and then put into production after it is approved.<BR><br \/>\nFollowing a process like this will make building scripts more production and hopefully less frustrating. Keep in mind that reusing code samples can speed up even a process like this. the more you code, the better you will get.<BR><br \/>\n<u>Pro Tip<\/u><br \/>\nCan&#8217;t get around a problem? Seek out someone with more PowerShell knowledge then you have.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When building a complex script in PowerShell, remember to bite off only what you can chew successfully. This means that you probably can&#8217;t code an entire script one setting, so don&#8217;t. A complex script will take time, it needs to be coded in chunks and in many iterations. Why? Experience will tell you that mistakes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rop_custom_images_group":[],"rop_custom_messages_group":[],"rop_publish_now":"initial","rop_publish_now_accounts":[],"rop_publish_now_history":[],"rop_publish_now_status":"pending","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-544","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Block Coding \/ Code Iterations - Practical PowerShell<\/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:\/\/practicalpowershell.com\/block-coding-code-iterations\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Block Coding \/ Code Iterations - Practical PowerShell\" \/>\n<meta property=\"og:description\" content=\"When building a complex script in PowerShell, remember to bite off only what you can chew successfully. This means that you probably can&#8217;t code an entire script one setting, so don&#8217;t. A complex script will take time, it needs to be coded in chunks and in many iterations. Why? Experience will tell you that mistakes [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/practicalpowershell.com\/block-coding-code-iterations\/\" \/>\n<meta property=\"og:site_name\" content=\"Practical PowerShell\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-24T14:00:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-14T19:57:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Menu-1.jpg\" \/>\n<meta name=\"author\" content=\"damian\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"damian\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/block-coding-code-iterations\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/block-coding-code-iterations\\\/\"},\"author\":{\"name\":\"damian\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"headline\":\"Block Coding \\\/ Code Iterations\",\"datePublished\":\"2018-09-24T14:00:44+00:00\",\"dateModified\":\"2020-03-14T19:57:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/block-coding-code-iterations\\\/\"},\"wordCount\":579,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"image\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/block-coding-code-iterations\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/Menu-1.jpg\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/practicalpowershell.com\\\/block-coding-code-iterations\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/block-coding-code-iterations\\\/\",\"url\":\"https:\\\/\\\/practicalpowershell.com\\\/block-coding-code-iterations\\\/\",\"name\":\"Block Coding \\\/ Code Iterations - Practical PowerShell\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/block-coding-code-iterations\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/block-coding-code-iterations\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/Menu-1.jpg\",\"datePublished\":\"2018-09-24T14:00:44+00:00\",\"dateModified\":\"2020-03-14T19:57:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/block-coding-code-iterations\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/practicalpowershell.com\\\/block-coding-code-iterations\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/block-coding-code-iterations\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/Menu-1.jpg?fit=430%2C216&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/Menu-1.jpg?fit=430%2C216&ssl=1\",\"width\":430,\"height\":216},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/block-coding-code-iterations\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/practicalpowershell.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Block Coding \\\/ Code Iterations\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#website\",\"url\":\"https:\\\/\\\/practicalpowershell.com\\\/\",\"name\":\"Practical PowerShell\",\"description\":\"PowerShell books written by experts\",\"publisher\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/practicalpowershell.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\",\"name\":\"damian\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"caption\":\"damian\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Block Coding \/ Code Iterations - Practical 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:\/\/practicalpowershell.com\/block-coding-code-iterations\/","og_locale":"en_US","og_type":"article","og_title":"Block Coding \/ Code Iterations - Practical PowerShell","og_description":"When building a complex script in PowerShell, remember to bite off only what you can chew successfully. This means that you probably can&#8217;t code an entire script one setting, so don&#8217;t. A complex script will take time, it needs to be coded in chunks and in many iterations. Why? Experience will tell you that mistakes [&hellip;]","og_url":"https:\/\/practicalpowershell.com\/block-coding-code-iterations\/","og_site_name":"Practical PowerShell","article_published_time":"2018-09-24T14:00:44+00:00","article_modified_time":"2020-03-14T19:57:08+00:00","og_image":[{"url":"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Menu-1.jpg","type":"","width":"","height":""}],"author":"damian","twitter_card":"summary_large_image","twitter_misc":{"Written by":"damian","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/practicalpowershell.com\/block-coding-code-iterations\/#article","isPartOf":{"@id":"https:\/\/practicalpowershell.com\/block-coding-code-iterations\/"},"author":{"name":"damian","@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"headline":"Block Coding \/ Code Iterations","datePublished":"2018-09-24T14:00:44+00:00","dateModified":"2020-03-14T19:57:08+00:00","mainEntityOfPage":{"@id":"https:\/\/practicalpowershell.com\/block-coding-code-iterations\/"},"wordCount":579,"commentCount":0,"publisher":{"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"image":{"@id":"https:\/\/practicalpowershell.com\/block-coding-code-iterations\/#primaryimage"},"thumbnailUrl":"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Menu-1.jpg","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/practicalpowershell.com\/block-coding-code-iterations\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/practicalpowershell.com\/block-coding-code-iterations\/","url":"https:\/\/practicalpowershell.com\/block-coding-code-iterations\/","name":"Block Coding \/ Code Iterations - Practical PowerShell","isPartOf":{"@id":"https:\/\/practicalpowershell.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/practicalpowershell.com\/block-coding-code-iterations\/#primaryimage"},"image":{"@id":"https:\/\/practicalpowershell.com\/block-coding-code-iterations\/#primaryimage"},"thumbnailUrl":"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Menu-1.jpg","datePublished":"2018-09-24T14:00:44+00:00","dateModified":"2020-03-14T19:57:08+00:00","breadcrumb":{"@id":"https:\/\/practicalpowershell.com\/block-coding-code-iterations\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/practicalpowershell.com\/block-coding-code-iterations\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/practicalpowershell.com\/block-coding-code-iterations\/#primaryimage","url":"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Menu-1.jpg?fit=430%2C216&ssl=1","contentUrl":"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2020\/03\/Menu-1.jpg?fit=430%2C216&ssl=1","width":430,"height":216},{"@type":"BreadcrumbList","@id":"https:\/\/practicalpowershell.com\/block-coding-code-iterations\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/practicalpowershell.com\/"},{"@type":"ListItem","position":2,"name":"Block Coding \/ Code Iterations"}]},{"@type":"WebSite","@id":"https:\/\/practicalpowershell.com\/#website","url":"https:\/\/practicalpowershell.com\/","name":"Practical PowerShell","description":"PowerShell books written by experts","publisher":{"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/practicalpowershell.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb","name":"damian","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","caption":"damian"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g"}}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/comments?post=544"}],"version-history":[{"count":2,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/544\/revisions"}],"predecessor-version":[{"id":547,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/544\/revisions\/547"}],"wp:attachment":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/media?parent=544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/categories?post=544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/tags?post=544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}