SAPIEN Solutions

SAPIEN homepage
PrimalScript script editor+ide
PrimalScope script debugger
Free Tools script utilities
SAPIEN Press tech books
ScriptingAnswers.com learn+share
ScriptingOutpost.com online store
Blog.Sapien.com official blog
Contact Us

 

Alternate Credentials - ADSI and WMI

 Here’s an example of using alternate credentials to connect to either WMI or ADSI. Note that WMI doesn’t allow alternate credentials if you’re connecting to the LOCAL computer - only for remote connections. Both of these examples use a function. GetPassword, which I’ve included. This prompts you for a password on the command-line (it’ll give you an error if run with WScript) and hides what you’re typing.

  1. '
  2. ' ScriptingAnswers.com Essentials - by Don Jones
  3. '
  4. ' Alternate Credentials
  5. ' This shows how to make a connection, using alternate
  6. ' credentials, using both WMI or ADSI. It also shows
  7. ' a very useful function that will gather a password,
  8. ' since it's a horrible idea to hardcode a password
  9. ' into any script
  10. '

  11. ' You're not intended to RUN this script - it just makes
  12. ' the connections; it doesn't do anything with them.
  13. ' Copy and paste appropriate code into your own script


  14. 'here's the WMI connection using alternate credentials
  15. Dim objLocator, objWMI
  16. Set objLocator = CreateObject("WbemScripting.SWbemLocator")
  17. On Error Resume Next
  18. Set objWMI = objLocator.ConnectServer("Server1","root\cimv2","Administrator",GetPassword("Administrator"))
  19. If Err <> 0 Then
  20. WScript.Echo "Error: " & Err.Description
  21. Else
  22. 'you're connected - use objWMI.ExecQuery to execute a query
  23. End If


  24. 'here's the ADSI connection using alternate credentials
  25. 'you'll obviously modify the LDAP query to match whatever
  26. 'AD object you're trying to get hold of. Notice that the
  27. 'alternate credential is an LDAP path!
  28. Const ADS_SECURE_AUTHENTICATION = 1
  29. Dim objRoot, objDomain
  30. Set objRoot = GetObject("LDAP:")
  31. On Error Resume Next
  32. Set objDomain = objRoot.OpenDSObject("LDAP://dc=company,dc=com", "cn=AdminDon,ou=Admins,dc=company,dc=com", _
  33. GetPassword("AdminDon@company.com"), ADS_SECURE_AUTHENTICATION)
  34. If Err <> 0 Then
  35. WScript.Echo "Error: " & Err.Description
  36. Else
  37. 'you're connected - objDomain, in this example, represents the domain
  38. End If


  39. ' Here's the password-collection Function
  40. ' It does assume we're (a) running on WinXP or 2003,
  41. ' and (b) running from CScript, not WScript
  42. ' Run CSCRIPT //H:cscript to make CScript your Default
  43. ' script host.
  44. Function GetPassword(strUsername)
  45. Dim objScriptPW, strPassword
  46. Set objScriptPW = CreateObject("ScriptPW.Password")
  47. WScript.Echo "Input password for " & strUsername & " and press Enter:"
  48. strPassword = objScriptPW.GetPassword
  49. GetPassword = strPassword
  50. End Function
[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags: , , , , ,

Leave a Reply


Entries (RSS) and Comments (RSS).