Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Sunday, June 7, 2009

How do you provide alternate credentials to a script?

Providing alternate credentials allows scripts to run while a non administrator is logged into the client. I found script samples for this on ScriptingAnswers.com
http://www.scriptinganswers.com/essentials/index.php/2008/02/15/alternate-credentials-adsi-and-wmi/

The following code was taken from WindowsITPro.comL http://windowsitpro.com/windowsscripting/article/articleid/39114/rem-running-a-script-with-alternate-credentials.html

LISTING 1: ADSI Script That Uses Alternate Credentials Const

ADS_SECURE_AUTHENTICATION = 1 strUserDN = "cn=Administrator,cn=Users,dc=acme,dc=com"strPassword = "bXk23s8w" ' BEGIN CALLOUT ASet objRoot = GetObject("LDAP:")Set objDomain = _ objRoot.OpenDSObject("LDAP://dc=acme,dc=com", _ strUserDN, strPassword, ADS_SECURE_AUTHENTICATION)' END CALLOUT A Set objOU = objDomain.Create("organizationalUnit", "ou=Students")objOU.Put "Description", "Student OU"objOU.SetInfo Set objGroup = objOU.Create("Group", "cn=Seniors")objGroup.Put "sAMAccountName", "Seniors"objGroup.Put "Description", "Seniors"objGroup.SetInfo Set objUser = objOU.Create("User", "cn=Student1")objUser.Put "sAMAccountName", "Student1"objUser.Put "Description", "Student1"objUser.SetInfo objGroup.Add objUser.ADSPath

LISTING 2: WMI Script That Uses Alternate Credentials Const

wbemImpersonationLevelImpersonate = 3 strComputer = "foo"strUser = "Administrator"strPassword = "bXk23s8w" ' BEGIN CALLOUT ASet objSWbemLocator = _ CreateObject("WbemScripting.SWbemLocator")objSWbemLocator.Security_.ImpersonationLevel = _ wbemImpersonationLevelImpersonateSet objSWbemServices = _ objSWbemLocator.ConnectServer(strComputer, _ "root\cimv2", strUser, strPassword)' END CALLOUT A Set colSWbemObjectSet = _ objSWbemServices.ExecQuery("SELECT * FROM “ _ & “Win32_OperatingSystem") For Each objSWbemObject In colSWbemObjectSet WScript.Echo "Name: " & objSWbemObject.Name WScript.Echo "Caption: " & objSWbemObject.CaptionBEGIN COMMENT ' Insert additional Win32_OperatingSystem properties here.END COMMENTNext

No comments: