Creating a record set

Batch, ASP, JScript, Kixtart, etc.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 16 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
monicafanton
Posts: 7
Last visit: Thu Feb 01, 2024 5:46 am

Creating a record set

Post by monicafanton »

I am having a problem now with my hta. Here is what I want to do:

Create a list of servers using an LDAP query.
Be able to select servers from the list.
Execute some code against the list of selected servers.

I am so far unable to pass the server names to the sub where the code executes. Specifically, I am having an issue with lines 252-256.

If someone could provide me with information or suggestions, I'd really appreciate it.

Thanks,
Monica

<html><!--*******************************************************************'* Intel EDW Engineering'**********************************************************************'* Title: Local Administrator Password Change'* Author: Monica J. Fanton'* E-mail: mjfantox@intel.com'**********************************************************************'* Description: '*'* This application will query Active Directory for a list of server'* name(s), then display the server name(s) for confirmation. Once'* confirmed, the application will prompt the user for the new'* password and change the local administrator password on each server'* listed.'**********************************************************************'* Version History:'* '* 1.0 Released for test May 22, 2007'* '**********************************************************************'* Dependencies:'* '* 1. The system(s) where this application is run must be a member of'* the same domain as the target system(s).'* 2. The target system(s) must be accessible on the network.'* 3. The individual using this application must have sufficient'* permissions on the target system(s) to perform the operations'* in this application.'* '**********************************************************************'* HTML Application Begins'*******************************************************************-->
<head><title>Password Change Tool</title><HTA:APPLICATION APPLICATIONNAME="Password Change Tool" SCROLL="yes" SINGLEINSTANCE="yes" WINDOWSTATE="normal"></head>
<!--*******************************************************************'* HTML Style Tags'*******************************************************************-->
<style>body{ background-color: buttonface; font-family: arial; font-size: 8pt;}.button{ font-family: arial; font-size: 8pt; width: 100px;}.buttonLong{ font-family: arial; font-size: 10pt; border-style:none; text-align: left; color:#483D8B; text-decoration:underline;}textarea{ font-family:monospace; font-size: 10pt; margin-left: 0px; padding: 0px;}span{ font-family: arial; font-size: 8pt; margin-left: -3px;}select{ font-family: arial; font-size: 10pt; margin-left: 0px;}input{ font-family: arial; font-size: 8pt; margin-left: 0px;}div.code{ font-family: arial; font-size:12pt; margin-left: 2px; position:absolute; color:#483D8B; left:10px; top:77px;}div.attention{ font-family: arial; font-size: 10pt; margin-left: 0px; color: red; font-style: bold; position:absolute; left:230px; top:40px; width:280px;}div.notes{ font-family: arial; font-size: 10pt; margin-left: 0px; color: black; position:absolute; top:95px;}</style>
<!--*******************************************************************'* HTML Application VBScript'*******************************************************************-->
<script language="VBScript">
'**********************************************************************'* HTML Application VBScript Variable Declarations'**********************************************************************
Option Explicit
Dim strQuery, strDomain, strComputer, strHTMLDim strScript, strServerName, strPasswordDim objRoot, objList, objConnection, objCommand, objRecordSetDim objServerList, objPassword, objWMIService, objAccount, objUserDim colAccounts
'**********************************************************************'* HTML Application VBScript Static Variable Assignments'**********************************************************************
Const ADS_SCOPE_SUBTREE = 2Const adVarChar = 200Const MaxCharacters = 255Const DISABLE_NETBIOS = 2
'**********************************************************************'* HTML Application VBScript Subs and Functions'**********************************************************************
On Error Resume Next
Sub Window_OnLoad()
'Prompt the user for the search string strQuery = InputBox("What is the search string?")
If strQuery = "" Then Window.close() Else ' Get the domain name Set objRoot = GetObject("LDAP://rootDSE") strDomain = "LDAP://" & objRoot.Get("defaultNamingContext")
' Create recordset for computer names Set objList = CreateObject("ADOR.RecordSet")
Set objList = CreateObject("ADOR.Recordset") objList.Fields.Append "ServerName", adVarChar, MaxCharacters objList.Open
' Create the connection Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider"
' Query the directory Set objCommand.ActiveConnection = objConnection objCommand.CommandText = "<" & strDomain & ">;(&(objectCategory=computer)" & _ "(Name=*" & strQuery & "*));Name;subtree"
' Set up command properties objCommand.Properties("Page Size") = 1000 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.Properties("Cache Results") = False
' Put computer names into the list Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
'Create the list While Not objRecordSet.EOF objList.AddNew objList("ServerName") = objRecordset.Fields("Name") objRecordSet.MoveNext Wend
'Alphabetize the list objList.Sort = "ServerName"
'Start at the beginning of the list objList.MoveFirst
'Go through the list and display each server name with a checkbox Do Until objList.EOF strComputer = objList.Fields.Item("ServerName") strHTML = ServerList.InnerHTML strHTML = strHTML & "<table><tr><td> </td><td>" & _ "<input type='checkbox' name=" & strComputer & _ " onclick='Selected(""" & strComputer & """)'>" & _ strComputer & "</td></tr>" objList.MoveNext ServerList.InnerHTML = strHTML Loop End IfEnd Sub
Sub Selected(strServerSelected) Dim objServerExists
objServerExists = False
Set objServerList = CreateObject("ADOR.Recordset") objServerList.Fields.Append "ServerName", adVarChar, MaxCharacters objServerList.Open
' If (objServerList.RecordCount > 0) Then' objServerList.MoveFirst
' Do Until objServerList.EOF' If objServerList.Fields.Item("ServerName") = strServerSelected Then' objServerList.Delete' objServerExists = True' End If' objServerList.MoveNext' Loop' End If
If objServerExists = False Then objServerList.AddNew objServerList("ServerName") = strServerSelected MsgBox(objServerList("ServerName")) End IfEnd Sub
Sub RunScript() If (objServerList.RecordCount > 0) Then objServerList.MoveFirst Do Until objServerList.EOF strServerName = objServerList.Fields.Item("ServerName") strPassword = InputBox("Please enter the new password:")
If strPassword = "" Then Window.close() End If
'Get the local administrator user name Set objWMIService = GetObject("winmgmts:" & strServerName & "rootcimv2")
Set colAccounts = objWMIService.ExecQuery _ ("Select * From Win32_UserAccount Where LocalAccount = TRUE")
For Each objAccount in colAccounts If Left (objAccount.SID, 6) = "S-1-5-" and Right(objAccount.SID, 4) = "-500" Then Set objUser = GetObject("WinNT://" & strServerName & "/" & objAccount.Name)
' Set the password objUser.SetPassword(strPassword) End If strHTML = "The local administrator password " & _ "has been successfully changed on " & strServerName & "." Next Logging.InnerHTML = strHTML
objServerList.MoveNext Loop end If
End Sub
Sub Quit Window.close()End Sub
</script>
<body>
<BR><table width="100%"><tr><td><b>Server List:</b><td align="right"><input id=runbutton class="button" type="button" value="Run Script" name="run_button" onClick="RunScript"><input id=runbutton class="button" type="button" value="Cancel" name="run_button" onClick="Quit"></td></tr></table><table><tr><td width = "10%" valign="top"><span id="ServerList"></span></td><td></td><td width = "67%" valign="top"> <span id="Logging"></span></td></tr></table> </body></html>
User avatar
monicafanton
Posts: 7
Last visit: Thu Feb 01, 2024 5:46 am

Creating a record set

Post by monicafanton »

I am having a problem now with my hta. Here is what I want to do:

Create a list of servers using an LDAP query.
Be able to select servers from the list.
Execute some code against the list of selected servers.

I am so far unable to pass the server names to the sub where the code executes. Specifically, I am having an issue with lines 252-256.

If someone could provide me with information or suggestions, I'd really appreciate it.

Thanks,
Monica

<html><!--*******************************************************************'* Intel EDW Engineering'**********************************************************************'* Title: Local Administrator Password Change'* Author: Monica J. Fanton'* E-mail: mjfantox@intel.com'**********************************************************************'* Description: '*'* This application will query Active Directory for a list of server'* name(s), then display the server name(s) for confirmation. Once'* confirmed, the application will prompt the user for the new'* password and change the local administrator password on each server'* listed.'**********************************************************************'* Version History:'* '* 1.0 Released for test May 22, 2007'* '**********************************************************************'* Dependencies:'* '* 1. The system(s) where this application is run must be a member of'* the same domain as the target system(s).'* 2. The target system(s) must be accessible on the network.'* 3. The individual using this application must have sufficient'* permissions on the target system(s) to perform the operations'* in this application.'* '**********************************************************************'* HTML Application Begins'*******************************************************************-->
<head><title>Password Change Tool</title><HTA:APPLICATION APPLICATIONNAME="Password Change Tool" SCROLL="yes" SINGLEINSTANCE="yes" WINDOWSTATE="normal"></head>
<!--*******************************************************************'* HTML Style Tags'*******************************************************************-->
<style>body{ background-color: buttonface; font-family: arial; font-size: 8pt;}.button{ font-family: arial; font-size: 8pt; width: 100px;}.buttonLong{ font-family: arial; font-size: 10pt; border-style:none; text-align: left; color:#483D8B; text-decoration:underline;}textarea{ font-family:monospace; font-size: 10pt; margin-left: 0px; padding: 0px;}span{ font-family: arial; font-size: 8pt; margin-left: -3px;}select{ font-family: arial; font-size: 10pt; margin-left: 0px;}input{ font-family: arial; font-size: 8pt; margin-left: 0px;}div.code{ font-family: arial; font-size:12pt; margin-left: 2px; position:absolute; color:#483D8B; left:10px; top:77px;}div.attention{ font-family: arial; font-size: 10pt; margin-left: 0px; color: red; font-style: bold; position:absolute; left:230px; top:40px; width:280px;}div.notes{ font-family: arial; font-size: 10pt; margin-left: 0px; color: black; position:absolute; top:95px;}</style>
<!--*******************************************************************'* HTML Application VBScript'*******************************************************************-->
<script language="VBScript">
'**********************************************************************'* HTML Application VBScript Variable Declarations'**********************************************************************
Option Explicit
Dim strQuery, strDomain, strComputer, strHTMLDim strScript, strServerName, strPasswordDim objRoot, objList, objConnection, objCommand, objRecordSetDim objServerList, objPassword, objWMIService, objAccount, objUserDim colAccounts
'**********************************************************************'* HTML Application VBScript Static Variable Assignments'**********************************************************************
Const ADS_SCOPE_SUBTREE = 2Const adVarChar = 200Const MaxCharacters = 255Const DISABLE_NETBIOS = 2
'**********************************************************************'* HTML Application VBScript Subs and Functions'**********************************************************************
On Error Resume Next
Sub Window_OnLoad()
'Prompt the user for the search string strQuery = InputBox("What is the search string?")
If strQuery = "" Then Window.close() Else ' Get the domain name Set objRoot = GetObject("LDAP://rootDSE") strDomain = "LDAP://" & objRoot.Get("defaultNamingContext")
' Create recordset for computer names Set objList = CreateObject("ADOR.RecordSet")
Set objList = CreateObject("ADOR.Recordset") objList.Fields.Append "ServerName", adVarChar, MaxCharacters objList.Open
' Create the connection Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider"
' Query the directory Set objCommand.ActiveConnection = objConnection objCommand.CommandText = "<" & strDomain & ">;(&(objectCategory=computer)" & _ "(Name=*" & strQuery & "*));Name;subtree"
' Set up command properties objCommand.Properties("Page Size") = 1000 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.Properties("Cache Results") = False
' Put computer names into the list Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
'Create the list While Not objRecordSet.EOF objList.AddNew objList("ServerName") = objRecordset.Fields("Name") objRecordSet.MoveNext Wend
'Alphabetize the list objList.Sort = "ServerName"
'Start at the beginning of the list objList.MoveFirst
'Go through the list and display each server name with a checkbox Do Until objList.EOF strComputer = objList.Fields.Item("ServerName") strHTML = ServerList.InnerHTML strHTML = strHTML & "<table><tr><td> </td><td>" & _ "<input type='checkbox' name=" & strComputer & _ " onclick='Selected(""" & strComputer & """)'>" & _ strComputer & "</td></tr>" objList.MoveNext ServerList.InnerHTML = strHTML Loop End IfEnd Sub
Sub Selected(strServerSelected) Dim objServerExists
objServerExists = False
Set objServerList = CreateObject("ADOR.Recordset") objServerList.Fields.Append "ServerName", adVarChar, MaxCharacters objServerList.Open
' If (objServerList.RecordCount > 0) Then' objServerList.MoveFirst
' Do Until objServerList.EOF' If objServerList.Fields.Item("ServerName") = strServerSelected Then' objServerList.Delete' objServerExists = True' End If' objServerList.MoveNext' Loop' End If
If objServerExists = False Then objServerList.AddNew objServerList("ServerName") = strServerSelected MsgBox(objServerList("ServerName")) End IfEnd Sub
Sub RunScript() If (objServerList.RecordCount > 0) Then objServerList.MoveFirst Do Until objServerList.EOF strServerName = objServerList.Fields.Item("ServerName") strPassword = InputBox("Please enter the new password:")
If strPassword = "" Then Window.close() End If
'Get the local administrator user name Set objWMIService = GetObject("winmgmts:" & strServerName & "rootcimv2")
Set colAccounts = objWMIService.ExecQuery _ ("Select * From Win32_UserAccount Where LocalAccount = TRUE")
For Each objAccount in colAccounts If Left (objAccount.SID, 6) = "S-1-5-" and Right(objAccount.SID, 4) = "-500" Then Set objUser = GetObject("WinNT://" & strServerName & "/" & objAccount.Name)
' Set the password objUser.SetPassword(strPassword) End If strHTML = "The local administrator password " & _ "has been successfully changed on " & strServerName & "." Next Logging.InnerHTML = strHTML
objServerList.MoveNext Loop end If
End Sub
Sub Quit Window.close()End Sub
</script>
<body>
<BR><table width="100%"><tr><td><b>Server List:</b><td align="right"><input id=runbutton class="button" type="button" value="Run Script" name="run_button" onClick="RunScript"><input id=runbutton class="button" type="button" value="Cancel" name="run_button" onClick="Quit"></td></tr></table><table><tr><td width = "10%" valign="top"><span id="ServerList"></span></td><td></td><td width = "67%" valign="top"> <span id="Logging"></span></td></tr></table> </body></html>
User avatar
monicafanton
Posts: 7
Last visit: Thu Feb 01, 2024 5:46 am

Creating a record set

Post by monicafanton »

To clarify:

The script is adding the server names to the recordset, but it appears that each server name is being overwritten by the next one. I need to create a recordset of all the selected server names.

Any information, comments, or suggestions would be very appreciated.

Thanks,
Monica
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Creating a record set

Post by jvierra »

What is the error message or problem you are having with this code. At a glance it looks correct.

If "objServerExists" is never gettng set it will always be false so teh code should execute. If it is gettng set to true or some other vaklue the code may always be skipped.

jvierra2007-06-01 11:16:37
This topic is 16 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked