Jump to content

Query Registry, Output to txt file


chilifrei64

Recommended Posts

Here is a KiX script that you could run on each machine at user logon. This will automatically update the file, but only if the workstation's information isn't already in the file. Be warned that if two users logon at the same time the first one that gets the file open will be the only one updated. However, since the other workstation's data isn't written it'll be written the next time someone logs onto that workstation.

The script can probably be modified to delete any entries older than a set amount of time so that a workstation's information remains relatively current.

I used to use something similar to this for gathering information on our PCs at user logon. For example, workstation name, who was logging on, what date/time they logged on, IP address, Windows version, Service Pack level, etc, etc.

EDIT: I guess I could've also mentioned that KiXtart can also read from/write to native Excel and Access files. I'm pretty sure it'll even read from/write to a SQL database.


; Allow script to be exited.  Set to 'On' for testing and 'Off' for production
; 'Break Off' will automatically log the user off if they force
;   the script to exit while it's running
Break On

; Common variables
; Edit '<server name' and 'share name'
; Users will need modify access to the share
$FILE = "\\<server name\<share name\wkid.csv"
$KEY  = "HKLM\SOFTWARE\InterNetworX Systems\ICS\Configuration"


; Check for the existence of the InterNetworX Systems key
; If it exists, read the value of 'Workstation ID'
; If it doesn't exists, exit script
If KeyExist("$KEY") = 1
  $ID = ReadValue("$KEY","ReinstallMode")
Else
  GoTo End
EndIf


; Open the file for read to see if this workstation has already been checked
; If this workstation has already been included in the file, the script will exit
If Open(1,$FILE,4) = 0
  $LINE = ReadLine(1)
  While @Error = 0
     If InStr($line,@WKSTA)
        $X = Close(1)
        GoTo end
     EndIf
     $LINE = ReadLine(1)
  Loop
EndIf

; Write the data to the file if not previously written
$X = WriteLine(1,"@WKSTA,@DATE,@TIME,$ID")

; Close the file
$X = Close(1)


:end
Exit

Edited by nmX.Memnoch
Link to comment
Share on other sites


correct me if i am wrong but do I have to install this KiX software on all of my PCs before this script will run?

If anyone can figure out how to get the batch script that Yzöwl wrote to output the file this will work perfectly and I would prefer this way.

Any ideas anyone?

Link to comment
Share on other sites

correct me if i am wrong but do I have to install this KiX software on all of my PCs before this script will run?

There's no install, but the KIX32.EXE executable does need to be available. The executable doesn't have to be on the local machine, it could be on a network share. Using GPO you can set a CMD file as a startup script that has this command in it:

\\<server name>\<share name>\KIX32.EXE \\<server name>\<share name>\wkid.kix

You could also use the WKIX32 version and hide it with a /i switch:

\\<server name>\<share name>\WKIX32.EXE /i \\<server name>\<share name>\wkid.kix

Edited by nmX.Memnoch
Link to comment
Share on other sites

Ok...try this. It's been tested and you can run it from your workstation instead of on each workstation. I used part of Yzöwl's CMD script. Grab KiX from www.kixtart.org. There is no install...just drop KIX32.EXE in an empty folder with the .kix script. You can either run it from the command line or via a CMD file...either way the syntax to run it is simply:

KIX32.EXE <scriptname.kix>


; Allow script to be exited. Set to 'On' for testing and 'Off' for production
; 'Break Off' will automatically log the user off if they force
; the script to exit while it's running
Break On

If Exist("_PCNAMES.TXT")
? "Deleting old _PCNAMES.TXT file"
Del "_PCNAMES.TXT"
EndIf

If Exist("_OUTPUT.CSV")
? "Deleting old _OUTPUT.CSV file"
Del "_OUTPUT.CSV"
EndIf

? "Querying for computer names"
Shell '%COMSPEC% /C NET VIEW |FIND "\\" > _PCNAMES.TXT'

