Roebuck1967
asked on
Would like to add some options to VB script
Need scriot to - record a successfull ping so I know that computer name was changed.
Dim objExcel, strPathExcel, objFile
Set objExcel = CreateObject("Excel.Applic ation")
strPathExcel = "\\DS\ADLookup\ADLookupR.x ls"
objExcel.Workbooks.open strPathExcel
Set objSheet = objExcel.ActiveWorkbook.Wo rksheets(1 )
Set SH = CreateObject("WScript.Shel l")
row = 1
newName = ""
Do While Trim(objSheet.cells(row, 1).Value) <> ""
CName = objSheet.cells(row, 1)
objSheet.cells(row, 6) = "CHANGED"
objSheet.cells(row, 7) = Trim(CStr(Now))
newName = Trim(objSheet.cells(row, 5).Value)
SH.Run "RENCOMP " + CName + " " + newName, 0, true
a = MsgBox("Changed " + CName + " to " + newName + "!", vbInformation)
row = row + 1
Loop
objExcel.ActiveWorkbook.Sa ve
objExcel.ActiveWorkbook.Cl ose
Dim objExcel, strPathExcel, objFile
Set objExcel = CreateObject("Excel.Applic
strPathExcel = "\\DS\ADLookup\ADLookupR.x
objExcel.Workbooks.open strPathExcel
Set objSheet = objExcel.ActiveWorkbook.Wo
Set SH = CreateObject("WScript.Shel
row = 1
newName = ""
Do While Trim(objSheet.cells(row, 1).Value) <> ""
CName = objSheet.cells(row, 1)
objSheet.cells(row, 6) = "CHANGED"
objSheet.cells(row, 7) = Trim(CStr(Now))
newName = Trim(objSheet.cells(row, 5).Value)
SH.Run "RENCOMP " + CName + " " + newName, 0, true
a = MsgBox("Changed " + CName + " to " + newName + "!", vbInformation)
row = row + 1
Loop
objExcel.ActiveWorkbook.Sa
objExcel.ActiveWorkbook.Cl
Perhaps you would like this better. It still logs out to the text file but it also updates columns F and G in the spreadsheet:
-------------------------- ---------- ---------- ---------- -----
Dim objExcel, strPathExcel, objFile
Set objExcel = CreateObject("Excel.Applic ation")
strPathExcel = "\\DS\ADLookup\ADLookupR.x ls"
objExcel.Workbooks.open strPathExcel
Set objSheet = objExcel.ActiveWorkbook.Wo rksheets(1 )
Set SH = CreateObject("WScript.Shel l")
row = 1
newName = ""
Do While Trim(objSheet.cells(row, 1).Value) <> ""
CName = objSheet.cells(row, 1)
newName = Trim(objSheet.cells(row, 5).Value)
SH.Run "RENCOMP " + CName + " " + newName, 1, true
a = MsgBox("Ran RENCOMP for " + CName + " to change name to " + newName + "!", vbInformation)
row = row + 1
Loop
Dim fso, ts, fso2, ts2
Const ForWriting = 8
Const ForReading = 1
Set fso = CreateObject("Scripting.Fi leSystemOb ject")
Set fso2 = CreateObject("Scripting.Fi leSystemOb ject")
'Temporary file path
Set ts = fso.OpenTextFile("c:\temp. tmp", ForReading, True)
'Log file path *******
Set ts2 = fso2.OpenTextFile("c:\logf ile.txt", ForWriting, True)
row = 1
Do While Trim(objSheet.cells(row, 1).Value) <> ""
newName = Trim(objSheet.cells(row, 5).Value)
SH.Run "cmd /c ping " + newName + " >c:\temp.tmp", 0, True
a = ts.ReadLine()
If InStr(a, "Ping request could not find") > 0 Then
ts2.WriteLine ("Computer " + newName + " was NOT be changed! " + CStr(Now))
objSheet.cells(row, 6) = ""
objSheet.cells(row, 7) = ""
Else
ts2.WriteLine ("Computer " + newName + " was successfully changed! " + CStr(Now))
objSheet.cells(row, 6) = "CHANGED"
objSheet.cells(row, 7) = Trim(CStr(Now))
End If
row = row + 1
Loop
ts2.Close
ts.Close
objExcel.ActiveWorkbook.Sa ve
objExcel.ActiveWorkbook.Cl ose
MsgBox("Script is complete, check logfile.txt for information!")
--------------------------
Dim objExcel, strPathExcel, objFile
Set objExcel = CreateObject("Excel.Applic
strPathExcel = "\\DS\ADLookup\ADLookupR.x
objExcel.Workbooks.open strPathExcel
Set objSheet = objExcel.ActiveWorkbook.Wo
Set SH = CreateObject("WScript.Shel
row = 1
newName = ""
Do While Trim(objSheet.cells(row, 1).Value) <> ""
CName = objSheet.cells(row, 1)
newName = Trim(objSheet.cells(row, 5).Value)
SH.Run "RENCOMP " + CName + " " + newName, 1, true
a = MsgBox("Ran RENCOMP for " + CName + " to change name to " + newName + "!", vbInformation)
row = row + 1
Loop
Dim fso, ts, fso2, ts2
Const ForWriting = 8
Const ForReading = 1
Set fso = CreateObject("Scripting.Fi
Set fso2 = CreateObject("Scripting.Fi
'Temporary file path
Set ts = fso.OpenTextFile("c:\temp.
'Log file path *******
Set ts2 = fso2.OpenTextFile("c:\logf
row = 1
Do While Trim(objSheet.cells(row, 1).Value) <> ""
newName = Trim(objSheet.cells(row, 5).Value)
SH.Run "cmd /c ping " + newName + " >c:\temp.tmp", 0, True
a = ts.ReadLine()
If InStr(a, "Ping request could not find") > 0 Then
ts2.WriteLine ("Computer " + newName + " was NOT be changed! " + CStr(Now))
objSheet.cells(row, 6) = ""
objSheet.cells(row, 7) = ""
Else
ts2.WriteLine ("Computer " + newName + " was successfully changed! " + CStr(Now))
objSheet.cells(row, 6) = "CHANGED"
objSheet.cells(row, 7) = Trim(CStr(Now))
End If
row = row + 1
Loop
ts2.Close
ts.Close
objExcel.ActiveWorkbook.Sa
objExcel.ActiveWorkbook.Cl
MsgBox("Script is complete, check logfile.txt for information!")
ASKER
I will give it a shot
ASKER
How did you get the script to work without rebooting the computer?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I will give it a shot.....You are the man!
ASKER
Script works great..only thing now it is rebooting the users computer where as before is was not. Any idea?
As I said, I did nothing different that would allow a rename without a restart... Perhaps the computer was already renamed? As stated, RENCOMP restarts the computer, unless that computer has been renamed already.
ASKER
Perhaps RENCOMP was not running in the first script
(First script above) SH.Run "RENCOMP " + CName + " " + newName, 1, true
(Latest script) SH.Run "cmd /c RENCOMP " + CName + " " + newName + " >c:\temp.tmp", 1, True
(First script above) SH.Run "RENCOMP " + CName + " " + newName, 1, true
(Latest script) SH.Run "cmd /c RENCOMP " + CName + " " + newName + " >c:\temp.tmp", 1, True
It should have run fine in the first script, but regardless, I think the only way it will work is with a restart. FYI - the second run command does the same thing, but it runs RENCOMP through CMD, which allows the output from RENCOMP to be directed to c:\temp.tmp so that the script can then read it and determine if the rename worked.
ASKER
You get the Points YohanShminge - - - Just posted a new Question regarding screen saver.
I'll get right on that one...as soon as I get back to my computer! Thanks for the points!
--------------------------
Dim objExcel, strPathExcel, objFile
Set objExcel = CreateObject("Excel.Applic
strPathExcel = "\\DS\ADLookup\ADLookupR.x
objExcel.Workbooks.open strPathExcel
Set objSheet = objExcel.ActiveWorkbook.Wo
Set SH = CreateObject("WScript.Shel
row = 1
newName = ""
Do While Trim(objSheet.cells(row, 1).Value) <> ""
CName = objSheet.cells(row, 1)
newName = Trim(objSheet.cells(row, 5).Value)
SH.Run "RENCOMP " + CName + " " + newName, 1, true
a = MsgBox("Ran RENCOMP for " + CName + " to change name to " + newName + "!", vbInformation)
row = row + 1
Loop
Dim fso, ts, fso2, ts2
Const ForWriting = 8
Const ForReading = 1
Set fso = CreateObject("Scripting.Fi
Set fso2 = CreateObject("Scripting.Fi
'Temporary file path
Set ts = fso.OpenTextFile("c:\temp.
'Log file path *******
Set ts2 = fso2.OpenTextFile("c:\logf
row = 1
Do While Trim(objSheet.cells(row, 1).Value) <> ""
newName = Trim(objSheet.cells(row, 5).Value)
SH.Run "cmd /c ping " + newName + " >c:\temp.tmp", 0, True
a = ts.ReadLine()
If InStr(a, "Ping request could not find") > 0 Then
ts2.WriteLine ("Computer " + newName + " was NOT be changed! " + CStr(Now))
Else
ts2.WriteLine ("Computer " + newName + " was successfully changed! " + CStr(Now))
End If
row = row + 1
Loop
ts2.Close
ts.Close
objExcel.ActiveWorkbook.Sa
objExcel.ActiveWorkbook.Cl
MsgBox("Script is complete, check logfile.txt for information!")