Hello, I'm trying to write a script that will look at each computer on the network and pull the Username and Machinename entries in the registry and compare them. From there it will write to one of two files dependent on if it's a match or not.
The purpose of this script is;
1) To learn scripting
2) To look at a file and see which Machines have a different
name than the machines' users.
I was hoping that the 'IntCmpVal' variable would help get past non-windows machines and invalid IP's, but it's not working very cleanly.
What I would like is to first run a check to see if a machine is a windows box, or even a computer and then go into the main script, but I don't know a good way to do this.
Here's the script, ignore any wordwrapping that the forum put in:
'on error resume next
HKLM = &H80000002 '*******5 LINES TO POINT AT REGISTRY KEYS
strUser = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
strMachine = "system\CurrentControlSet\Control\ComputerName\ComputerName"
strUserValueName = "DefaultUserName"
strMachineValueName = "ComputerName"
strComputer = "."
ForWriting = "2"
'*******Pointer For File, Open To Write
wmiRoot = ("winmgmts:\\" & strComputer & "\root\cimv2")
wmiQuery = ("Select SystemDrive From Win32_OperatingSystem")
Set objWMI = GetObject(wmiRoot)
Set colItems = objwmi.ExecQuery(wmiQuery)
For Each objItem in colItems
strDrive = objItem.systemdrive
Next
FileMatch = strDrive & "\Documents and Settings\crlove\Desktop\VBS\Match.txt"
FileUnMatch = strDrive & "\Documents and Settings\crlove\Desktop\VBS\UnMatch.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFileMatch = ObjFSO.OpenTextFile(FileMatch, ForWriting, True)
Set ObjFileUnMatch = ObjFSO.OpenTextFile(FileUnMatch, ForWriting, True)
For i = 8 to 254
strComputer = "192.0.0." & i
wmiRoot = ("winmgmts:\\" & strComputer & "\root\default:stdRegProv")
set objReg = GetObject(wmiRoot)
objReg.GetStringValue HKLM,strUser,strUserValueName,UserName
wscript.echo UserName
objReg.GetStringValue HKLM,strMachine,strMachineValueName,MachineName
wscript.echo MachineName
IntCmpVal = StrComp(UserName, MachineName, 1)
MachineLen = Len(MachineName)
wscript.echo strComputer
If MachineLen = 0 then 'If length is 0 then assume the address isn't used
wscript.echo "*****Invalid IP Address*****"
ElseIf IntCmpVal = 0 then 'If Var returns 0 there's a match, if 1 or -1 there's no match
MatchFile
ElseIf IntCmpVal = 1 then
UnMatch
ElseIf IntCmpVal = -1 then
UnMatch
Else
UnMatch
End If
i = i + 1
Next
wscript.echo "*****Script Complete*****"
Sub MatchFile
ObjFileMatch.WriteLine(UserName & ", " & MachineName & ", " & strComputer)
End Sub
Sub UnMatch
ObjFileUnMatch.WriteLine(UserName & ", " & MachineName & ", " & strComputer)
End Sub
The purpose of this script is;
1) To learn scripting
2) To look at a file and see which Machines have a different
name than the machines' users.
I was hoping that the 'IntCmpVal' variable would help get past non-windows machines and invalid IP's, but it's not working very cleanly.
What I would like is to first run a check to see if a machine is a windows box, or even a computer and then go into the main script, but I don't know a good way to do this.
Here's the script, ignore any wordwrapping that the forum put in:
'on error resume next
HKLM = &H80000002 '*******5 LINES TO POINT AT REGISTRY KEYS
strUser = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
strMachine = "system\CurrentControlSet\Control\ComputerName\ComputerName"
strUserValueName = "DefaultUserName"
strMachineValueName = "ComputerName"
strComputer = "."
ForWriting = "2"
'*******Pointer For File, Open To Write
wmiRoot = ("winmgmts:\\" & strComputer & "\root\cimv2")
wmiQuery = ("Select SystemDrive From Win32_OperatingSystem")
Set objWMI = GetObject(wmiRoot)
Set colItems = objwmi.ExecQuery(wmiQuery)
For Each objItem in colItems
strDrive = objItem.systemdrive
Next
FileMatch = strDrive & "\Documents and Settings\crlove\Desktop\VBS\Match.txt"
FileUnMatch = strDrive & "\Documents and Settings\crlove\Desktop\VBS\UnMatch.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFileMatch = ObjFSO.OpenTextFile(FileMatch, ForWriting, True)
Set ObjFileUnMatch = ObjFSO.OpenTextFile(FileUnMatch, ForWriting, True)
For i = 8 to 254
strComputer = "192.0.0." & i
wmiRoot = ("winmgmts:\\" & strComputer & "\root\default:stdRegProv")
set objReg = GetObject(wmiRoot)
objReg.GetStringValue HKLM,strUser,strUserValueName,UserName
wscript.echo UserName
objReg.GetStringValue HKLM,strMachine,strMachineValueName,MachineName
wscript.echo MachineName
IntCmpVal = StrComp(UserName, MachineName, 1)
MachineLen = Len(MachineName)
wscript.echo strComputer
If MachineLen = 0 then 'If length is 0 then assume the address isn't used
wscript.echo "*****Invalid IP Address*****"
ElseIf IntCmpVal = 0 then 'If Var returns 0 there's a match, if 1 or -1 there's no match
MatchFile
ElseIf IntCmpVal = 1 then
UnMatch
ElseIf IntCmpVal = -1 then
UnMatch
Else
UnMatch
End If
i = i + 1
Next
wscript.echo "*****Script Complete*****"
Sub MatchFile
ObjFileMatch.WriteLine(UserName & ", " & MachineName & ", " & strComputer)
End Sub
Sub UnMatch
ObjFileUnMatch.WriteLine(UserName & ", " & MachineName & ", " & strComputer)
End Sub