? "Reading values and creating new _OUTPUT.CSV"
If Open(1,"_PCNAMES.TXT",2) = 0
$LINE = ReadLine(1)
While @ERROR = 0
$WK = Trim(Left($LINE,20))
$Val = ReadValue("$WK\HKLM\SOFTWARE\InterNetworX Systems\ICS\Configuration","Workstation ID")
If Open(2,"_OUTPUT.CSV",5) = 0
$X = WriteLine(2,"$WK,$Val@CRLF")
$X = Close(2)
EndIf
$LINE = ReadLine(1)
Loop
Else
? "File not opened."
EndIf

Link to comment
Share on other sites

Figured I'd add my 2 cents--

Here is a .vbs script that should do what you are trying to do. I just made some modifications to script I had been using for a different but similar purpose, and I apologize if there are still some unnecessary remnants of the old version.

Steps to take:

1. Replace "xxx" in lines 14 and 27 with the name of your domain.

2. Written to capture a registry string value, if the key your looking for is a D_WORD then change "Getstringvalue" at line 130 to "Getdwordvalue" (result will be in Hex.)

3. Other than that just copy to a text editor (notepad, or my favorite, notepad2), save with a .vbs extension and run it. You will need to make a folder called "C:\Logs"--this is the default location; you will be prompted to do so if it does not exist.

' Declare the constants
Const HKLM = &H80000002 ' HKEY_LOCAL_MACHINE
Const REG_SZ = 1 ' String value in registry (Not DWORD)
Const ForReading = 1
Const ForWriting = 2

' Set File objects...
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objDictionary2 = CreateObject("Scripting.Dictionary")

' Set string variables
strDomain = "xxx" ' Your Domain
strPCsFile = "DomainPCs.txt"
strPath = "C:\logs\" ' Create this folder
strWorkstationID = "C:\logs\WorkstationID.txt"

If objFSO.FolderExists(strPath) Then
Wscript.Echo "This program will collect Workstation ID on remote compter(s)"
Else
Wscript.Echo "This program will collect Workstation ID on remote compter(s)"
WScript.Echo "Please create folder:  <c:\Logs> then click OK."
End If

' Get list of domain PCs - Using above variables.
strMbox = MsgBox("Would you like info for entire domain: xxx ?",3,"Hostname")

'an answer of yes will return a value of 6, causing script to collect domain PC info
If strMbox = 6 Then
Set objPCTXTFile = objFSO.OpenTextFile(strPath & strPCsFile, ForWriting, True)
Set objDomain = GetObject("WinNT://" & strDomain) ' Note LDAP does not work
objDomain.Filter = Array("Computer")
For Each pcObject In objDomain
objPCTXTFile.WriteLine pcObject.Name
Next
objPCTXTFile.close

Else
'an answer of no will prompt user to input name of computer to scan and create PC file
strHost = InputBox("Enter the computer you wish to get Workstation ID","Hostname"," ")
Set strFile = objfso.CreateTextFile(strPath & strPCsFile, True)
strFile.WriteLine(strHost)
strFile.Close
End If


' Read list of computers from strPCsFile into objDictionary
Set readPCFile = objFSO.OpenTextFile(strPath & strPCsFile, ForReading)
i = 0
Do Until readPCFile.AtEndOfStream
strNextLine = readPCFile.Readline
objDictionary.Add i, strNextLine
i = i + 1
Loop
readPCFile.Close

' Run the procedure defined in the Sub routine GetWorkstationID()

For each DomainPC in objDictionary
strComputer = objDictionary.Item(DomainPC)
GetWorkstationID()
Next

Set objFilesystem = Nothing

WScript.echo "Finished Scanning Network check : " & strPath

objFSO.DeleteFile(strWorkstationID)
objFSO.DeleteFile(strPath & strPCsFile)

Sub GetWorkstationID()

On Error Resume next

' WMI connection to the operating system note StdRegProv
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")

If Err <> "0" Then
Exit Sub
End If

' Registry paths which hold the WorkstationID information.
unKeyPath = "SOFTWARE\InterNetworX Systems\ICS\Configuration"
unValueName = ("Workstation ID")

