{"id":70823,"date":"2004-12-13T15:43:00","date_gmt":"2004-12-13T15:43:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/12\/13\/how-can-i-run-a-script-under-alternate-credentials\/"},"modified":"2004-12-13T15:43:00","modified_gmt":"2004-12-13T15:43:00","slug":"how-can-i-run-a-script-under-alternate-credentials","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-run-a-script-under-alternate-credentials\/","title":{"rendered":"How Can I Run a Script Under Alternate 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! Is there a way for me to log on to a computer using a regular user account, but then run a script as a domain administrator?<BR><BR>&#8212; WW<\/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, WW. In answer to your question, yes, both WMI and ADSI provide a way for you to run a script under alternate security credentials; that is, both allow you to specify a user name and a password under which the script will run. Furthermore, both of these technologies provide a way to ensure that this user name and password are encrypted, and are <I>not<\/I> passed across the network using clear text. Thanks for the question, and hope that helped.<\/P>\n<P>Hey, just kidding! Most likely what you really want to know is <I>how<\/I> to run a script under alternate security credentials. Let\u2019s start by taking a look at a WMI script that connects to a remote computer (<B>atl-ws-01<\/B>) and retrieves the name of the operating system installed on that computer. Furthermore, it does this by running under the <B>Administrator<\/B> account, which in this example has a password of <B>4rTGh2#1<\/B>:<\/P><PRE class=\"codeSample\">Const WbemAuthenticationLevelPktPrivacy = 6<\/p>\n<p>strComputer = &#8220;atl-ws-01&#8221;\nstrNamespace = \u201croot\\cimv2\u201d\nstrUser = &#8220;Administrator&#8221;\nstrPassword = &#8220;4rTGh2#1&#8221;<\/p>\n<p>Set objWbemLocator = CreateObject(&#8220;WbemScripting.SWbemLocator&#8221;)\nSet objWMIService = objwbemLocator.ConnectServer _\n    (strComputer, strNamespace, strUser, strPassword)\nobjWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_OperatingSystem&#8221;)\nFor Each objItem in ColItems\n    Wscript.Echo strComputer &amp; &#8220;: &#8221; &amp; objItem.Caption\nNext\n<\/PRE>\n<P>As you can see, we start by creating a constant named WbemAuthenticationLevelPktPrivacy and assigning it the value 6; this value will be used to encrypt the communication between our computer and the remote computer. We then define four variables &#8211; strComputer, strNamespace, strUser, and strPassword &#8211; which hold the name of our remote computer; the WMI namespace we want to connect to; the user account we want to run the script under; and the password for that user account.<\/P>\n<P>At this point we\u2019re ready to connect to the remote computer using these alternate credentials. That\u2019s what these lines of code do:<\/P><PRE class=\"codeSample\">Set objWbemLocator = CreateObject(&#8220;WbemScripting.SWbemLocator&#8221;)\nSet objWMIService = objwbemLocator.ConnectServer _\n    (strComputer, strNamespace, strUser, strPassword)\nobjWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy\n<\/PRE>\n<P>We begin by creating an instance of the SWbemLocator object, and then calling the ConnectServer method. ConnectServer gets passed four parameters, which just happen to correspond to the four variables we created a moment ago. We then set the <B>Security_authenticationLevel<\/B> property to WbemAuthenticationLevelPktPrivacy, giving us a more secure connection between the two computers.<\/P>\n<P>From that point on, the rest of the code is the same old WMI code you know and love.<\/P>\n<P>One important note: this approach, in which you run a script under alternate credentials, works <I>only<\/I> on remote machines. For some reason, WMI won\u2019t let you run a script under alternate credentials on your own computer. Go figure.<\/P>\n<P>Now let\u2019s take a look at the ADSI version of this script. This sample script binds to the Ken Myer user account in Active Directory and tells us whether or not this account is disabled. The script connects to Active Directory using the <B>fabrikam\\Administrator<\/B>, which has a password of <B>4rTGh2#1<\/B>:<\/P><PRE class=\"codeSample\">Const ADS_SECURE_AUTHENTICATION = 1\nConst ADS_USE_ENCRYPTION = 2<\/p>\n<p>strPath = &#8220;LDAP:\/\/cn=kenmyer,ou=Finance,dc=fabrikam,dc=com&#8221;\nstrUser = &#8220;fabrikam\\Administrator&#8221;\nstrPassword = &#8220;4rTGh2#1&#8221;<\/p>\n<p>Set objDSO = GetObject(&#8220;LDAP:&#8221;)\nSet objUser = objDSO.OpenDSObject _\n    (strPath, strUser, strPassword, _\n        ADS_USE_ENCRYPTION OR ADS_SECURE_AUTHENTICATION)<\/p>\n<p>Wscript.Echo objUser.AccountDisabled\n<\/PRE>\n<P>In this script, we create two constants (ADS_SECURE_AUTHENTICATION and ADS_USE_ENCRYPTION) to ensure that the information passed between our computer and the domain is encrypted. We then create three variables &#8211; strPath, strUser, and strPassword &#8211; which host the ADsPath to the object we want to bind to in Active Directory (in this case, the Ken Myer user account); the account name we want to use when connecting to Active Directory; and the password for that account.<\/P>\n<P>Next we bind to the LDAP provider; we start here because the LDAP provider accepts anonymous bindings. Having connected to the LDAP object, we can then call the OpenDSObject method to bind to the Ken Myer user account. Note that we must pass OpenDSObject four parameters: the ADsPath to the Active Directory object; the user name we want to use when connecting to Active Directory; the password for that account; and the two constants we defined earlier. As with WMI, as soon as we make the connection, the rest of the script is the same as any other ADSI script.<\/P>\n<P>By the way, OpenDSObject also works with local user accounts; the primary difference is that you bind to WinNT provider instead of the LDAP provider (and, of course, the ADsPath will be different). Here are the relevant lines of code from a script that connects to a local computer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;WinNT:\/\/atl-ws-01\u201d\nstrUser = &#8220;Administrator&#8221;\nstrPassword = &#8220;4rTGh2#1&#8221;<\/p>\n<p>Set objDSO = GetObject(&#8220;WinNT:&#8221;)\nSet objComputer = objDSO.OpenDSObject _\n    (strComputer, strUser, strPassword, _\n        ADS_SECURE_AUTHENTICATION OR ADS_USE_ENCRYPTION)\n<\/PRE>\n<P>One thing we should add is that we don\u2019t recommend you hardcode passwords (especially Administrator passwords) in your scripts. Instead, you should make allowances to enter the password as a command-line argument or via an Input box or whatever works best for you. <\/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\/dec04\/hey1213.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\/dec04\/hey1213.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there a way for me to log on to a computer using a regular user account, but then run a script as a domain administrator?&#8212; WW Hey, WW. In answer to your question, yes, both WMI and ADSI provide a way for you to run a script under alternate security credentials; [&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-70823","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! Is there a way for me to log on to a computer using a regular user account, but then run a script as a domain administrator?&#8212; WW Hey, WW. In answer to your question, yes, both WMI and ADSI provide a way for you to run a script under alternate security credentials; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70823","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=70823"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70823\/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=70823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}