{"id":65563,"date":"2007-02-07T21:41:00","date_gmt":"2007-02-07T21:41:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/02\/07\/hey-scripting-guy-how-can-i-prompt-a-user-for-information-and-then-pass-that-information-to-a-command-line-tool\/"},"modified":"2007-02-07T21:41:00","modified_gmt":"2007-02-07T21:41:00","slug":"hey-scripting-guy-how-can-i-prompt-a-user-for-information-and-then-pass-that-information-to-a-command-line-tool","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-prompt-a-user-for-information-and-then-pass-that-information-to-a-command-line-tool\/","title":{"rendered":"Hey, Scripting Guy! How Can I Prompt a User for Information and Then Pass That Information to a Command-Line Tool?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! How can I prompt a user for information and then pass that information to a command-line tool?<BR><BR>&#8212; SD<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, SD. efh;r9d\/fjwf<\/P>\n<P>Sorry; let\u2019s try that again. j ewewrj 2gr CX # 2kkkkw<BR><\/P>\n<P>Sorry; the Scripting Guy who writes this column is having a little difficulty moving his arms today. Last night the Scripting Son thought it would be \u201cfun\u201d to go to the gym and swim laps for 40 minutes instead of going to the gym and riding the exercise bike for 40 minutes.<\/P>\n<P>Did we ever mention how dumb the Scripting Son is?<\/P>\n<TABLE id=\"EAD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. OK, good point: if the Scripting Son is dumb for suggesting that the two swim laps for 40 minutes, what does that make the Scripting Dad for <I>agreeing<\/I> to that suggestion? Don\u2019t bother; we all know the answer to that question. Making things even worse is the fact that the Scripting Dad knows better. Back in his high school days, one of his PE teachers offered to give anyone an A for the quarter if they could complete a one-mile swim in a specified amount of time. The Scripting Guy who writes this column was able to complete the swim and earn his A. Several days later, when he finally managed to crawl out of the pool, he vowed never to do anything that dumb ever again.<\/P>\n<P>On the bright side, though, suppose the Scripting Dad finds himself on a boat that\u2019s a mile or so off shore and that boat suddenly sinks. After last night, the Scripting Dad knows that he\u2019ll be able to swim all the way back to land, no problem.<\/P>\n<P>Provided, of course, that the water has walls every 50 yards or so that he can rest on.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Of course, the question on everyone\u2019s mind right now is this: will a little thing like not being able to move his arms keep the Scripting Guy who writes this column from actually writing this column? Given his druthers, yes it would. Needless to say, however, the Scripting Guy who writes this column never seems to <I>get<\/I> his druthers:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)<\/PRE><PRE class=\"codeSample\">strName = InputBox(&#8220;Please enter the user name:&#8221;)<\/PRE><PRE class=\"codeSample\">If strName = &#8220;&#8221; Then    Wscript.Quit<\/PRE><PRE class=\"codeSample\">End If<\/PRE><PRE class=\"codeSample\">strCommand = &#8220;%comspec% \/k dsquery user -name &#8221; &amp; Chr(34) &amp; strName &amp; chr(34)strCommand = strCommand &amp; &#8221; | dsget user -tel&#8221;<\/PRE><PRE class=\"codeSample\">objShell.Run strCommand<\/PRE>\n<P>According to your email, SD, you noted that what you <I>really<\/I> need to do is prompt a user to enter a name, then use the command-line tools Dsquery and Dsget to search Active Directory, locate the user with that name, and then display his or her telephone number. Just to keep everyone else in the loop, here\u2019s the sample command SD sent, with the variable <I>x<\/I> representing the user name:<\/P><PRE class=\"codeSample\">dsquery user \u2013name x | dsget user \u2013tel<\/PRE>\n<P>The script we just showed you? That script will run this very command, substituting the name typed in by the user for the variable <I>x<\/I>.<\/P>\n<P>No, we don\u2019t have water on the brain. Well, not much anyway. Listen, maybe it will help if we explain how the script works. As you can see, we begin by creating an instance of the <B>Wscript.Shell<\/B> object; we\u2019ll use that object when it comes time to call our command-line tools. We then use this line of code to pop up an InputBox, solicit a user name, and then store that name in a variable named strName:<\/P><PRE class=\"codeSample\">strName = InputBox(&#8220;Please enter the user name:&#8221;)<\/PRE>\n<P>Our next step is optional, but seemed like a good idea. If you type a name into an InputBox and then click <B>OK<\/B> (or press ENTER) that name will get stored (in this example) in the variable strName. But what happens if you click the <B>Cancel<\/B> button instead? In that case, an empty string value (\u201c\u201d) gets stored in strName. On top of that, if you click <B>Cancel<\/B> that probably means you want to cancel the script. (Yes, we Scripting Guys <I>are<\/I> pretty darn smart, aren\u2019t we?) Therefore, we inserted this block of code to determine whether or not strName contains an empty string. If it does, we call the <B>Quit<\/B> method and terminate the script:<\/P><PRE class=\"codeSample\">If strName = &#8220;&#8221; Then    Wscript.QuitEnd If<\/PRE>\n<P>Hey, that\u2019s why they pay us the big bucks.<\/P>\n<P>We\u2019re now ready to compose our command-line string (and yes, we usually find it easiest to compose the command-line string, store it in a variable, and only <I>then<\/I> run the command). This isn\u2019t particularly difficult, but there are a couple of things to watch out for:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Just running the two command-line tools isn\u2019t good enough: the tools will run, but the output will scroll past too quickly to be read. Instead, we need to open a new command window, run the tools, then make sure that the command window stays open when the tools are finished. In our example, that\u2019s what %comspec% \/k does (%comspec% opens the command window and the \/k keeps the window open).<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>We have to include the double quote marks in the command. Admittedly, embedding double quotes in a string variable, as we\u2019re about to do, can be a little tricky. But we\u2019ll show you a pretty straightforward way to take care of that.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>In order to keep things simple we opted to use two steps in order put our command-line string together. Here\u2019s step 1:<\/P><PRE class=\"codeSample\">strCommand = &#8220;%comspec% \/k dsquery user -name &#8221; &amp; Chr(34) &amp; strName &amp; chr(34)<\/PRE>\n<P>What are we doing here? First we\u2019re opening a new window and ensuring that the window stays open after Dsquery and Dsget finish running; again, that\u2019s what the <B>%comspec% \/k<\/B> is for. We then issue the basic Dsquery command: <B>dsquery user \u2013name<\/B>. That brings us to the tricky part: specifying the user name, making sure that name is enclosed in double quote marks.<\/P>\n<P>You know, now that we look at the script, it turns out that this wasn\u2019t that tricky after all, was it? Notice that when we included the Dsquery command we put a blank space after the <B>\u2013name<\/B> parameter; that\u2019s gives us a ready-made spot for entering the user name. That\u2019s something we do using this reasonably-simple syntax: <B>Chr(34) &amp; strName &amp; Chr(34)<\/B>.<\/P>\n<P>What\u2019s all that mean? Well, for starters, the <B>Chr <\/B>function takes an ASCII value and substitutes the actual character corresponding to that value. As it turns out, the double quote mark has an ASCII value of 34; thus Chr(34) is going to insert a double quote mark for us.<\/P>\n<P>After that we insert the variable strName and a second double quote mark. Assuming you typed in <I>Ken Myer<\/I> as the user name, the variable strCommand will now be equal to this:<\/P><PRE class=\"codeSample\">%comspec% \/k dsquery user \u2013name &#8220;Ken Myer&#8221;<\/PRE>\n<P>Pretty good, huh?<\/P>\n<P>We then use this line of code to add in the second half of our command:<\/P><PRE class=\"codeSample\">strCommand = strCommand &amp; &#8221; | dsget user -tel&#8221;<\/PRE>\n<P>All we\u2019re doing here is setting the value of strCommand to the current value of strCommand plus this:<B> | dsget user \u2013tel<\/B>. In turn, that\u2019s going to make strCommand equal to this:<\/P><PRE class=\"codeSample\">%comspec% \/k dsquery user \u2013name &#8220;Ken Myer&#8221; | dsget user -tel<\/PRE>\n<P>Which, coincidentally enough, is the very command we need in order to get Dsquery and Dsget to retrieve Ken Myer\u2019s phone number.<\/P>\n<P>In fact, all that\u2019s left now is to call the <B>Run<\/B> method:<\/P><PRE class=\"codeSample\">objShell.Run strCommand<\/PRE>\n<P>We should point out that this script is simply going to call Dsquery and Dsget and then display Ken Myer\u2019s phone number in the command window. Is it possible to grab that phone number and then use it elsewhere in your script? As a matter of fact it is, but that requires you to do a little bit more coding (and to use the <B>Exec<\/B> method rather than the Run method). For more information, see the <I>Tales from the Script<\/I> column \u201c<A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/tales\/sg1002.mspx\"><B>Running Programs From WSH Scripts<\/B><\/A>,\u201d which, for you trivia buffs, happens to be the very first column published in the Script Center.<\/P>\n<P>And yes, it probably <I>is<\/I> worth a lot of money. Feel free to make us an offer.<\/P>\n<P>As for the Scripting Guy who writes this column, at this point all he can say is dsfhg3e^t r4.<\/P>\n<P>And this time, he means it.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I prompt a user for information and then pass that information to a command-line tool?&#8212; SD Hey, SD. efh;r9d\/fjwf Sorry; let\u2019s try that again. j ewewrj 2gr CX # 2kkkkw Sorry; the Scripting Guy who writes this column is having a little difficulty moving his arms today. Last night the [&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":[22,3,4,5],"class_list":["post-65563","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-retrieving-input","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I prompt a user for information and then pass that information to a command-line tool?&#8212; SD Hey, SD. efh;r9d\/fjwf Sorry; let\u2019s try that again. j ewewrj 2gr CX # 2kkkkw Sorry; the Scripting Guy who writes this column is having a little difficulty moving his arms today. Last night the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65563","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=65563"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65563\/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=65563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}