{"id":67383,"date":"2006-05-05T09:57:00","date_gmt":"2006-05-05T09:57:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/05\/05\/how-can-i-force-users-to-type-command-line-arguments-in-a-specific-order\/"},"modified":"2006-05-05T09:57:00","modified_gmt":"2006-05-05T09:57:00","slug":"how-can-i-force-users-to-type-command-line-arguments-in-a-specific-order","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-force-users-to-type-command-line-arguments-in-a-specific-order\/","title":{"rendered":"How Can I Force Users to Type Command-line Arguments in a Specific Order?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" width=\"34\" height=\"34\" align=\"left\" border=\"0\" \/><\/p>\n<p>Hey, Scripting Guy! How can I force users to type command-line arguments in a specific order?<\/p>\n<p>&#8212; JA<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" alt=\"Spacer\" width=\"5\" height=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" width=\"34\" height=\"34\" align=\"left\" border=\"0\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" alt=\"Script Center\" width=\"120\" height=\"288\" align=\"right\" border=\"0\" \/><\/a><\/p>\n<p>Hey, JA. You know, the Scripting Guy who writes this column has a 16-year-old son. He coaches Colt League baseball. He works every day with all the other Scripting Guys. With that background, it probably come as no surprise to hear that he doesn&rsquo;t believe it&rsquo;s possible to get anyone to do <em>anything<\/em>, let alone get them to type command-line arguments in a specific order. So, sorry, but as far as we know there&rsquo;s no way to force users to type command-line arguments in a specific order (e.g., <strong>myscript.vbs atl-fs-01 alerter<\/strong>, where atl-fs-01 is the name of a computer and alerter is the name of a service). It just can&rsquo;t be done.<\/p>\n<p>But wait, don&rsquo;t go: the Scripting Guys never let anyone leave empty-handed. Let&rsquo;s think about what it is you <em>really<\/em> want to do here. We&rsquo;re guessing you don&rsquo;t really care whether the computer name is entered before the service name; you&rsquo;re just looking for a way to know which command-line argument is which. And that makes sense; after all, these two commands are going to produce very different results:<\/p>\n<pre class=\"codeSample\">myscript.vbs atl-ws-01 alerter\nmyscript.vbs alerter atl-ws-01\n<\/pre>\n<p>You&rsquo;d like to force users to type the computer name first, because then you can be assured that the first argument will always be the computer name and the second argument will always be the service name. We&rsquo;re assuming that the order doesn&rsquo;t really matter to you: you just want to be able to identify which argument represents the computer name and which argument represents the service name.<\/p>\n<p>If it&rsquo;s true that you don&rsquo;t really care about order you just care about being able to identify which argument is which, well, then here&rsquo;s your answer:<\/p>\n<pre class=\"codeSample\">If Wscript.Arguments.Named(\"s\") = \"\" Or Wscript.Arguments.Named(\"c\") = \"\" Then\n    Wscript.Echo \"You must specify both the service and the computer using syntax like this:\"\n    Wscript.Echo\n    Wscript.Echo \"myscript.vbs \/s:alerter \/c:atl-ws-01\"\n    Wscript.Quit\nEnd If\n\nWscript.Echo \"Service: \" &amp; Wscript.Arguments.Named(\"s\")\nWscript.Echo \"Computer: \" &amp; Wscript.Arguments.Named(\"c\")\n<\/pre>\n<p>What we&rsquo;re doing in this script is taking advantage of &ldquo;named arguments&rdquo; in Windows Script Host. We can&rsquo;t force users to type command-line arguments in a particular order; however, we <em>can<\/em> require users to specify arguments <em>by name<\/em>. In this script, we require people to include an argument named <strong>s<\/strong> and an argument named <strong>c<\/strong>. In other words, you need to start the script using one of the following statements:<\/p>\n<pre class=\"codeSample\">myscript.vbs \/s:alerter \/c:atl-ws-01\nmyscript.vbs \/c:atl-ws-01 \/s:alerter\n<\/pre>\n<p>If either (or both) of the two arguments are missing, the script will display some usage instructions and then quit. For example, suppose you try starting the script like this, with nary a \/s or a \/c in sight:<\/p>\n<pre class=\"codeSample\">myscript.vbs alerter atl-ws-01\n<\/pre>\n<p>Here&rsquo;s what you&rsquo;ll get back:<\/p>\n<pre class=\"codeSample\">You must specify both the service and the computer using syntax like this:\n\nmyscript.vbs \/s:alerter \/c:atl-ws-01\n<\/pre>\n<p>Is this foolproof? No; obviously a user could type \/s (for service) and then type the computer name, like so:<\/p>\n<pre class=\"codeSample\">\/s:atl-ws-01\n<\/pre>\n<p>We can&rsquo;t guard against that. But, at the very least, there will be no doubt as to which argument is <em>supposed<\/em> to represent the service name. And that&rsquo;s all we were hoping to do.<\/p>\n<p>So how does the script work? Well, the key is the very first line of code:<\/p>\n<pre class=\"codeSample\">If Wscript.Arguments.Named(\"s\") = \"\" Or Wscript.Arguments.Named(\"c\") = \"\" Then\n<\/pre>\n<p>As you can see, we&rsquo;re checking two things here: whether or not the named argument <strong>s<\/strong> is an empty string <em>or<\/em> whether or not the named argument <strong>c<\/strong> is an empty string. Suppose that argument s <em>is<\/em> equal to an empty string. That means one of two things: either you didn&rsquo;t include that particular argument when starting the script, or you didn&rsquo;t supply that argument with a value (for example, you started the script using the command <strong>myscript.vbs \/s: \/c:atl-fs-01<\/strong>). If no argument (or no value) can be found for either the named argument s or the named argument c then this block of code executes:<\/p>\n<pre class=\"codeSample\">Wscript.Echo \"You must specify both the service and the computer using syntax like this:\"\nWscript.Echo\nWscript.Echo \"myscript.vbs \/s:alerter \/c:atl-ws-01\"\nWscript.Quit\n<\/pre>\n<p>This code simply echoes back usage instructions and then uses the <strong>Quit<\/strong> method to terminate the script. In other words, if you don&rsquo;t supply the proper arguments the script won&rsquo;t run. Period.<\/p>\n<p>But suppose you <em>do<\/em> supply values for both arguments. In that case the script uses these two lines of code to echo back the values for the two arguments:<\/p>\n<pre class=\"codeSample\">Wscript.Echo \"Service: \" &amp; Wscript.Arguments.Named(\"s\")\nWscript.Echo \"Computer: \" &amp; Wscript.Arguments.Named(\"c\")\n<\/pre>\n<p>Of course, you&rsquo;d probably do something a bit more interesting with those values &#8211; like, say, connect to computer c and then stop service s. But that&rsquo;s up to you. The important thing is that we can require people to enter specific arguments, and we can identify which argument is which. And all with a minimal amount of code.<\/p>\n<p>A couple quick notes here. To begin with, argument names are purely arbitrary: we went with s and c because they&rsquo;re easy to type and easy to remember. However, we could have given these arguments any name we wanted; we would just need to modify the code accordingly. For example, if we wanted to use <strong>service<\/strong> and <strong>computer<\/strong> then the first line of our script needs to look like this:<\/p>\n<pre class=\"codeSample\">If Wscript.Arguments.Named(\"service\") = \"\" Or Wscript.Arguments.Named(\"computer\") = \"\" Then\n<\/pre>\n<p>Second, keep in mind that the name of the argument is a tad bit different from the syntax used when specifying that argument. Our arguments are named s and c. However, when we actually use those arguments we need to use this syntax: <strong><em>\/argument_name:argument_value<\/em><\/strong>. Don&rsquo;t type something like this; it won&rsquo;t work:<\/p>\n<pre class=\"codeSample\">myscript.vbs s alerter c atl-ws-01\n<\/pre>\n<p>Type this instead:<\/p>\n<pre class=\"codeSample\">myscript.vbs \/s:alerter \/c:atl-ws-01\n<\/pre>\n<p>One more thing: if an argument value includes a blank space make sure you enclose that value in double quote marks. For example, suppose you want to stop the service <strong>Automatic Updates<\/strong>. In that case, you need to type the following command:<\/p>\n<pre class=\"codeSample\">arguments.vbs \/s:\"Automatic Updates\" \/c:atl-fs-01\n<\/pre>\n<p>And, of course, order doesn&rsquo;t matter: if you&rsquo;d rather type the computer name and <em>then<\/em> the service name, well, hey, why not?<\/p>\n<p>Incidentally, if you&rsquo;re new to the whole idea of command-line arguments you might want to take a look at the <em>Tales from the Script<\/em> column <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee692833.aspx#EHAA\"><strong>Scripts Like a Good Argument<\/strong><\/a>. And, no, we can&rsquo;t <em>force<\/em> you to read that column. After all, we still can&rsquo;t get our rightfielder to throw the ball to the cutoff man, let alone make someone read an article about scripting and command-line arguments. But we&rsquo;re working on it.<\/p>\n<table id=\"EMG\" class=\"dataTable\" cellspacing=\"0\" cellpadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" valign=\"top\">\n<td style=\"border-right: #cccccc 1px solid\">\n<p class=\"lastInCell\"><strong>Note<\/strong>. Which <em>one<\/em> are we working on? Forcing people to read our articles. We&rsquo;ve given up on our rightfielder.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I force users to type command-line arguments in a specific order? &#8212; JA Hey, JA. You know, the Scripting Guy who writes this column has a 16-year-old son. He coaches Colt League baseball. He works every day with all the other Scripting Guys. With that background, it probably come as [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,5,77],"class_list":["post-67383","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-writing"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I force users to type command-line arguments in a specific order? &#8212; JA Hey, JA. You know, the Scripting Guy who writes this column has a 16-year-old son. He coaches Colt League baseball. He works every day with all the other Scripting Guys. With that background, it probably come as [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67383","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67383"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67383\/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=67383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}