sAMAccountName and case-sensitivity - vbscript

I'm able to fetch the user details for a particular ADS user through the following vbs code.
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"<LDAP://"& objDomain.Get("distinguishedName") &">;" & _
"(&(objectclass=user)(objectcategory=person)(sAMAccountName=" & strUserName & "));" & _
"cn,displayName;subtree"
objCommand.Execute
Here could I want to apply LCase to sAMAccountName before comparing with strUserName. Is this possible?

sAMAccountName is case-insensitive, so it doesn't matter if the value of strUserName is in upper, lower, or mixed case.

Related

Put DN into variable VBS

I am creating a script that will allow me to enter a username in our domain, and have it look up attributes from their AD profile.
So first I am getting the users' DN. Once I have that - I can run;
Set objAD = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & ***I NEED DN HERE***)
and query specific attributes to be output.
So what I need to do is somehow get the DN into a variable to put into the LDAP query. I know (I think) I need to get it from the Do Loop below, but am having a complete blank and can't figure out how to just put the whole DN into a variable.
Username = InputBox("Enter UserName to lookup...")
Set rootDSE = GetObject("LDAP://RootDSE")
base = "<LDAP://" & rootDSE.Get("defaultNamingContext") & ">"
fltr = "(&(objectClass=user)(objectCategory=Person)" & "(sAMAccountName=" & UserName & "))"
attr = "distinguishedName,sAMAccountName"
scope = "subtree"
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADsDSOObject"
conn.Open "Active Directory Provider"
Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandText = base & ";" & fltr & ";" & attr & ";" & scope
Set rs = cmd.Execute
Do Until rs.EOF
WScript.Echo rs.Fields("distinguishedName").Value
rs.MoveNext
Loop
rs.Close
conn.Close
In case anyone has the same problem - it was an easy fix.
Just needed to write it to a variable instead of echoing.
Do Until rs.EOF
strDN = rs.Fields("distinguishedname").value
rs.MoveNext
Loop

VBScript to check if the computer is present in Active Driectory

I want to know is there any script to check if the machine is present in AD or not. Like say i have a machine named XYZ and I want to check if this machine is in AD or not.
Using VBScript how do I do this?
I am new to LDAP.
If your AD is a Windows Server 2008 or 2008 R2 take a look at Dsquery Computer
Use : Dsquery computer -name MyComputer
However, you can try using ADODB
Or this example : (sample from VBsedit)
' List All Computer Accounts in Active Directory
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select Name, Location from 'LDAP://DC=fabrikam,DC=com' " _
& "Where objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value
objRecordSet.MoveNext
Loop
You can find an example here.. You need to use the WSCript.Network
http://social.technet.microsoft.com/Forums/windowsserver/en-US/58aea18c-d5ff-48a7-bc76-5bd64183ba8c/use-vbscript-to-query-ad-for-computer-account?forum=winserverDS

Writing From HKEY_USERS

