sandeepagarwals
Programmer
Hi All,
My application requires installing some services with local system account or windows domain account. When domain account is selected, i need to validate windows domain username and password supplied by the user using vbscript. I found following script on net which does the same, but there are concerns that it tries to create a mapped drive in the machine with user account details.
Is there a better and easier way to do this only using vbscripting?
-----------------------------------------------------------
Dim objRootDSE, strConfig, objConnection, objCommand, strQuery
Dim objRecordSet, objDC, objSite
Dim fso, WshNetwork, sUser, oUser, sPassword, sDomain, oDomain, mappedDrive, drive
Dim objDictionary, strComputer, objWMIService, objDisk, strDrive, freeDrive, colDisks, i
Set oArgs = WScript.Arguments
sUser = "xyz\username"
aTokens = Split(sUser, "\")
sDomain = Trim(aTokens(0))
sUser = Trim(aTokens(1))
sPassword = "password"
If sDomain = sUser Then
sDomain = "."
End If
Set oDomain = GetObject("WinNT://" & sDomain)
oDomain.Filter = Array("user")
For Each oUser In oDomain
If LCase(oUser.Name) = LCase(sUser) Then
Set objDictionary = CreateObject("Scripting.Dictionary")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
objDictionary.Add objDisk.DeviceID, objDisk.DeviceID
Next
freeDrive = "Empty"
For i = 67 to 90
strDrive = Chr(i) & ":"
If objDictionary.Exists(strDrive) Then
Else
Wscript.Echo strDrive & " is the next available drive letter."
freeDrive = strDrive
Exit For
End If
Next
If freeDrive = "Empty" Then
Else
' Determine configuration context from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strConfig = objRootDSE.Get("configurationNamingContext")
' Use ADO to search Active Directory for ObjectClass nTDSDSA.
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
strQuery = "<LDAP://" & strConfig _
& ">;(ObjectClass=nTDSDSA);AdsPath;subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
' creating file system objects
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = WScript.CreateObject("WScript.Network")
' The parent object of each object with ObjectClass=nTDSDSA is a Domain
' Controller. The parent of each Domain Controller is a "Servers"
' container, and the parent of this container is the "Site" container.
Do Until objRecordSet.EOF
Set objDC = GetObject( _
GetObject(objRecordSet.Fields("AdsPath")).Parent)
Set objSite = GetObject(GetObject(objDC.Parent).Parent)
On Error Resume Next
WshNetwork.MapNetworkDrive freeDrive, "\\" & objDC.cn & "\netlogon",false,sDomain & "\" & sUser, sPassword
If fso.FolderExists(freeDrive) Then
WshNetwork.RemoveNetworkDrive freeDrive
Wscript.Echo "SUCCESS"
wscript.Quit(0)
End If
objRecordSet.MoveNext
Loop
Wscript.Echo "Password is wrong"
Wscript.Quit(1)
' Clean up.
objConnection.Close
Set objRootDSE = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Set objRecordSet = Nothing
Set objDC = Nothing
Set objSite = Nothing
End If
End If
Next
Wscript.Echo "username is wrong"
Thanks
Sandeep
My application requires installing some services with local system account or windows domain account. When domain account is selected, i need to validate windows domain username and password supplied by the user using vbscript. I found following script on net which does the same, but there are concerns that it tries to create a mapped drive in the machine with user account details.
Is there a better and easier way to do this only using vbscripting?
-----------------------------------------------------------
Dim objRootDSE, strConfig, objConnection, objCommand, strQuery
Dim objRecordSet, objDC, objSite
Dim fso, WshNetwork, sUser, oUser, sPassword, sDomain, oDomain, mappedDrive, drive
Dim objDictionary, strComputer, objWMIService, objDisk, strDrive, freeDrive, colDisks, i
Set oArgs = WScript.Arguments
sUser = "xyz\username"
aTokens = Split(sUser, "\")
sDomain = Trim(aTokens(0))
sUser = Trim(aTokens(1))
sPassword = "password"
If sDomain = sUser Then
sDomain = "."
End If
Set oDomain = GetObject("WinNT://" & sDomain)
oDomain.Filter = Array("user")
For Each oUser In oDomain
If LCase(oUser.Name) = LCase(sUser) Then
Set objDictionary = CreateObject("Scripting.Dictionary")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
objDictionary.Add objDisk.DeviceID, objDisk.DeviceID
Next
freeDrive = "Empty"
For i = 67 to 90
strDrive = Chr(i) & ":"
If objDictionary.Exists(strDrive) Then
Else
Wscript.Echo strDrive & " is the next available drive letter."
freeDrive = strDrive
Exit For
End If
Next
If freeDrive = "Empty" Then
Else
' Determine configuration context from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strConfig = objRootDSE.Get("configurationNamingContext")
' Use ADO to search Active Directory for ObjectClass nTDSDSA.
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
strQuery = "<LDAP://" & strConfig _
& ">;(ObjectClass=nTDSDSA);AdsPath;subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
' creating file system objects
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = WScript.CreateObject("WScript.Network")
' The parent object of each object with ObjectClass=nTDSDSA is a Domain
' Controller. The parent of each Domain Controller is a "Servers"
' container, and the parent of this container is the "Site" container.
Do Until objRecordSet.EOF
Set objDC = GetObject( _
GetObject(objRecordSet.Fields("AdsPath")).Parent)
Set objSite = GetObject(GetObject(objDC.Parent).Parent)
On Error Resume Next
WshNetwork.MapNetworkDrive freeDrive, "\\" & objDC.cn & "\netlogon",false,sDomain & "\" & sUser, sPassword
If fso.FolderExists(freeDrive) Then
WshNetwork.RemoveNetworkDrive freeDrive
Wscript.Echo "SUCCESS"
wscript.Quit(0)
End If
objRecordSet.MoveNext
Loop
Wscript.Echo "Password is wrong"
Wscript.Quit(1)
' Clean up.
objConnection.Close
Set objRootDSE = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Set objRecordSet = Nothing
Set objDC = Nothing
Set objSite = Nothing
End If
End If
Next
Wscript.Echo "username is wrong"
Thanks
Sandeep