Login | |
|
 |
RE: Trying to find % of total active user home folders ... - 2/7/2006 8:18:26 AM
|
|
 |
|
| |
Exidy
Posts: 18
Score: 0
Joined: 1/26/2006
Status: offline
|
The saga continues... The script runs now! Hooray! Much thanks to ebgreen for helping me get this far. Unfortunately it isn't returning the information I need. Here's the lowdown: When I specify the entire domain for the LDAP path, the script absolutely refuses to return a number of users other than 0. I thought it might be related to the Page Size property, so I changed that to 10000, but that didn't work, so I changed the path to be an OU which had only 763 users. That worked. The script will actually run, and it creates a text file as instructed -- except the only correct number in the text file is the total number of users (in the OU). For the number of users on each server, the values are "1". The dictionary objects don't seem to be storing anything, except for a single key - server01, one entry. Nothing else. I even used the following to examine the contents of the dictionary objects... ' Testing storage of items in dictionary objects colItems = Group1.Items For Each strUsername in colItems Wscript.Echo strUsername Next Since I told the script to store userPrincipalNames alongside the server names in the dictionary objects, I thought I'd get at least one userPrincipalName. Nope. The echo was totally blank. It also turns out that the percentages in the text file are blank. Before, I noticed that I forgot some ampersands in the strFile.WriteLines, but I fixed those. As far as I can tell, my arithmetic expressions for Percent1 and Percent2 are fine...
|
|
| |
|
|
|
 |
RE: Trying to find % of total active user home folders ... - 2/7/2006 8:40:00 AM
|
|
 |
|
| |
ebgreen
Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
|
If you could, would you please post your entire script exactly as it now is. I realize it may very well be almost identical to something already posted in this thread, I just find that the almost part is often the problem. Oh and an example of the output that you get and an example of the output you expect would also be helpful.
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: Trying to find % of total active user home folders ... - 2/7/2006 8:52:41 AM
|
|
 |
|
| |
Exidy
Posts: 18
Score: 0
Joined: 1/26/2006
Status: offline
|
Here's the code as it is currently being used: On Error Resume Next ' Define various variables and objects Dim Group1 ' Create variable for first dictionary object (server01) Dim Group2 ' Create variable for second dictionary object (server02) Dim Count1 Dim Count2 Set Group1 = CreateObject("Scripting.Dictionary") ' Create the actual dictionary object Set Group2 = CreateObject("Scripting.Dictionary") ' Create the actual dictionary object Set Count1 = Group1.Count Set Count2 = Group2.Count ' Establish connection to Active Directory Const ADS_SCOPE_SUBTREE = 1 Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" Set objCommand.ActiveConnection = objConnection objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.Properties("Page Size") = 1000 objCommand.CommandText = _ "SELECT homeDirectory FROM 'LDAP://ou=Users,ou=Site1,ou=Sites,dc=mycompany,dc=com' WHERE objectClass='user'" ' I'm restricting it to one specific OU for the moment because a domainwide search doesn't work Set objRecordSet = objCommand.Execute ' Steps through user records, plucks out home directory path, creates dictionary objects according to the results objRecordSet.MoveFirst Do Until objRecordSet.EOF strHomeDirectory = objRecordSet.Fields("homeDirectory").Value strUsername = objRecordSet.Fields("userPrincipalName").Value For Each strHomeDirectory In objRecordSet.Fields If InStr(strHomeDirectory, "server01") > 0 then Group1.Add "server01", strUsername Else If InStr(strHomeDirectory, "server02") > 0 Then Group2.Add "server02", strUsername End If End If Next objRecordSet.MoveNext Loop ' Testing storage of items in dictionary objects (this echo always turns out to be blank) colItems = Group1.Items For Each strUsername in colItems Wscript.Echo strUsername Next ' Gets percentages of users on each server strTotalUsers = objRecordSet.RecordCount Dim Percent1 Dim Percent2 Set Percent1 = Count1 / strTotalUsers Set Percent2 = Count2 / strTotalUsers ' Output results to text file strPath = "C:\HomeFolderStats.txt" Set objfso = CreateObject("Scripting.FileSystemObject") Set strFile = objfso.CreateTextFile(strPath, True) strFile.WriteLine("Number of users on server01: " & Group1.Count) strFile.WriteLine("Number of users on server02: " & Group2.Count) strFIle.WriteLine("Total number of users: " & strTotalUsers) strFile.WriteLine("Percentage of users on server01: " & Percent1) strFile.WriteLine("Percentage of users on server02: " & Percent2) strFile.Close *** Here's a sample of the output text file. The number of users on each server stays stubbornly at 1, and the percentages are always blank. Number of users on server01: 1 Number of users on server02: 1 Total number of users: 763 Percentage of users on server01: Percentage of users on server02: And here's a sample of what I wanted to get: Number of users on server01: 699 Number of users on server02: 64 (just sample numbers, logically they should add to 763) Total number of users: 763 Percentage of users on server01: 0.916 (no clue what it would round to, I'll probably put in a x100 in the future plus a % sign) Percentage of users on server02: 0.084
< Message edited by Exidy -- 2/7/2006 9:31:28 AM >
|
|
| |
|
|
|
 |
