{"id":67433,"date":"2006-04-28T17:18:00","date_gmt":"2006-04-28T17:18:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/04\/28\/how-can-i-use-the-runas-command-to-run-a-script-under-alternate-user-credentials\/"},"modified":"2006-04-28T17:18:00","modified_gmt":"2006-04-28T17:18:00","slug":"how-can-i-use-the-runas-command-to-run-a-script-under-alternate-user-credentials","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-use-the-runas-command-to-run-a-script-under-alternate-user-credentials\/","title":{"rendered":"How Can I Use the RunAs Command to Run a Script Under Alternate User Credentials?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I use the RunAs command to run a script under alternate user credentials?<BR><BR>&#8212; BE<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, BE. You know, in the business world one of the hot topics these days is synergy. For example, after <I>The Mighty Ducks<\/I> became a hit movie the Disney Company went out and bought themselves an NHL hockey team, renaming it the Anaheim Mighty Ducks. Coincidence? No, <I>synergy<\/I>, defined as \u201c\u2026the phenomenon in which two or more discrete influences or agents acting together create an effect greater than the sum of the effects each is able to create independently.\u201d If there\u2019s something more exciting than two or more discrete influences or agents acting together to create an effect greater than the sum of the effects each is able to create independently, well, we\u2019d like to see it.<\/P>\n<P>Synergy is important in the scripting world as well. For example, today we published the answer to a <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/puzzle\/answers\/apr2106.mspx\"><B>scripting puzzle<\/B><\/A> that dealt with the issue of trying to run scripts under alternate credentials. In our answer we discussed the problems involved in getting WMI to run under alternate credentials when the script needs to run against the local computer. Our suggestion: read today\u2019s <I>Hey, Scripting Guy!<\/I> column and learn how to run a script using the RunAs command.<\/P>\n<P>Coincidence? No, laziness: we actually received this question several months ago, and we\u2019re only just now getting around to answering it. But chalking this up to synergy sounded much better than chalking it up to lethargy!<\/P>\n<P>As we noted in the weekly puzzle, you can use code similar to this snippet to run a script under alternate user credentials, provided that the script is running against a remote computer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;\nstrNamespace = &#8220;root\\cimv2&#8221;\nstrUser = &#8220;kenmyer&#8221;\nstrPassword = &#8220;password&#8221;<\/p>\n<p>Set objWbemLocator = CreateObject(&#8220;WbemScripting.SWbemLocator&#8221;)\nSet objWMIService = objwbemLocator.ConnectServer _\n    (strComputer, strNamespace, strUser, strPassword)\n<\/PRE>\n<P>Unfortunately, though, this script will fail if you try to run it against the local computer. That\u2019s because the ConnectServer method does not allow you to pass user credentials (like a user name and password) to the local machine. And, no, to be honest we\u2019re not sure <I>why<\/I> it won\u2019t let you pass user credentials to the local machine; it just won\u2019t. <\/P>\n<TABLE class=\"dataTable\" id=\"EYD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Think of ConnectServer as being like a three-year-old child. Nobody knows why three-year-olds will eat only boxed macaroni-and-cheese; they just won\u2019t eat anything else, period. And there\u2019s not much you can do about that except try to live with the situation, whether that means making boxed macaroni-and-cheese for dinner every night, or not passing user credentials any time ConnectServer runs against the local computer.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So is this really a problem? Well, it can be. After all, Microsoft recommends that &#8211; for security reasons &#8211; you never log on to a computer using an Administrator account. Instead, we suggest that you log on using a regular old user account, then use a utility such as RunAs any time you need to run a program as an Administrator. And so, seeing as how everyone always does whatever Microsoft tells you to do, you type the following command at the command prompt and try using the RunAs utility to run the script C:\\Scripts\\Test.vbs:<\/P><PRE class=\"codeSample\">runas \/profile \/user:fabrikam\\kenmyer &#8220;C:\\Scripts\\Test.vbs&#8221;\n<\/PRE>\n<P>And here\u2019s what happens when you press ENTER:<\/P><PRE class=\"codeSample\">Attempting to start C:\\Scripts\\Test.vbs as user &#8220;fabrikam\\kenmyer&#8221; &#8230;\nRUNAS ERROR: Unable to run &#8211; C:\\Scripts\\Test.vbs\n193: C:\\Scripts\\Test.vbs is not a valid Win32 application.\n<\/PRE>\n<P>It\u2019s at this point that people typically do one of two things: either they give up, or they start debugging the script, trying to figure out why their code isn\u2019t valid. But here\u2019s the deal: the error message says nothing about the script or the script code being invalid, it simply states that Test.vbs is not a valid Win32 application. That\u2019s because RunAs is expecting to run an executable file. Test.vbs is obviously not an executable file, so the command fails. Most likely your code is just fine; the problem is that you\u2019re trying to run a script rather than a .exe file.<\/P>\n<P>So if RunAs accepts only executable files then obviously we <I>can\u2019t<\/I> run a script using RunAs, right? Well, not necessarily. Although Test.vbs might not be an executable file, the two Windows script hosts &#8211; Cscript.exe and Wscript.exe &#8211; <I>are<\/I> executable files. The secret to running a script under RunAs is to call one of the two script hosts, passing along the name of the script as a command-line argument. For example, this RunAs command will fire up Cscript.exe and then run the script C:\\Scripts\\Test.vbs:<\/P><PRE class=\"codeSample\">runas \/profile \/user:fabrikam\\kenmyer &#8220;cscript.exe C:\\Scripts\\Test.vbs&#8221;\n<\/PRE>\n<P>You can see how easy that is. You simply call the RunAs command along with the following parameters:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>\/profile<\/B>, which causes the appropriate user profile to be loaded. This is optional, but makes it more likely RunAs will be able to do what it needs to do.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>\/user:fabrikam\\kenmyer<\/B>, which is the user account (in the form <I>domain\\user_name<\/I>) under which the process is to run.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>\u201ccscript.exe C:\\Scripts\\Test\\.vbs\u201d<\/B>, which is the process you want RunAs to execute. Note that the entire command must be enclosed in double quote marks, and should be typed the same way you would type the command from the command prompt.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>That, by the way, is the second problem people encounter when trying to run a script under RunAs. As long as your scripts are in the folder C:\\Scripts you won\u2019t have any problems; that\u2019s because C:\\Scripts has no spaces in the path name. But suppose your scripts are in C:\\Documents and Settings\\kenmyer\\Scripts. If you were at the command prompt and you wanted to run a script in this folder you would need to surround the path in double quotes; that\u2019s because the path includes blank spaces. In other words:<\/P><PRE class=\"codeSample\">cscript.exe &#8220;C:\\Documents and Settings\\kenmyer\\Scripts\\Test.vbs&#8221;\n<\/PRE>\n<P>Logically enough, then, you might then expect the RunAs equivalent to look something like this:<\/P><PRE class=\"codeSample\">runas \/profile \/user:fabrikam\\kenmyer &#8220;cscript.exe &#8220;C:\\Documents and Settings\\kenmyer\\Scripts\\Test.vbs&#8221;&#8221;\n<\/PRE>\n<P>Unfortunately, that\u2019s not the case: the double quotes nested inside double quotes simply causes RunAs to give up and display the usage screen. Tossing double quotes inside double quotes won\u2019t work. Nor will it work to try double double quotes or triple double quotes or any other variation of quotes inside of quotes. Like a three-year-old child (or many thirty-three-year-old adults) RunAs wants things to be exactly right or it won\u2019t even try to help you.<\/P>\n<P>Fortunately, it\u2019s fairly easy to make RunAs happy. All you need to do is use the \\ character to \u201cescape\u201d any double quote marks that must to be embedded within the process path. This command <I>will<\/I> work:<\/P><PRE class=\"codeSample\">runas \/profile \/user:fabrikam\\kenmyer &#8220;cscript.exe \\&#8221;C:\\Documents and Settings\\kenmyer\\Scripts\\test.vbs&#8221;\\&#8221;\n<\/PRE>\n<P>Notice how we put our path together. We start out with a double quote mark and then the name of the script host:<\/P><PRE class=\"codeSample\">&#8220;cscript.exe\n<\/PRE>\n<P>At this point we need to insert the path, which includes blank spaces (and thus needs to be surrounded by double quotes). To do that we simply put a \\ immediately before the first double quote and another \\ immediately after the second double quote:<\/P><PRE class=\"codeSample\">&#8220;cscript.exe \\&#8221;C:\\Documents and Settings\\kenmyer\\Scripts\\test.vbs&#8221;\\\n<\/PRE>\n<P>All we have to do then is tack on the ending double quote and we\u2019re done:<\/P><PRE class=\"codeSample\">&#8220;cscript.exe \\&#8221;C:\\Documents and Settings\\kenmyer\\Scripts\\test.vbs&#8221;\\&#8221;\n<\/PRE>\n<P>It\u2019s simple, but it works. Just remember to bracket long file paths and any other item requiring double quotes and you\u2019ll be home free.<\/P>\n<P>So there you have it, BE: you can now use the RunAs command to run a script. As we noted earlier, you might want to check out the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/puzzle\/answers\/apr2106.mspx\"><B>weekly puzzle<\/B><\/A> for more information on running scripts under alternate credentials. And, in the spirit of synergy, stay tuned: it\u2019s just a matter of time before the Anaheim Mighty Scripters come to a hockey rink near you. (And, yes, this would be the time when the Scripting Guy who writes this column would usually note that hockey isn\u2019t even a real sport. But he\u2019s decided not to say anything controversial ever again, so you won\u2019t hear a word from him about that.)<\/P><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr06\/hey0428.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr06\/hey0428.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I use the RunAs command to run a script under alternate user credentials?&#8212; BE Hey, BE. You know, in the business world one of the hot topics these days is synergy. For example, after The Mighty Ducks became a hit movie the Disney Company went out and bought themselves an [&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,6],"class_list":["post-67433","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I use the RunAs command to run a script under alternate user credentials?&#8212; BE Hey, BE. You know, in the business world one of the hot topics these days is synergy. For example, after The Mighty Ducks became a hit movie the Disney Company went out and bought themselves an [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67433","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=67433"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67433\/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=67433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}