{"id":63703,"date":"2007-10-30T00:06:00","date_gmt":"2007-10-30T00:06:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/10\/30\/hey-scripting-guy-how-can-i-start-a-remote-process-under-cscript-if-the-script-path-contains-blank-spaces\/"},"modified":"2007-10-30T00:06:00","modified_gmt":"2007-10-30T00:06:00","slug":"hey-scripting-guy-how-can-i-start-a-remote-process-under-cscript-if-the-script-path-contains-blank-spaces","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-start-a-remote-process-under-cscript-if-the-script-path-contains-blank-spaces\/","title":{"rendered":"Hey, Scripting Guy! How Can I Start a Remote Process Under CScript If the Script Path Contains Blank Spaces?"},"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! I\u2019m trying to create a process that runs a script under the CScript script host. However, I keep running into problems because there are spaces in the script path name. How can I use WMI\u2019s Win32_Process class to run a script under CScript when there are spaces in the script path name?<BR><BR>&#8212; TA<\/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, TA. ZDjhdvS ASBF VX<\/P>\n<P>Oh. Sorry; it\u2019s hard to get used to typing while wearing this heavy coat and these ski gloves. But we\u2019re going to have to get used to it: like it or not, winter is upon us. Granted, it\u2019s not even November yet, but this morning the Scripting Guy who writes this column had to use an ice scraper to scrape the frost off his car windows. That\u2019s a bad sign; after all, he rarely has to scrape frost off his car windows when the temperature is in the 80s. As it was, as he headed off to work the temperature outside was just 35 degrees Fahrenheit. That\u2019s also a bad sign: when you\u2019re dealing with Fahrenheit you never want to see a temperature that starts with a 3.<\/P>\n<P>Ever.<\/P>\n<P>So, anyway asdjSdjkSY ABNS ASDNB SA DDDPPpppS<\/P>\n<P>Sorry. So, anyway, the Scripting Guy who writes this column is bundled up and in a bad mood; after all, this past year the Seattle area had a so-so Spring, a miserable Summer, and a Fall that lasted about 3 days, total. And now we\u2019re right back into winter which, last year, meant windstorms, ice storms, power failures, and all those other winter pastimes that never make it onto calendars or greeting cards. <\/P>\n<P>Gee, and we were afraid that winter would <I>never<\/I> get here.<\/P>\n<P>As you probably know, neither snow nor rain not heat nor gloom of night can keep the US postal service from the swift completion of its appointed rounds. So does that mean that the Scripting Guys won\u2019t be deterred by a little cold weather? Heck no; in fact, the Scripting Guy who writes this column had every intention of going back home, crawling under the covers, and staying there till Spring. And he probably would have, too, except for his unwavering devotion to duty and to his readers.<\/P>\n<P>Plus the fact that the Scripting Son starts college next year, which means this is not a good time for the Scripting Dad to be quitting his job.<\/P>\n<P>With that in mind, TA, here\u2019s a script that can start a second script using the Win32_Process class, even if the path name to that second script includes blank spaces:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objProcess = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2:Win32_Process&#8221;)<\/p>\n<p>strCommand = &#8220;cscript &#8221; &amp; Chr(34) &amp; &#8220;C:\\Documents and Settings\\Scripts\\QA.vbs&#8221; &amp; Chr(34)\nobjProcess.Create strCommand,null,null,intProcessID\n<\/PRE>\n<P>As you can see (assuming that you, unlike those of us in the Seattle area, don\u2019t have to scrape the ice off your monitor) we start off by connecting to the WMI service on the local computer. Actually, to be a little more technical \u2013 and a little more accurate \u2013 we connect to the WMI class <B>Win32_Process<\/B> on the local computer; note that we specified Win32_Process in our WMI path:<\/P>\n<P>\\root\\cimv2:<B>Win32_Process<\/B><\/P>\n<P>Why did we do that? Because the <B>Create<\/B> method (the method used create a new process) is a so-called \u201cstatic\u201d method; any time you use a static method you need to bind to the WMI class rather than the individual instances of the class. You might have noticed that we don\u2019t have an ExecQuery method that selects instances of the Win32_Process class. That\u2019s why: we don\u2019t <I>want<\/I> instances of the Win32_Process class.<\/P>\n<P>And yes, you <I>can<\/I> run this script against a remote computer; to do that, simply assign the name of the remote computer to the variable strComputer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ws-01&#8221;\n<\/PRE>\n<P>Keep in mind, however, that the script will run in an invisible window on that remote machine; nothing will ever appear onscreen. That\u2019s probably why TA is explicitly running the script under CScript: by doing so, if the script includes any Echo commands, those commands will be written to the invisible console window and will <I>not<\/I> pop up as message boxes. And that\u2019s good; after all, those message boxes will also be invisible. If an invisible message box <I>did<\/I> pop up while your script was running, the script would pause until someone dismissed that message box. Needless to say, invisible message boxes are a bit difficult to dismiss.<\/P>\n<TABLE id=\"EHE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Actually, they\u2019re <I>impossible<\/I> to dismiss. The only way to get rid of them would be to terminate the script process, which would also terminate the script. If you\u2019re going to use code like this to start a script on a remote machine make sure that you <I>always<\/I> run that script under CScript.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As it turns out, TA has been having trouble with this line of code:<\/P><PRE class=\"codeSample\">objWMIService.Create(&#8220;Cscript C:\\Documents and Settings\\Scripts\\QA.vbs&#8221;,null,null,intProcessID)\n<\/PRE>\n<P>TA also correctly identified the problem: it\u2019s the blank spaces that appear in the path name. When you call the Create method, you need to enter a command that looks exactly like the command you would type from the command prompt. How would you enter this command from the command prompt? Well, because of the blank spaces, you\u2019d enclose the file path in double quote marks, like so:<\/P><PRE class=\"codeSample\">Cscript &#8220;C:\\Documents and Settings\\Scripts\\QA.vbs&#8221;\n<\/PRE>\n<P>But we can\u2019t do that here. If we enclose the command in double quotes we\u2019ll have double quotes inside of double quotes, and all \u2026 heck \u2026 will break loose:<\/P><PRE class=\"codeSample\">objWMIService.Create &#8220;Cscript &#8220;C:\\Documents and Settings\\Scripts\\QA.vbs&#8221;&#8221;,null,null,intProcessID\n<\/PRE>\n<P>So how are we going to deal with the issue of double double quotes in our Create command? We\u2019re not; instead, we\u2019re going to use this little trick to bypass the double quote problem altogether:<\/P><PRE class=\"codeSample\">strCommand = &#8220;cscript &#8221; &amp; Chr(34) &amp; &#8220;C:\\Documents and Settings\\Scripts\\QA.vbs&#8221; &amp; Chr(34)\n<\/PRE>\n<P>What we\u2019re doing here is simply constructing a string value equivalent to the command we would type at the command prompt; we do that by combining the following values:<\/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><B>Cscript<\/B> (Note the blank space immediately following the letter t.)<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>Chr(34)<\/B>. What\u2019s Chr(34)? Well, the Chr function takes an ASCII value and converts it to a character. Guess which character the ASCII value 34 converts into? That\u2019s right: the double quote mark.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>C:\\Documents and Settings\\Scripts\\QA.vbs<\/B><\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>Chr(34)<\/B><\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Or, a little more visually:<\/P><PRE class=\"codeSample\">Cscript \n+          &#8221;\n+           C:\\Documents and Settings\\Scripts\\QA.vbs\n+                                                   &#8221;\n_____________________________________________________<\/p>\n<p>   Cscript &#8220;C:\\Documents and Settings\\Scripts\\QA.vbs&#8221;\n<\/PRE>\n<P>Now we don\u2019t have to worry about double double quotes; all our double quotes are safely tucked away inside the variable strCommand. In turn, that means we can create a new process \u2013 and run the script \u2013 simply by using strCommand as the first parameter passed to the Create method:<\/P><PRE class=\"codeSample\">objProcess.Create strCommand,null,null,intProcessID\n<\/PRE>\n<P>And that should do it<\/P>\n<P>Incidentally, would you like to know how cold it <I>really<\/I> got here in Seattle? Well, it was so cold that the Scripting Guy who writes this column actually had his brain freeze. When this Scripting Guy sat down to tackle this question, he copied TA\u2019s original script, saving it to a file named C:\\Scripts\\Test Scripts\\Test.vbs. Of course, that meant he also had to change path to the script being called by Test.vbs as well. So what did he change that to? Sadly, you\u2019re right: he changed the path to C:\\Scripts\\Test Scripts\\Test.vbs. That meant he had a script \u2013 Test.vbs \u2013 that had only one mission in life: to open and run a script called Test.vbs.<\/P>\n<P>Was that a problem? Well, only if you think that having hundreds of command windows suddenly pop up on your screen is a problem. When he started Test.vbs it immediately, and dutifully, started a second copy of Test.vbs. That second copy promptly started a third copy of Test.vbs, which promptly started a fourth copy of Test.vbs, which \u2013 well, you get the idea. All these command windows were popping up so quickly that he couldn\u2019t deal with them; he couldn\u2019t even stop the process because each Cscript.exe process would pop up and then disappear before he could kill it; meanwhile, 5 or 6 more processes would pop up along the way. And because the processes were created faster than they could be terminated, his screen was soon littered with empty command windows.<\/P>\n<P>In fact, he couldn\u2019t even shut down any of the other applications he was running because each time he clicked somewhere (anywhere) a new command window would pop up and steal the focus away. All he could do was shut down the computer.<\/P>\n<P>Trust us: it was <I>way<\/I> worse than it sounds. And it doesn\u2019t really sound all that good, to begin with.<\/P>\n<P>And then \u2013 alas \u2013 <I>he did the exact same thing all over again!<\/I> After glancing at the code, the Scripting Guy who writes this column assumed that he\u2019d been the victim of an unfortunate computer glitch; after all, Test.vbs didn\u2019t include For Next loops or Do While loops or anything else that might cause multiple command windows to appear on screen. <I>And so he ran the script again<\/I>. It was only while watching the command windows re-proliferate that he realized what he had done. (And, after restarting the computer <I>again<\/I>, finally managed to fix it.)<\/P>\n<P>But don\u2019t worry: as soon as spring comes and his brain unfreezes everything should be fine.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019m trying to create a process that runs a script under the CScript script host. However, I keep running into problems because there are spaces in the script path name. How can I use WMI\u2019s Win32_Process class to run a script under CScript when there are spaces in the script path name?&#8212; [&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":[2,3,4,5],"class_list":["post-63703","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I\u2019m trying to create a process that runs a script under the CScript script host. However, I keep running into problems because there are spaces in the script path name. How can I use WMI\u2019s Win32_Process class to run a script under CScript when there are spaces in the script path name?&#8212; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63703","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=63703"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63703\/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=63703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}