?
Solved

need help modifying script

Posted on 2005-03-12
7
Medium Priority
?
775 Views
Last Modified: 2008-01-09
Can someone help me modify this script?  I need it to run for only the logged in user.
It 'does' - BUT it shows X number of wscript popup windows as it's looping through userlist until it finds the correct user.
I'd like to make the "X" number be 'just' the one where they match please.
It was scary this week with 80 users in that list - I'm worried that when we have 200 users in the list, it will abend the system....

Set WshNetwork = WScript.CreateObject("WScript.Network")
strUser = WshNetwork.UserName

Const ADS_SCOPE_SUBTREE = 2
Const ADS_SECURE_AUTHENTICATION = 1

AdminUser  = "XXXX"
AdminPswd  = "XXXX"
InputFile = "\\Server\share\userlist.txt"
DC1        = "XdomainX"
DC2         = "com"

Set FSO    = CreateObject("Scripting.FilesyStemObject")
Set oFile  = FSO.OpenTextFile(InputFile)

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.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Set openDS = GetObject("LDAP:")

Do until oFile.AtEndOfLine
   LineIn = oFile.ReadLine
   strData  = Split(LineIn, ",")
   If Ubound(strData) > 0 Then
      strData(0) = trim(strData(0))   ' current username
      strData(1) = trim(strData(1))   ' new username
      if lcase(strData(0)) = lcase(strUser) then
        objCommand.CommandText = _
          "SELECT distinguishedName FROM 'LDAP://dc=" & DC1 & ",dc=" & DC2 & "' " & "WHERE objectCategory = 'user' " & _
             "AND SAMAccountName = '" & strUser & "'"
            Set objRecordSet = objCommand.Execute
           objRecordSet.MoveFirst
           Do Until objRecordSet.EOF
              DN = objRecordSet.Fields("distinguishedName").Value
              Set usr = openDS.OpenDSObject("LDAP://" & DC1 & "." & DC2 & "/" & DN, AdminUser, AdminPswd, ADS_SECURE_AUTHENTICATION)
              usr.put "samAccountName", strData(1)
              usr.put "userPrincipalName", strData(1) & "@" & DC1 & "." & DC2
              usr.SetInfo
              usr.SetPassword "xxxxxx"
             'fso.GetFolder("F:\Users\" & strData(0)).Name = strData(1)
             'msgbox "About to rename " & strData(0) & " to " & strData(1)
             FSO.GetFolder("\\Server\USERS\" & strData(0)).Name = strData(1)
             FSO.GetFolder("\\Server\HDBackup\" & strData(0)).Name = strData(1)
           objRecordSet.MoveNext
         Loop
      End if
   End If
Loop

oFile.Close
msgbox "Processing completed at " & now()
Set usr = Nothing
Set objCommand.ActiveConnection = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Set openDS = Nothing
Set oFile = Nothing
Set FSO = Nothing
wscript.quit
0
Comment
Question by:sirbounty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 22

Accepted Solution

by:
JesterToo earned 2000 total points
ID: 13527330
I didn't see the behavior you described (at least, with the posted script... the only active msgbox is the one announcing completion... perhaps some relevant code was omitted when you posted.  Anyway, I altered the script to loop thru the text file looking for the right user and only if it is found to execute the rest of the logic (as an isolated subroutine).

If this isn't what you needed let me know.

Set WshNetwork = WScript.CreateObject("WScript.Network")
strUser = WshNetwork.UserName

Const ADS_SCOPE_SUBTREE = 2
Const ADS_SECURE_AUTHENTICATION = 1

AdminUser = "XXXX"
AdminPswd = "XXXX"
InputFile = "\\Server\share\userlist.txt"
DC1       = "XdomainX"
DC2       = "com"

Set FSO   = CreateObject("Scripting.FilesyStemObject")
Set oFile = FSO.OpenTextFile(InputFile)

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.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Set openDS = GetObject("LDAP:")

Do until oFile.AtEndOfLine
   LineIn = oFile.ReadLine
   strData = Split(LineIn, ",")
   If Ubound(strData) > 0 Then
      strData(0) = trim(strData(0))   ' current username
      strData(1) = trim(strData(1))   ' new username
      if lcase(strData(0)) = lcase(strUser) then
         ChangeUser
         Exit Do
      End If
   End If
Loop

oFile.Close
wscript.echo "Processing completed at " & now()
Set usr = Nothing
Set objCommand.ActiveConnection = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Set openDS = Nothing
Set oFile = Nothing
Set FSO = Nothing
wscript.quit

Sub ChangeUser

   objCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://dc=" & DC1 & ",dc=" & DC2 & "' " & _
                             "WHERE objectCategory = 'user' " & _
                               "AND SAMAccountName = '" & strUser & "'"
   Set objRecordSet = objCommand.Execute
   objRecordSet.MoveFirst
   Do Until objRecordSet.EOF
      DN = objRecordSet.Fields("distinguishedName").Value
      wscript.echo "DN for user " & strData(0) & " is " & DN
      Set usr = openDS.OpenDSObject("LDAP://" & DC1 & "." & DC2 & "/" & DN, AdminUser, AdminPswd, ADS_SECURE_AUTHENTICATION)
      usr.put "samAccountName", strData(1)
      usr.put "userPrincipalName", strData(1) & "@" & DC1 & "." & DC2
      usr.SetInfo
      usr.SetPassword "ok"
     'wscript.echo "About to rename " & strData(0) & " to " & strData(1)
      FSO.GetFolder("\\Server\USERS\" & strData(0)).Name = strData(1)
      FSO.GetFolder("\\Server\HDBackup\" & strData(0)).Name = strData(1)
      objRecordSet.MoveNext
   Loop

End Sub


HTH,
Lynn
0
 
LVL 67

Author Comment

by:sirbounty
ID: 13528615
I'm sorry Lynn - I should've seen that.
Sure it was shelling for each account - didn't think to check before running the process - duh... :(
5 hours sleep for the past 48 could be the problem, eh?
Glad I get next weekend off...haha.
Thanx again!
0
 
LVL 22

Expert Comment

by:JesterToo
ID: 13529354
You're welcome...

Yeah, lack of sleep will do that.  It'll make an ol' man outta ya before your time. Just look at me... grey hair, half deaf, can't see, can't remember... and I'm only 57 ;-)

Like I said, I didn't observe the problem you described.  I'm actually surprised the modified script I posted performs detectably different from yours... you already had an "if" statement checking for a match on username before any real work was attempted and mine only differs by being more "modular" and obvious. The only different functionality is to terminate immediately after the user is changed which would only keep it from reading the rest of the input file.

Anyway, I'm glad it worked out for you.  Thanx for the grade/points and get some rest!

-- Lynn
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 67

Author Comment

by:sirbounty
ID: 13558114
Finally left that horrid place, but I still have to provide remote support.
Just wanted to clarify - the multi-windows I was seeing was from a shell within VB - if I simply ran the VBS, it behaved very nicely.
Thanx again. :)
0
 
LVL 22

Expert Comment

by:JesterToo
ID: 13558567
>> Finally left that horrid place<<

Temporary vacation... or permanent?  

Thanks for the clarification... I thought I might have been overlooking something critical... I must have missed the part about "VB" & "shell".  I'm glad it worked for you.

You're welcome and good luck,
Lynn
0
 
LVL 67

Author Comment

by:sirbounty
ID: 13558593
Niether - it was a remote office I was working out of.
You know - most of the problems could probably be attributed to a crappy network.
Found out on Monday that their switch ports were all misconfigured and mislabled...can't wait till we get them on our network...
Time for my catch-up nap... :)
0
 
LVL 22

Expert Comment

by:JesterToo
ID: 13558927
I can identify with that.  Back in '91 when I started with my former employer they had a similar situation.  The "network manager" (I'm being VERY loose with that term in this case) had cabled the room with the servers, routers, switches, Northern Telecom  phone switch, and big Xerox laster printers (connected to mainframes in 4 remote cities) all sitting on raised floor.  Some of the tiles of the floor were bulging upwards from the mass of cabling below... step in the wrong place and parts of the network quit working.  The guy that hired me (he eventually became the CTO) spent an entire weekend from 6PM Friday night til 10AM Monday morning pulling out cables, labelling, rerouting and reinstalling them... a significant amount of the cabling wasn't even connected anywhere.  I came aboard just after that so I missed most of that pain.  Unfortunately, the "logical" side of the infrastructure was just about as bad as the "physical"... that's why I was brought in.  

After spending 12+ years getting the business processes and I.T. support organized, developing several new products for our 500+ clients and growing it from 150 employees to around 2000 only to see new excutive management come in for the third time and tell us how bad we were doing was more than I could take... so I quit taking it and here I am!  BTW, the CTO (my longtime friend of 30 years and immediate superior) was replaced with a new CTO, 4 Senior new VP's, 4 new Project Leaders, and a raft of "foreign contractors"... so you can imagine the workload and responsibility we were sharing.

Don't nap too long... life is moving at an ever increasing pace! :-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin Within the dynamic life of an IT administrator, we hold many information in our minds like user names, passwords, IDs, phone numbers, incomes, service tags, bills and the order from our wives to buy milk when coming back to home.…
Many of us need to configure DHCP server(s) in their environment. We can do that simply via DHCP console on server or using MMC snap-in on each computer with Administrative Tools installed in a network. But what if we have to configure many DHCP ser…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question