' Enumerate Registry subkey paths for WorkstationID.
'objReg.EnumKey HKLM, unKeyPath
Set objTextFile1 = objFSO.OpenTextFile(strWorkstationID, ForWriting,True)
'For Each Subkey in arrSubKeys
objTextFile1.WriteLine (unKeyPath & (Enter))
'Next

' Read Registry info stored in the strWorkstationID file
Set objTextFile3 = objFSO.OpenTextFile(strWorkstationID, ForReading)

'pipe the WorkstationID paths from the WorkstationID.txt file into a second dictionary
i = 0
Do Until objTextFile3.AtEndOfStream
strNextLine = objTextFile3.Readline
objDictionary2.Add i, strNextLine
i = i + 1
Loop

' These paths are used in the filenames you see in the strPath
pcName = "SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\"
pcNameValueName = "ComputerName"
userPath = "Software\Microsoft\Windows NT\CurrentVersion\Winlogon\"
userValueName = "DefaultUserName"
objReg.GetStringValue HKLM,pcName,pcNameValueName,pcValue
objReg.GetStringValue HKLM,userPath,userValueName,userValue

' Build up the filename found in the strPath
strFileName = UserValue & "_" & "On" & "_" & PCValue & "_" & "Workstation ID" _
& year(date()) & right("0" & month(date()),2) _
& right("0" & day(date()),2) & ".txt"

' Write each PC's software info file...
Set objTextFile2 = objFSO.OpenTextFile(strPath & strFileName, ForWriting, True)
' Writing info to the corresponding Software info file...
objTextFile2.WriteLine(vbCRLF & "==============================" & vbCRLF & _
"Current Workstation ID " & vbCRLF & Time & vbCRLF & Date _
& vbCRLF & "ID for:" & "" & userValue & vbCRLF & "On System:" _
& "" & pcValue & vbCRLF & "----------------------------------------" & vbCRLF)

' First enumeration also clean up if error exists (Second enumaration omitted)
For Each objItem in objDictionary2
strKeyPath = "SOFTWARE\InterNetworX Systems\ICS\Configuration"
objReg.GetstringValue HKLM ,strKeyPath, unValueName, strValue
objTextFile2.WriteLine (strValue)
If Err Then
objDictionary2.Remove(objItem)
End If
Next

End Sub

wscript.Quit

Credit for the original script goes to Shane Rudy and Guy Thomas.

Edited by TheFlash428
Link to comment
Share on other sites

nmX.Memnoch

I downloaded the exe from the site and it appears to of executed however no output

TheFlash428

This is almost exactally what I am looking for... It runs and creates an output file.. however.. is there any way to get it to write to a single file as oppsed to a different file for each pc?

Yzöwl

you say that if the output isnt being created. there is a problem with the query.. if i run the REG QUERY \\computername\HKLM\SOFTWARE\InterNetworX Systems\ICS\Configuration" /v "Workstation ID"

from the command line i get a result.. this is the same query I have in the batch file. except with the %_NAME% instead of the computer name

when i turn echo on.. it says basically says that it found it and that it is writing _OUTPUT.TXT but nothing shows up. (for testing purposes.. share and ntfs permissions are set to full control everyone)

Link to comment
Share on other sites

nmX.Memnoch

I downloaded the exe from the site and it appears to of executed however no output

That's odd...the same exact code worked perfectly when I tested it.

Try the attachment. Just extract to an empty folder. KIX32.EXE will also need to be in either the same folder or somewhere in your path (i.e. in %WINDIR%). Run wkid.cmd to execute the script.

wkid.zip

Edited by nmX.Memnoch
Link to comment
Share on other sites

OK, let's give this a shot...

Again, copy to text editor and save with .vbs extension. Edit lines 13 and 26 for your domain.

I made the edits at home, so I didn't have a domain to test this on, but it should work (it worked when I ran it on my single PC). I'll test it on my domain tomorrow morning (GMT -4) and make additional edits if necessary.

' Declare the constants
Const HKLM = &H80000002 ' HKEY_LOCAL_MACHINE
Const REG_SZ = 1 ' String value in registry (Not DWORD)
Const ForReading = 1
Const ForWriting = 2

