Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Script that works as a login script need to query on a list of machine names in a txt file.

Hi,

Script that works as a login script need to query on a list of machine names in a txt file.
The only change is query the machines whose names are in the txt file.

I tried psexec method but that did not work.

Regards
sharath
Avatar of Joseph Daly
Joseph Daly
Flag of United States of America image

What are you trying to do
Avatar of bsharath

ASKER

Sorry missed the code.
Software inventory

I dont know why the code snippet is not attaching
Pasting the code below


@echo off
:: Start getting the date and time and then into single variable - Don't Modify
for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set MM=%%j
set DD=%%k
set YYYY=%%l
set date=%%k-%%j-%%l
set dirdate=%%k-%%j-%%l
)
for /f "Tokens=1-2 delims=/ " %%i in ('time /t') do (
set tm=%%i
set ampm=%%j
)
:: End getting the date and time and then into single variable - Don't Modify
set ServerLoc=\\insm\Logs\Software\
::It's check for current date folder, if not available, it will create one
if not exist "%ServerLoc%\%dirdate%\" mkdir "%ServerLoc%\%dirdate%\"
if exist "%ServerLoc%\%dirdate%\%ComputerName%_%date%.txt" del "%ServerLoc%\%dirdate%\%ComputerName%_%date%.txt"
for /f "tokens=*" %%a in ('reg query "hklm\software\microsoft\windows\currentversion\uninstall" ^| find /i "currentversion\uninstall\"') do (
    reg query "%%a" | find /i "parentkeyname">NUL
    if errorlevel 1 (
        reg query "%%a" | find /i "systemcomponent" | find /i "0x1">NUL
        if errorlevel 1 (
            for /f "tokens=2,*" %%b in ('reg query "%%a" ^| find /i " displayname"') do echo %%c>>"%ServerLoc%\%dirdate%\%ComputerName%_%date%.txt"
        )
    )
)
sort "%ServerLoc%\%dirdate%\%ComputerName%_%date%.txt">"%ServerLoc%\%dirdate%\%ComputerName%_%date%.srt"
 
SET LastLine=
IF EXIST "%ServerLoc%\%dirdate%\%ComputerName%_%DIRdate%.one" DEL /Q "%ServerLoc%\%dirdate%\%ComputerName%_%DIRdate%.one"
FOR /F "tokens=*" %%a IN (%ServerLoc%\%dirdate%\%ComputerName%_%DIRdate%.srt) DO (
  ECHO LineRead: "%%a"
  IF NOT "%%a"=="!LastLine!" (
    ECHO %%a>>"%ServerLoc%\%dirdate%\%ComputerName%_%DIRdate%.one"
    SET LastLine=%%a
  )
)
 
move /y "%ServerLoc%\%dirdate%\%ComputerName%_%date%.one" "%ServerLoc%\%dirdate%\%ComputerName%_%date%.txt"
 
if exist "%ServerLoc%\%dirdate%\%ComputerName%_%date%.srt" del "%ServerLoc%\%dirdate%\%ComputerName%_%date%.srt"
Sharath, try using this VBS to run the above Batch file.

Regards,

Rob.
strAdmin = "domain\administrator"
strComputers = "Computers.txt"
strLogFile = "\\server\share\GetInstalledProgramsLog.txt"
strPSExec = "\\server\share\psexec.exe"
strBatchFile = "\\server\share\GetInstalledPrograms.bat"

