{"id":796,"date":"2014-08-22T00:01:00","date_gmt":"2014-08-22T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/08\/22\/powershell-mini-scripting-games-2014-answer-5\/"},"modified":"2014-08-22T00:01:00","modified_gmt":"2014-08-22T00:01:00","slug":"powershell-mini-scripting-games-2014-answer-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-mini-scripting-games-2014-answer-5\/","title":{"rendered":"PowerShell Mini-Scripting Games 2014: Answer 5"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Here is the answer to Problem 5 in Windows PowerShell Mini-Scripting Games 2014.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here. It is time for the answer to Problem 5 in Mini-Scripting Games 2014. Last week, I posted <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2014\/08\/15\/powershell-mini-scripting-games-2014-problem-5.aspx\" target=\"_blank\">the complete Problem 5 description and task outline<\/a>.<\/p>\n<h2>Problem 5 synopsis<\/h2>\n<p>Your manager has tasked you to help a junior admin with a script. It appears that the junior admin wants to solicit input from the user when the script is running. Then based on the input from the user, the script will do something else.<\/p>\n<h2>Answer<\/h2>\n<p>There are many ways of obtaining input from the user. One way to do it is to create parameters for a Windows PowerShell function, and then when the function is called, the input is gleaned. You can also use VBScript style input boxes or other tools. But to me, the easiest way to do it is to use the <b>Read-Host<\/b> cmdlet.\nTo use the <b>Read-Host<\/b> cmdlet, all I do is specify a value for the <b>Prompt<\/b>, and store the results in a variable. I can then use that variable later.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> It can be dangerous to provide an unchecked input box, so it is a good idea to implement some checks for validity of the data that is input.\nIn this example, I ask for the name of a process, and I supply a couple of valid examples for the syntax. I store the inputted data in the <b>$a<\/b> variable.<\/p>\n<p style=\"margin-left:30px\">$a = Read-host -Prompt &#8220;Process to start: ex. notepad \/ calc&#8221;\nI now do a simple check to see if the inputted data matches <b>notepad<\/b> or <b>calc<\/b>. If it does, I pass the value of <b>$a<\/b> to the <b>Start-Process<\/b> cmdlet. Otherwise, I state that I cannot start the process. Here is the complete script:<\/p>\n<p style=\"margin-left:30px\">$a = Read-host -Prompt &#8220;Process to start: ex. notepad \/ calc&#8221;<\/p>\n<p style=\"margin-left:30px\">if($a -match &#8216;notepad&#8217; -or $a -match &#8216;calc&#8217;)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; { Start-Process $a }<\/p>\n<p style=\"margin-left:30px\">ELSE { &#8220;cannot start the $a process&#8221;}\n<b>&nbsp; &nbsp;Note<\/b>&nbsp; This is a very rudimentary example and a very basic check. It would be rather easy to work around this <br \/>&nbsp; &nbsp;type of check.\nThat is all there is to solving Problem 5. The answer to the final question will arrive in a few hours in the PowerTip, at which time Mini-Scripting Games Week will conclude. Join me tomorrow for more cool Windows PowerShell stuff.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Here is the answer to Problem 5 in Windows PowerShell Mini-Scripting Games 2014. Microsoft Scripting Guy, Ed Wilson, is here. It is time for the answer to Problem 5 in Mini-Scripting Games 2014. Last week, I posted the complete Problem 5 description and task outline. Problem 5 synopsis Your manager has tasked you to [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[191,262,521,3,4,45],"class_list":["post-796","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-community","tag-input","tag-mini-scripting-games","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Here is the answer to Problem 5 in Windows PowerShell Mini-Scripting Games 2014. Microsoft Scripting Guy, Ed Wilson, is here. It is time for the answer to Problem 5 in Mini-Scripting Games 2014. Last week, I posted the complete Problem 5 description and task outline. Problem 5 synopsis Your manager has tasked you to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=796"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/796\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}