RE: Trying to find % of total active user home folders ... - 2/7/2006 9:18:08 AM
|
|
 |
|
| |
ebgreen
Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
|
The first thing that I see is this: Set Count1 = Group1.Count Set Count2 = Group2.Count This should error out saying Microsoft VBScript runtime error: Object required: 'Count'. Do you have On Error Resume Next in your script?
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: Trying to find % of total active user home folders ... - 2/7/2006 9:47:04 AM
|
|
 |
|
| |
ebgreen
Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
|
Ok, so remove those lines (since they are invalid anyway) and change these lines: Set Percent1 = Count1 / strTotalUsers Set Percent2 = Count2 / strTotalUsers to this: Set Percent1 = Group1.Count / strTotalUsers Set Percent2 = Group2.Count / strTotalUsers Now, If we get down to the end of the sript and it says the count of Group1 is 1 then the count of group1 is 1. We need to find out why. Look at this chunk: objRecordSet.MoveFirst Do Until objRecordSet.EOF strHomeDirectory = objRecordSet.Fields("homeDirectory").Value strUsername = objRecordSet.Fields("userPrincipalName").Value For Each strHomeDirectory In objRecordSet.Fields If InStr(strHomeDirectory, "server01") > 0 then Group1.Add "server01", strUsername Else If InStr(strHomeDirectory, "server02") > 0 Then Group2.Add "server02", strUsername End If End If Next objRecordSet.MoveNext Loop You go through each item in the collection. If the home server name has "server01" in it then you add an item to Group1 named "server01" and you give that item a value that is the user name. Now (back on the soapbox) if you did not have On Error Resume Next at the top of your script, you would have been politely informed by the WSH that you cannot add a dictionary entry with the same name as one that already exists. Since you decided you didn't want WSH to tell you when something was wrong with your code (by saying On Error Resume Next) it merrily goes on it's way instead of pointing out your problem. To adress the problem, Change this line: Group1.Add "server01", strUsername and this line: Group2.Add "server02", strUsername to this: Group1.Add strUsername, "server01" and this: Group2.Add strUsername, "server02" respectively. If it still is not working the way you want, then post back here with the code that you are actually running, the actual output, the expected output, and a description of the issue with exact errors and lines that the errors occur on if there are any. EDIT: Sorry, I re-read that post and it is a bit cheeky. My apologies. I'm in a cheeky mood today.
< Message edited by ebgreen -- 2/7/2006 9:48:58 AM >
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: Trying to find % of total active user home folders ... - 2/7/2006 10:04:51 AM
|
|
 |
|
| |
Exidy
Posts: 18
Score: 0
Joined: 1/26/2006
Status: offline
|
I followed the steps that you laid out, including removing "On Error Resume Next" from the top. The script now errors out with the following message: Line: 30 Char: 3 Error: Item cannot be found in the collection corresponding to the requested name or ordinal. Code: 800A0CC1 Source: ADODB.Recordset Here's the code I'm running now: ' Define various variables and objects Dim Group1 ' Create variable for first dictionary object (server01) Dim Group2 ' Create variable for second dictionary object (server02) Dim Count1 Dim Count2 Set Group1 = CreateObject("Scripting.Dictionary") ' Create the actual dictionary object Set Group2 = CreateObject("Scripting.Dictionary") ' Create the actual dictionary object ' Establish connection to Active Directory Const ADS_SCOPE_SUBTREE = 1 Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" Set objCommand.ActiveConnection = objConnection objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.Properties("Page Size") = 1000 objCommand.CommandText = _ "SELECT homeDirectory FROM 'LDAP://ou=Users,ou=Site1,ou=Sites,dc=mycompany,dc=com' WHERE objectClass='user'" ' Not sure whether objectClass=user is correct to use Set objRecordSet = objCommand.Execute ' Steps through user records, plucks out home directory path, creates dictionary objects according to the results objRecordSet.MoveFirst Do Until objRecordSet.EOF strHomeDirectory = objRecordSet.Fields("homeDirectory").Value strUsername = objRecordSet.Fields("userPrincipalName").Value For Each strHomeDirectory In objRecordSet.Fields If InStr(strHomeDirectory, "server01") > 0 then Group1.Add strUsername, "server01" Else If InStr(strHomeDirectory, "server02") > 0 Then Group2.Add strUsername, "server02" End If End If Next objRecordSet.MoveNext Loop ' Gets percentages of users on each server strTotalUsers = objRecordSet.RecordCount Dim Percent1 Dim Percent2 Set Percent1 = Group1.Count / strTotalUsers Set Percent2 = Group2.Count / strTotalUsers ' Output results to text file strPath = "C:\HomeFolderStats.txt" Set objfso = CreateObject("Scripting.FileSystemObject") Set strFile = objfso.CreateTextFile(strPath, True) strFile.WriteLine("Number of users on server01: " & Group1.Count) strFile.WriteLine("Number of users on server02: " & Group2.Count) strFIle.WriteLine("Total number of users: " & strTotalUsers) strFile.WriteLine("Percentage of users on server01: " & Percent1) strFile.WriteLine("Percentage of users on server02: " & Percent2) strFile.Close Expected output: Number of users on server01: 699 Number of users on server02: 64 (just sample numbers) Total number of users: 763 Percentage of users on server01: 0.916 Percentage of users on server02: 0.084 There's no actual output to compare with now since the script won't run. Line 30 Char 3 refers to: strHomeDirectory = objRecordSet.Fields("homeDirectory").Value Now that I'm looking at it, the SQL query and the attributes I'm requesting look a little odd...
< Message edited by Exidy -- 2/7/2006 10:10:04 AM >
|
|
| |
|
|
|
 |