strPassword = InputBox("Please enter the password for " & strAdmin & ":", "Password")
If strPassword = "" Then
	MsgBox "Password cannot be blank"
	WScript.Quit
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objShell = CreateObject("WScript.Shell")
strPSExec = objFSO.GetFile(strPSExec).ShortPath
Set objComputers = objFSO.OpenTextFile(strComputers, intForReading, False)
Set objLog = objFSO.CreateTextFile(strLogFile, True)
While Not objComputers.AtEndOfStream
	strComputer = objComputers.ReadLine
	If Ping(strComputer) = True Then
		strCommand = "cmd /c " & strPSExec & " -accepteula -e -u " & strAdmin & " -p " & strPassword & " \\" & strComputer & " cmd /c """ & strBatchFile & """"
		intReturn = objShell.Run(strCommand, 0, True)
		If intReturn = 0 Then
			objLog.WriteLine strComputer & ": SUCCESS"
		Else
			objLog.WriteLine strComputer & ": ERROR " & intReturn
		End If
	Else
		objLog.WriteLine strComputer & ": OFFLINE"
	End If
Wend
objComputers.Close
objLog.Close
MsgBox "Finished"
 
Function Ping(strComputer)
    Dim objShell, boolCode
    Set objShell = CreateObject("WScript.Shell")
    boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
    If boolCode = 0 Then
        Ping = True
    Else
        Ping = False
    End If
End Function

Open in new window

Thanks Rob works perfect
Can we run multiple computers at the same time...
I have 1000's of computers to query
Can we run on 50 each time?
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Rob
I am using this vbs for run a lot of batch files..
Can you help adding some code that when run asks for the computers.txt file name and then splits it into 20 of each file and then runs it seperately...
Some time back you gave me a code to split. Can this be added into the initial code
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const intPerFile = 3
strFile = "computers.txt"
intLines = 0
intStart = 1
strCommands = ""
If objFSO.FileExists(strFile) Then
	Set objFile = objFSO.OpenTextFile(strFile, intForReading, False)
	While Not objFile.AtEndOfStream
		strText = objFile.ReadLine
		If Trim(strText) <> "" Then
			If strContents = "" Then
				strContents = strText
			Else
				strContents = strContents & VbCrLf & strText
			End If
			intLines = intLines + 1
			If intLines = intPerFile Then
				strNewName = CStr(intStart) & "to" & CStr(intStart + intPerFile - 1) & ".txt"
				Set objOutput = objFSO.CreateTextFile(strNewName, True)
				objOutput.Write strContents
				objOutput.Close
				Set objOutput = Nothing
				intLines = 0
				intStart = intStart + intPerFile
				strContents = ""
				If strCommands = "" Then
					strCommands = "Start wscript.exe inventory.vbs " & strNewName & " inactive_" & strNewName & " Results" & Left(strNewName, Len(strNewName) - 3) & "xls"
				Else
					strCommands = strCommands & VbCrLf & "wscript.exe inventory.vbs " & strNewName & " inactive_" & strNewName & " Results" & Left(strNewName, Len(strNewName) - 3) & "xls"
				End If
			End If
		End If
	Wend
	objFile.Close
	If strContents <> "" Then
		intTotal = UBound(Split(strContents, VbCrLf))
		strNewName = CStr(intStart) & "to" & CStr(intStart + intTotal) & ".txt"
		Set objOutput = objFSO.CreateTextFile(strNewName, True)
		objOutput.Write strContents
		objOutput.Close
		Set objOutput = Nothing
		If strCommands = "" Then
			strCommands = "wscript.exe inventory.vbs " & strNewName & " inactive_" & strNewName & " Results" & Left(strNewName, Len(strNewName) - 3) & "xls"
		Else
			strCommands = strCommands & VbCrLf & "wscript.exe inventory.vbs " & strNewName & " inactive_" & strNewName & " Results" & Left(strNewName, Len(strNewName) - 3) & "xls"
		End If
	End If
	Set objCmdFile = objFSO.CreateTextFile("InventoryBatch.bat", True)
	objCmdFile.Write strCommands
	objCmdFile.Close
	Set objCmdFile = Nothing
	MsgBox "Done"
Else
	WScript.Echo "Could not find " & strFile
End If

Open in new window

I can't really make a generic one to create a batch file for every script, because the parameters will be different.  You can use this version to create a batch for this particular script though.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const intPerFile = 3
strFile = "computers.txt"
strVBSToRun = "C:\Temp\Temp\Test Script\Test2.vbs"
intLines = 0
intStart = 1
strCommands = ""
If objFSO.FileExists(strFile) Then
	Set objFile = objFSO.OpenTextFile(strFile, intForReading, False)
	While Not objFile.AtEndOfStream
		strText = objFile.ReadLine
		If Trim(strText) <> "" Then
			If strContents = "" Then
				strContents = strText
			Else
				strContents = strContents & VbCrLf & strText
			End If
			intLines = intLines + 1
			If intLines = intPerFile Then
				strNewName = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & CStr(intStart) & "_to_" & CStr(intStart + intPerFile - 1) & ".txt"
				Set objOutput = objFSO.CreateTextFile(strNewName, True)
				objOutput.Write strContents
				objOutput.Close
				Set objOutput = Nothing
				intLines = 0
				intStart = intStart + intPerFile
				strContents = ""
				If strCommands = "" Then
					strCommands = "wscript.exe " & objFSO.GetFile(strVBSToRun).ShortPath & " /i:""" & objFSO.GetFile(strNewName).ShortPath & """"
				Else
					strCommands = strCommands & VbCrLf & "wscript.exe " & objFSO.GetFile(strVBSToRun).ShortPath & " /i:""" & objFSO.GetFile(strNewName).ShortPath & """"
				End If
			End If
		End If
	Wend
	objFile.Close
	If strContents <> "" Then
		intTotal = UBound(Split(strContents, VbCrLf))
		strNewName = CStr(intStart) & "_to_" & CStr(intStart + intTotal) & ".txt"
		Set objOutput = objFSO.CreateTextFile(strNewName, True)
		objOutput.Write strContents
		objOutput.Close
		Set objOutput = Nothing
		If strCommands = "" Then
			strCommands = "wscript.exe " & objFSO.GetFile(strVBSToRun).ShortPath & " /i:""" & objFSO.GetFile(strNewName).ShortPath & """"
		Else
			strCommands = strCommands & VbCrLf & "wscript.exe " & objFSO.GetFile(strVBSToRun).ShortPath & " /i:""" & objFSO.GetFile(strNewName).ShortPath & """"
		End If
	End If
	Set objCmdFile = objFSO.CreateTextFile("GetInstalledAppsBatch.bat", True)
	objCmdFile.Write strCommands
	objCmdFile.Close
	Set objCmdFile = Nothing
	MsgBox "Done"
Else
	WScript.Echo "Could not find " & strFile
End If

Open in new window

Thanks Rob i can use the same for the uninstall code also right?
Not quite.  The parameters aren't the same....I'll post another version there.  Not much to change...

Rob.
Thank you Rob all fine now.. :-)