I am attempting to create an application that will allow me to input a username and switch that user's default printer by modifying the registry under HKEY_USERS\UserSID. I cannot seem write values to that section of the registry though. Perhaps it's a Windows limitation? Here's the code I have so far.
Dim strComputer = "."
Dim objWMIService As Object = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Dim theUsername As String = TextBox1.Text
Dim theDomain As String = TextBox2.Text
Dim objAccount As Object = objWMIService.Get("Win32_UserAccount.Name='" & theUsername & "',Domain='" & theDomain & "'")
Dim theport As RegistryKey
theport = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\Devices")
Dim val As Object = theport.GetValue(ListBox1.SelectedItem)
theport.Close()
Dim theSid As String = objAccount.sid
Dim theKey As RegistryKey = Registry.Users.OpenSubKey(theSid + "\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows", True)
I don't think that there is some Windows limitation, because I wrote to HKEY_USERS\SIDs many times. But I used for this purpose the vbscript. Also I should warn you that you can only read&write to Users registry if they are logged. For not logged users - use the ActiveSetup.
There is my script on vbs which writes some registry to all logged users. Hope you could adapt it to VB.NET.
Option Explicit
Const HKEY_USERS = &H80000003
Dim objReg, objWMI, colSessions, objSession, colList, colUsers, objUser, Domain, UserName, objUserAccount, SID, WshShell
Set WshShell = CreateObject("WScript.Shell")
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colSessions = objWMI.ExecQuery("Select * from Win32_LogonSession Where LogonType = 2 Or LogonType = 10")
If colSessions.Count <> 0 Then
For Each objSession in colSessions
Set colUsers = objWMI.ExecQuery("Associators of " & "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " & "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )
For Each objUser in colUsers
Domain = objUser.Domain : UserName = objUser.Name
Set objUserAccount = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2:Win32_UserAccount.Domain='" & Domain & "',Name='" & UserName & "'")
SID = objUserAccount.SID
objReg.CreateKey HKEY_USERS, SID & "\Control Panel\Desktop"
objReg.SetStringValue HKEY_USERS, SID & "\Control Panel\Desktop", "Example", "1"
objReg.SetDwordValue HKEY_USERS, SID & "\Control Panel\Desktop", "Example", "2"
Next
Next
End If

vbs ldap query issues

i am currently trying to use my small knowledge of scripting to search through ldap find a user based on a variable then get my the displayname for that user.
so far i ahve the below and im stuck
On Error Resume Next
Dim objNetwork
Dim userName
Set objNetwork = CreateObject("WScript.Network")
userName = objNetwork.UserName
WScript.Echo userName
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.CommandText = _
"<LDAP://dc=domain,dc=com>;(objectCategory=User);Name;Subtree"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.Fields("Name").Value = username
objRecordSet.MoveNext
Loop
Wscript.Echo objRecordSet.Fields("Name").Value
= PLEASE HELP!!
I scartached the above and am now trying this but get syntax errors
option explicit dim cmd, cn, rs, objRoot,
set cmd = createobject("ADODB.Command")
set cn = createobject("ADODB.Connection")
set rs = createobject("ADODB.Recordset")
cn.open "Provider=ADsDSOObject;"
cmd.activeconnection = cn set
objRoot = getobject("LDAP://RootDSE") cmd.commandtext = "<LDAP://dn=domain,dn=com">;(&(objectClass=user)(!(objectClass=computer) (sAMAccountName=first.last))));name,displayname;subtree"
cmd.properties("page size")=1000 set rs = cmd.execute
wscript.echo rs("name")
I doubt the attribute name is Name, and try displayName instead in this line:
"<LDAP://dc=domain,dc=com>;(objectCategory=User);Name;Subtree"
ended up getting with this
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection
strDomainName = "dc=domain,dc=com"
strUserCN = username
objCommand.CommandText = "<LDAP://" & strDomainName & ">;(&(objectCategory=person) (objectClass=user)(cn=" & strUserCN & "));displayName;subtree"
Set objRecordSet = objCommand.Execute
If Not objRecordset.EOF Then
strray = split(objRecordSet.Fields("displayName"),"(")
strdat = strray(0)
username = trim(strdat)
End If

Get SamAccountname using DisplayName in Active Directory

I need vbscript that will do an LDAP query using a displayname, and retrieve the SamAccountName for me.
Something like this should do it:
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection
strDomainName = "dc=YOURDOMAIN,dc=com"
strUserCN = "FIRSTNAME LASTNAME"
objCommand.CommandText = "<LDAP://" & strDomainName & ">;(&(objectCategory=person)(objectClass=user)(cn=" & strUserCN & "));samAccountName;subtree"
Set objRecordSet = objCommand.Execute
If Not objRecordset.EOF Then
WScript.Echo objRecordSet.Fields("samAccountName")
End If
objConnection.Close
Set objRecordSet = Nothing
Set objConnection = Nothing
Set objCommand = Nothing
Great script ( and thankyou ) but at the moment only shows the first result in the recordset - the displaying the output bit needs to something more like this:
DO until objRecordset.EOF = TRUE
WScript.Echo objRecordSet.Fields("samAccountName")
objRecordSet.MoveNext
Loop

Resources