Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Your site was well structured and I found what I was looking for in about 2 minutes. I am looking forward to participating with you in the future..."

Geography

Where in the world do Tek-Tips members come from?

Using VBScript to write to Excel

ttobaben (MIS)
2 Sep 11 11:08
I have written a VBScript that will allow me to locate computer in Active Directory and change the local administrator password on those computers.  My script is successful in doing this and I have it putting the results in an Excel spreadsheet.  One sheet is labeled Password Success and one is labeled Password Fail. The problem I am running into is with my code listing the successes and failures properly.  If I have a success it will list it at the top of the spreadsheet as it should, if I have a failure it will list it on the proper spreadsheet, but the next row down.  The next success is again listed on the proper spreadsheet, but it skips a row between the last successes as there was a failure in between.  the way my code is written, it advances to the next row to write the information as it should, but it should do this on EACH SHEET not as a combined effort.  I am posting the code, if anyone could help me resolve this it would be much appreciated.  

(On a side note, I am looking to advance this code a little and have it look to see if the spreadsheet it creates exists and if it does read from the Password Fail sheet to try those machines again and move them to the Password Success side.  If the spreadsheet does not exist it will then execute the code I have written.  If someone could point me in the direction on how to accomplish this as well, that would be great!)

CODE

On Error Resume Next
Const xlExcel7 = 39
strExcelPath = "c:\PasswordChange.xls"

Set objExcel = CreateObject("Excel.Application")



objExcel.Workbooks.Add

Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
objSheet.Name = "Password Success"

Set objSheet2 = objExcel.ActiveWorkbook.Worksheets(2)
objSheet2.Name = "Password Fail"

objSheet.Cells(1, 1).Value = "Computer Name"
objSheet.Cells(1, 2).Value = "Status"
objSheet.Cells(1, 3).Value = "Date"
objSheet2.Cells(1, 1).Value = "Computer Name"
objSheet2.Cells(1, 2).Value = "Status"
objSheet2.Cells(1, 3).Value = "Date"

x=2

Set objOU = GetObject("LDAP://ou=, ou=, ou=, dc=, dc=, dc=, dc=")
objOU.Filter = Array("Computer")
 
For Each objComputer in objOU
    strComputer = objComputer.CN
 
    Set objShell = CreateObject("WScript.Shell")
    strCommand = "%comspec% /c ping -n 3 -w 1000 " & strComputer & ""
    Set objExecObject = objShell.Exec(strCommand)
 
    Do While Not objExecObject.StdOut.AtEndOfStream
        strText = objExecObject.StdOut.ReadAll()
        If Instr(strText, "Reply") > 0 Then

    Set objUser = GetObject("WinNT://" & strComputer & "/Administrator")
    objUser.SetPassword "Password"
    Wscript.Echo strComputer & " passowrd changed."
    objSheet.Cells(x, 1).Value = strComputer
    objSheet.Cells(x, 2).Value = "Password Changed"
    objSheet.Cells(x, 3).Value = Now()
  
    
         Else

    Wscript.Echo strComputer & " could not be reached."
    objSheet2.Cells(x, 1).Value = strComputer
    objSheet2.Cells(x, 2).Value = "Computer couldn't be reached, Password was NOT changed"
    objSheet2.Cells(x, 3).Value = Now()

 
End If
    x = x + 1
Loop
Set objRange = objExcel.Range("A1")
objRange.Activate

Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()

Set objRange = objExcel.Range("B1")
objRange.Activate
Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()

Set objRange = objExcel.Range("C1")
objRange.Activate

Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()


Set objRange = objExcel.Range("A1").SpecialCells(11)
Set objRange2 = objExcel.Range("C1")
Set objRange3 = objExcel.Range("A1")

      
Next

       ' Save the spreadsheet and close the workbook.
       ' Specify Excel7 File Format.
       objExcel.ActiveWorkbook.SaveAs strExcelPath, xlExcel7
       objExcel.ActiveWorkbook.Close

       ' Quit Excel.
       objExcel.Application.Quit
Geates (Programmer)
2 Sep 11 11:27
You answered your own question in your post.

How do you write to different sheets?  By having different objects, objSheet and objSheet2.  It would seem quite apparent that you should use a different counters for different sheets, x and x2.


CODE

Do While Not objExecObject.StdOut.AtEndOfStream
    strText = objExecObject.StdOut.ReadAll()
    If Instr(strText, "Reply") > 0 Then
        Set objUser = GetObject("WinNT://" & strComputer & "/Administrator")
        objUser.SetPassword "Password"
        Wscript.Echo strComputer & " passowrd changed."
        objSheet.Cells(x, 1).Value = strComputer
        objSheet.Cells(x, 2).Value = "Password Changed"
        objSheet.Cells(x, 3).Value = Now()
        x = x + 1
    Else
        Wscript.Echo strComputer & " could not be reached."
        objSheet2.Cells(x2, 1).Value = strComputer
        objSheet2.Cells(x2, 2).Value = "Computer couldn't be reached, Password was NOT changed"
        objSheet2.Cells(x2, 3).Value = Now()
        x2 = x2 + 1
    End If
Loop
-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again.  Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 

ttobaben (MIS)
2 Sep 11 11:36
You are absolutely right.  Thanks, sometimes it takes someone else to point out the obvious!
SkipVought (Programmer)
2 Sep 11 21:30



I would question why you are writing this data to 2 separate sheets?  Why can't ALL the data be in one sheet in one table?  Makes the maintenance simpler and makes the analysis and reporting of data simpler as well!

Why complicate things???

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

ttobaben (MIS)
3 Sep 11 8:18
Skip, my initial thought was to make it easier for everyone to readily identify which machines did not have their passwords changed. However if I advance my script to go back through to retry the machines that had not been done then there is no reason that it couldn't be done on one sheet/table.  You're right, I need to reevaluate the script and apply the KISS principle!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close