sentinel0
Member
I have a project that I started and initially it was not to complicated parse a spreadsheet then run a command on each item parsed. It has since grown into a really nasty mess because of my lack of true programming skills. I have a button that has ~180 lines of code in it. Most of the code flows like this if this is true and this is true and this is true allow this to happen. I have a lot more if's in there and I need to add some additional functionality to that button now but, I know the first step to that is to get some control of my flow control some how break it down it put it into a class by it's self. I got so many lines now I just don't know where to begin. Not to mention when I started writing this I was writing it in vbscript/hta and have slowly converted it over to .net. I'm going to post my production button code and my test code that is more .net friendly. With the hopes so one can please help me break down some of the flow control into something not so blah.
Production:
Testing:
I know there has to be a better way to handle the flow and break it down. Any help would be awesome. thanks
Side note: The test code works however I can't figure out how to save and close the instance of Excel that I spawn from the class that creates it.
Production:
VB.NET:
Const ForWriting = 2
Dim objFso, objExcel
Dim booleanVal As Boolean
Dim intCount As Integer
Dim intRow As Integer
Dim dtmStartTime As DateTime
Dim strDUser As String
Dim strDPassword As String
Dim strNewCname As String
Dim strOldCname As String
Dim now As DateTime = DateTime.Now
dtmStartTime = CType(now.ToString("G"), DateTime)
objExcel = CreateObject("Excel.Application")
objFso = CreateObject("Scripting.FileSystemObject")
booleanVal = True
strDUser = txtUserName.Text
strDPassword = txtPassword.Text
If mfl.ValidateCredentials(strDUser, strDPassword) = False Then
booleanVal = False
MsgBox("Incorrect Username or password entered!")
WriteLog("NetDomTool Rename Report:")
WriteLog("Runtime: " & dtmStartTime)
WriteLog("Error: Incorrect Username or password entered!")
WriteLog("Terminating application!")
WriteLog("CloseWriter")
Application.Exit()
Else
End If
If booleanVal = True Then
WriteLog("NetDomTool Rename Report:")
WriteLog("Runtime: " & dtmStartTime)
If StrFile = "" Then
booleanVal = False
MsgBox("No File Selected!")
WriteLog("Error: File passed to NetDomTool - Null")
WriteLog("Terminating application!")
WriteLog("CloseWriter")
Application.Exit()
Else
Dim intRecordCount As Integer = mfl.RecordCount(StrFile)
ProgressBar1.Maximum = intRecordCount
ProgressBar1.Step = 1
WriteLog("File passed to NetDomTool - " & StrFile)
WriteLog("Total items to process: " & intRecordCount & vbCrLf)
End If
Else
End If
'Allows processing if username and password are entered
If booleanVal = True Then
objExcel.WorkBooks.Open(StrFile)
intCount = 0
intRow = 2
Do Until objExcel.Cells(intRow, 1).Value = ""
booleanVal = True
On Error Resume Next
strOldCname = objExcel.Cells(intRow, 1).Value
strNewCname = objExcel.Cells(intRow, 2).Value
'Checks if new and old computer accounts exist before proceeding
If mfl.ValidateComputer(strOldCname) = True Then
WriteLog("PC to be renamed: " & strOldCname & " Found in AD!")
'Checks whether old machine is connectable
Dim objSWbemLocator, objSWbemServices
objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
objSWbemServices = objSWbemLocator.ConnectServer(strOldCname, "root\cimv2")
If Err.Number <> 0 Then
objExcel.Cells(intRow, 3).Value = ("PC to be renamed: " & strOldCname & " unable to communicate!")
DataGridView1.Rows.Add(strOldCname, strNewCname, objExcel.Cells(intRow, 3).Value)
WriteLog("Error: PC to be renamed: " & strOldCname & " unable to communicate!")
WriteLog("Skipping Rename!" & vbCrLf)
Err.Clear()
intCount = intCount + 1
booleanVal = False
End If
'Checks if the new computer account already exists and skips if true
If mfl.ValidateComputer(strNewCname) = True Then
objExcel.Cells(intRow, 3).Value = ("New PC Name: " & strNewCname & " already exists!")
DataGridView1.Rows.Add(strOldCname, strNewCname, objExcel.Cells(intRow, 3).Value)
WriteLog("Error: New PC Name: " & strNewCname & " already exists!")
WriteLog("Skipping Rename!")
booleanVal = False
intCount = intCount + 1
End If
'Checks if old computer account exists and skips if false
ElseIf mfl.ValidateComputer(strOldCname) = False Then
booleanVal = False
objExcel.Cells(intRow, 3).Value = ("PC to be renamed: " & strOldCname & " Not Found in Domain!")
DataGridView1.Rows.Add(strOldCname, strNewCname, objExcel.Cells(intRow, 3).Value)
'InvalidPCArr = InvalidPCArr & objExcel.Cells(intRow, 1).Value & vbcrlf
WriteLog("Error: PC to be renamed: " & strOldCname & " Not Found in Domain!")
WriteLog("Skipping Rename!" & vbCrLf)
intCount = intCount + 1
End If
'Lets script proceed if it has made it through all conditionals
If booleanVal = True Then
'DataGridView1.Rows.Add(strOldCname, strNewCname)
RegTatoo(strOldCname, strDUser, strNewCname)
If objExcel.Cells(intRow, 4).Value = 1 Then
RegAutoLogin(strNewCname, strOldCname)
DataGridView1.Rows.Add(strOldCname, strNewCname, "", objExcel.Cells(intRow, 4).Value)
Else
DataGridView1.Rows.Add(strOldCname, strNewCname)
End If
'Builds NETDOM statement and executes
Dim myProcess As New Process()
myProcess.StartInfo.FileName = "NetDom.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.RedirectStandardOutput = True
Dim strCommand As String
'Dim strCmdOutput As String
'Dim oExec
If ckbForceReboot.Checked Then
myProcess.StartInfo.Arguments = "RENAMECOMPUTER " & strOldCname & " /newname:" & strNewCname & " /userd:healthall.com\" & strDUser & " /passwordd:" & strDPassword & " /force /reboot:60"
strCommand = "NETDOM RENAMECOMPUTER " & strOldCname & " /newname:" & strNewCname & " /userd:healthall.com\" & strDUser & " /passwordd:" & strDPassword & " /force /reboot:60"
WriteLog("Netdom command being executed:")
WriteLog(Replace(strCommand, strDPassword, "********"))
ElseIf ckbSchedReboot.Checked Then
' Schedules the system reboot in the PC's Task Scheduler
If Scheduler.Execute(strOldCname, DateTimePicker1.Value) = True Then
WriteLog("Success: Reboot has been scheduled for PC: " & strOldCname & " at: " & DateTimePicker1.Value().ToString)
Else
WriteLog("Error: Failed to schedule system reboot")
End If
myProcess.StartInfo.Arguments = "RENAMECOMPUTER " & strOldCname & " /newname:" & strNewCname & " /userd:healthall.com\" & strDUser & " /passwordd:" & strDPassword & " /force"
strCommand = "NETDOM RENAMECOMPUTER " & strOldCname & " /newname:" & strNewCname & " /userd:healthall.com\" & strDUser & " /passwordd:" & strDPassword & " /force"
WriteLog("Netdom command being executed:")
WriteLog(Replace(strCommand, strDPassword, "********"))
Else
myProcess.StartInfo.Arguments = "RENAMECOMPUTER " & strOldCname & " /newname:" & strNewCname & " /userd:healthall.com\" & strDUser & " /passwordd:" & strDPassword & " /force"
strCommand = "NETDOM RENAMECOMPUTER " & strOldCname & " /newname:" & strNewCname & " /userd:healthall.com\" & strDUser & " /passwordd:" & strDPassword & " /force"
WriteLog("Netdom command being executed:")
WriteLog(Replace(strCommand, strDPassword, "********"))
End If
myProcess.Start()
WriteLog(myProcess.StandardOutput.ReadToEnd())
Console.WriteLine(myProcess.StandardOutput.ReadToEnd())
myProcess.WaitForExit()
End If
intRow = intRow + 1
DataGridView1.EndEdit()
DataGridView1.Refresh()
DataGridView1.Parent.Refresh()
ProgressBar1.PerformStep()
Loop
MsgBox("NetDomTool has completed!")
WriteLog("NetDomTool has completed!" & vbCrLf)
'Saves errors occured in processing to Excel file
objExcel.ActiveWorkbook.Save()
If intCount <> 0 Then
MsgBox("There where " & intCount & " errors" & vbCrLf & "Please review the error log at " & StrPath & "PCRename_ERR_LOG-" & DtBuild & ".log")
WriteLog("Error: Total errors in log " & intCount)
Else
MsgBox("There where " & intCount & " errors")
WriteLog("Success: Total errors in log " & intCount)
End If
WriteLog("CloseWriter")
StrFile = ""
'Closes spreadsheet and terminates Excel process
objExcel.ActiveWorkbook.Close()
objExcel.Application.Quit()
objExcel = Nothing
ElseIf booleanVal = False Then
MsgBox("Please correct information!")
End If
Testing:
VB.NET:
Const ForWriting = 2
Dim booleanVal As Boolean
Dim intCount As Integer
Dim intRow As Integer
Dim dtmStartTime As DateTime
Dim strDUser As String
Dim strDPassword As String
Dim strNewCname As String
Dim strOldCname As String
Dim range As Excel.Range
range = ExWs.UsedRange
Dim rCount As Integer
Dim now As DateTime = DateTime.Now
dtmStartTime = CType(now.ToString("G"), DateTime)
booleanVal = True
strDUser = txtUserName.Text
strDPassword = txtPassword.Text
If mfl.ValidateCredentials(strDUser, strDPassword) = False Then
booleanVal = False
MsgBox("Incorrect Username or password entered!")
WriteLog("NetDomTool Rename Report:")
WriteLog("Runtime: " & dtmStartTime)
WriteLog("Error: Incorrect Username or password entered!")
WriteLog("Terminating application!")
WriteLog("CloseWriter")
Application.Exit()
Else
End If
If booleanVal = True Then
WriteLog("NetDomTool Rename Report:")
WriteLog("Runtime: " & dtmStartTime)
If StrFile = "" Then
booleanVal = False
MsgBox("No File Selected!")
WriteLog("Error: File passed to NetDomTool - Null")
WriteLog("Terminating application!")
WriteLog("CloseWriter")
Application.Exit()
Else
ProgressBar1.Maximum = intRecordCount
ProgressBar1.Step = 1
WriteLog("File passed to NetDomTool - " & StrFile)
WriteLog("Total items to process: " & intRecordCount & vbCrLf)
End If
Else
End If
'Allows processing if username and password are entered
If booleanVal = True Then
intCount = 0
intRow = 2
For rCount = 2 to range.Rows.Count
booleanVal = True
On Error Resume Next
strOldCname = CType(range(intRow,1),Excel.Range).Value.ToString
strNewCname = Ctype(range(intRow, 2),Excel.Range).Value.ToString
'Checks if new and old computer accounts exist before proceeding
If mfl.ValidateComputer(strOldCname) = True Then
WriteLog("PC to be renamed: " & strOldCname & " Found in AD!")
'Checks whether old machine is connectable
If mfl.IsPingable(strOldCname) = True then
If mfl.IsWmiConn(strOldCname) = False then
CType(range(intRow, 3), Excel.Range).Value = ("PC to be renamed: " & strOldCname & " wmi failed!")
DataGridView1.Rows.Add(strOldCname, strNewCname, CType(range(intRow, 3), Excel.Range).Value)
WriteLog("Error: PC to be renamed: " & strOldCname & " wmi failed!")
WriteLog("Skipping Rename!" & vbCrLf)
intCount = intCount + 1
booleanVal = False
End If
Else
CType(range(intRow, 3), Excel.Range).Value = ("PC to be renamed: " & strOldCname & " unable to ping!")
DataGridView1.Rows.Add(strOldCname, strNewCname, CType(range(intRow, 3), Excel.Range).Value)
WriteLog("Error: PC to be renamed: " & strOldCname & " unable to ping!")
WriteLog("Skipping Rename!" & vbCrLf)
intCount = intCount + 1
booleanVal = False
End if
'Checks if the new computer account already exists and skips if true
If mfl.ValidateComputer(strNewCname) = True Then
CType(Range(intRow, 3),Excel.Range).Value = ("New PC Name: " & strNewCname & " already exists!")
DataGridView1.Rows.Add(strOldCname, strNewCname, CType(Range(intRow, 3),Excel.Range).Value)
WriteLog("Error: New PC Name: " & strNewCname & " already exists!")
WriteLog("Skipping Rename!")
booleanVal = False
intCount = intCount + 1
End If
'Checks if old computer account exists and skips if false
ElseIf mfl.ValidateComputer(strOldCname) = False Then
booleanVal = False
CType(Range(intRow, 3),Excel.Range).Value = ("PC to be renamed: " & strOldCname & " Not Found in Domain!")
DataGridView1.Rows.Add(strOldCname, strNewCname, CType(Range(intRow, 3),Excel.Range).Value)
WriteLog("Error: PC to be renamed: " & strOldCname & " Not Found in Domain!")
WriteLog("Skipping Rename!" & vbCrLf)
intCount = intCount + 1
End If
'Lets script proceed if it has made it through all conditionals
If booleanVal = True Then
'DataGridView1.Rows.Add(strOldCname, strNewCname)
RegTatoo(strOldCname, strDUser, strNewCname)
If CType(Range(intRow, 4),Excel.Range).Value is "1" Then
RegAutoLogin(strNewCname, strOldCname)
DataGridView1.Rows.Add(strOldCname, strNewCname, "", CType(Range(intRow, 4),Excel.Range).Value)
Else
DataGridView1.Rows.Add(strOldCname, strNewCname)
End If
'Builds NETDOM statement and executes
Dim myProcess As New Process()
myProcess.StartInfo.FileName = "NetDom.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.RedirectStandardOutput = True
Dim strCommand As String
'Dim strCmdOutput As String
'Dim oExec
If ckbForceReboot.Checked Then
myProcess.StartInfo.Arguments = "RENAMECOMPUTER " & strOldCname & " /newname:" & strNewCname & " /userd:healthall.com\" & strDUser & " /passwordd:" & strDPassword & " /force /reboot:60"
strCommand = "NETDOM RENAMECOMPUTER " & strOldCname & " /newname:" & strNewCname & " /userd:healthall.com\" & strDUser & " /passwordd:" & strDPassword & " /force /reboot:60"
WriteLog("Netdom command being executed:")
WriteLog(Replace(strCommand, strDPassword, "********"))
ElseIf ckbSchedReboot.Checked Then
' Schedules the system reboot in the PC's Task Scheduler
If Scheduler.Execute(strOldCname, DateTimePicker1.Value) = True Then
WriteLog("Success: Reboot has been scheduled for PC: " & strOldCname & " at: " & DateTimePicker1.Value().ToString)
Else
WriteLog("Error: Failed to schedule system reboot")
End If
myProcess.StartInfo.Arguments = "RENAMECOMPUTER " & strOldCname & " /newname:" & strNewCname & " /userd:healthall.com\" & strDUser & " /passwordd:" & strDPassword & " /force"
strCommand = "NETDOM RENAMECOMPUTER " & strOldCname & " /newname:" & strNewCname & " /userd:healthall.com\" & strDUser & " /passwordd:" & strDPassword & " /force"
WriteLog("Netdom command being executed:")
WriteLog(Replace(strCommand, strDPassword, "********"))
Else
myProcess.StartInfo.Arguments = "RENAMECOMPUTER " & strOldCname & " /newname:" & strNewCname & " /userd:healthall.com\" & strDUser & " /passwordd:" & strDPassword & " /force"
strCommand = "NETDOM RENAMECOMPUTER " & strOldCname & " /newname:" & strNewCname & " /userd:healthall.com\" & strDUser & " /passwordd:" & strDPassword & " /force"
WriteLog("Netdom command being executed:")
WriteLog(Replace(strCommand, strDPassword, "********"))
End If
myProcess.Start()
WriteLog(myProcess.StandardOutput.ReadToEnd())
Console.WriteLine(myProcess.StandardOutput.ReadToEnd())
myProcess.WaitForExit()
End If
intRow = intRow + 1
DataGridView1.EndEdit()
DataGridView1.Refresh()
DataGridView1.Parent.Refresh()
ProgressBar1.PerformStep()
Next
MsgBox("NetDomTool has completed!")
WriteLog("NetDomTool has completed!" & vbCrLf)
'Saves errors occured in processing to Excel file
Dim objEx As New ClientValidation
objEx.Save()
If intCount <> 0 Then
MsgBox("There where " & intCount & " errors" & vbCrLf & "Please review the error log at " & StrPath & "PCRename_ERR_LOG-" & DtBuild & ".log")
WriteLog("Error: Total errors in log " & intCount)
Else
MsgBox("There where " & intCount & " errors")
WriteLog("Success: Total errors in log " & intCount)
End If
WriteLog("CloseWriter")
StrFile = ""
'Closes spreadsheet and terminates Excel process
objEx.Close()
ElseIf booleanVal = False Then
MsgBox("Please correct information!")
End If
I know there has to be a better way to handle the flow and break it down. Any help would be awesome. thanks
Side note: The test code works however I can't figure out how to save and close the instance of Excel that I spawn from the class that creates it.