RE: Trying to find % of total active user home folders ... - 2/7/2006 10:48:37 AM
|
|
 |
|
| |
Exidy
Posts: 18
Score: 0
Joined: 1/26/2006
Status: offline
|
Update! I made some further changes. Here's the code I'm running now. ' Define various variables and objects Dim Group1 ' Create variable for first dictionary object (server01) Dim Group2 ' Create variable for second dictionary object (server02) Dim Count1 Dim Count2 Set Group1 = CreateObject("Scripting.Dictionary") ' Create the actual dictionary object Set Group2 = CreateObject("Scripting.Dictionary") ' Create the actual dictionary object ' Establish connection to Active Directory Const ADS_SCOPE_SUBTREE = 1 Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" Set objCommand.ActiveConnection = objConnection objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.Properties("Page Size") = 1000 strBase = "<LDAP://ou=Users,ou=Site1,ou=Sites,dc=mycompany,dc=com>" strFilter = "(&(objectCategory=person)(objectClass=user))" strAttributes = "homeDirectory,userPrincipalName" strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree" objCommand.CommandText = strQuery Set objRecordSet = objCommand.Execute ' Steps through user records, plucks out home directory path, creates dictionary objects according to the results objRecordSet.MoveFirst Do Until objRecordSet.EOF strHomeDirectory = objRecordSet.Fields("homeDirectory").Value strUsername = objRecordSet.Fields("userPrincipalName").Value For Each strHomeDirectory In objRecordSet.Fields If InStr(strHomeDirectory, "server01") > 0 then Group1.Add strUsername, "server01" Else If InStr(strHomeDirectory, "server02") > 0 Then Group2.Add strUsername, "server02" End If End If Next objRecordSet.MoveNext Loop ' Gets percentages of users on each server strTotalUsers = objRecordSet.RecordCount Dim Percent1 Dim Percent2 Set Percent1 = Group1.Count / strTotalUsers Set Percent2 = Group2.Count / strTotalUsers ' Output results to text file strPath = "C:\HomeFolderStats.txt" Set objfso = CreateObject("Scripting.FileSystemObject") Set strFile = objfso.CreateTextFile(strPath, True) strFile.WriteLine("Number of users on server01: " & Group1.Count) strFile.WriteLine("Number of users on server02: " & Group2.Count) strFIle.WriteLine("Total number of users: " & strTotalUsers) strFile.WriteLine("Percentage of users on server01: " & Percent1) strFile.WriteLine("Percentage of users on server02: " & Percent2) strFile.Close Now the error I get is: Line: 41 Char: 11 Error: This key is already associated with an element of this collection Code: 800A01C9 I'm guessing that I need some way to populate the dictionary object full of the server/username pairs.
< Message edited by Exidy -- 2/7/2006 10:57:30 AM >
|
|
| |
|
|
|
 |
RE: Trying to find % of total active user home folders ... - 2/8/2006 7:04:46 AM
|
|
 |
|
| |
ebgreen
Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
|
If you are getting an object exists error then the name you are using appears twice in AD. To get around this test for existence before you add: For Each strHomeDirectory In objRecordSet.Fields If InStr(strHomeDirectory, "server01") > 0 then If Not Group1.Exists(strUserName) Then Group1.Add strUsername, "server01" Else If InStr(strHomeDirectory, "server02") > 0 Then If Not Group2.Exists(strUserName) Then Group2.Add strUsername, "server02" End If End If Next objRecordSet.MoveNext Loop
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
|
|