' Set File objects...
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objDictionary2 = CreateObject("Scripting.Dictionary")

' Set string variables
strDomain = "xxx" ' Your Domain
strPCsFile = "DomainPCs.txt"
strPath = "C:\logs\" ' Create this folder
strWorkstationID = "C:\logs\WorkstationID.txt"

If objFSO.FolderExists(strPath) Then
Wscript.Echo "This program will collect Workstation ID on remote compter(s)"
Else
Wscript.Echo "This program will collect Workstation ID on remote compter(s)"
WScript.Echo "Please create folder:  <c:\Logs> then click OK."
End If

' Get list of domain PCs - Using above variables.
strMbox = MsgBox("Would you like info for entire domain: xxx ?",3,"Hostname")

'an answer of yes will return a value of 6, causing script to collect domain PC info
If strMbox = 6 Then
Set objPCTXTFile = objFSO.OpenTextFile(strPath & strPCsFile, ForWriting, True)
Set objDomain = GetObject("WinNT://" & strDomain) ' Note LDAP does not work
objDomain.Filter = Array("Computer")
For Each pcObject In objDomain
objPCTXTFile.WriteLine pcObject.Name
Next
objPCTXTFile.close

Else
'an answer of no will prompt user to input name of computer to scan and create PC file
strHost = InputBox("Enter the computer you wish to get Workstation ID","Hostname"," ")
Set strFile = objfso.CreateTextFile(strPath & strPCsFile, True)
strFile.WriteLine(strHost)
strFile.Close
End If


' Read list of computers from strPCsFile into objDictionary
Set readPCFile = objFSO.OpenTextFile(strPath & strPCsFile, ForReading)
i = 0
Do Until readPCFile.AtEndOfStream
strNextLine = readPCFile.Readline
objDictionary.Add i, strNextLine
i = i + 1
Loop
readPCFile.Close


' Build up the filename found in the strPath
strFileName = "Workstation ID_" _
& year(date()) & right("0" & month(date()),2) _
& right("0" & day(date()),2) & ".txt"

' Write each PC's software info file...
Set objTextFile2 = objFSO.OpenTextFile(strPath & strFileName, ForWriting, True)

For each DomainPC in objDictionary
strComputer = objDictionary.Item(DomainPC)

' WMI connection to the operating system note StdRegProv
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")

' Registry paths which hold the WorkstationID information.
unKeyPath = "SOFTWARE\InterNetworX Systems\ICS\Configuration"
unValueName = ("Workstation ID")

' These paths are used in the filenames you see in the strPath
pcName = "SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\"
pcNameValueName = "ComputerName"
userPath = "Software\Microsoft\Windows NT\CurrentVersion\Winlogon\"
userValueName = "DefaultUserName"
objReg.GetStringValue HKLM,pcName,pcNameValueName,pcValue
objReg.GetStringValue HKLM,userPath,userValueName,userValue

objTextFile2.WriteLine(vbCRLF & "==============================" & vbCRLF & _
"Current Workstation ID " & vbCRLF & Time & vbCRLF & Date _
& vbCRLF & "ID for:" & "" & userValue & vbCRLF & "On System:" _
& "" & pcValue & vbCRLF & "----------------------------------------" & vbCRLF)

strKeyPath = "SOFTWARE\InterNetworX Systems\ICS\Configuration"
objReg.GetstringValue HKLM ,strKeyPath, unValueName, strValue
objTextFile2.WriteLine (strValue)

'GetWorkstationID()
Next

Set objFilesystem = Nothing

WScript.echo "Finished Scanning Network check : " & strPath

'objFSO.DeleteFile(strWorkstationID)
objFSO.DeleteFile(strPath & strPCsFile)

wscript.Quit

Edited by TheFlash428
Link to comment
Share on other sites

This one seems to have a problem if the machine is unavailable(turned off/firewalled) it returns the error on line 70 character 1 which is

Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _

This would be fine if it misses some but it errors out and stops

If i run it on a single machine that I know is online it works fine

Edited by chilifrei64
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...