Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inventory machine hardware, etc.. 11

Status
Not open for further replies.

bmquiroz

IS-IT--Management
Sep 26, 2003
207
0
0
US
I need to inventory several workstations and servers and need a script to automate this process. I know this could be accomplished using WMI so if any one has code that they can pass on, I would appreciate it. I basically need a script that will enumerate drives, shares, get memory size, CPU speed, OS version and SP, etc..

Thanks!
 
Use

If Lcase(Left(subkey, 8)) = "kb123456" Then

To query additional machines you need to replace the machine name. Right now it is called by
strComputer = env.Item("Computername")

strComputer is then used in the WMI calls

You could set a static value like this:

Code:
strComputer="server"

Or refer to my FAQ for an example of how to read from a list. faq329-4871



I hope you find this post helpful.

Regards,

Mark
 
Here's a piece of code i found that will ping a list of computers to make sure they are online before executing the code. the computers that are offline will be put into a text file so you can do them later.

On Error Resume Next
'========================================================================================
' TITLE: Script Template
' Description: Used for creating any script that will be ran against multiple machines so
' that a countdown will display the number of machines remaining (can be used
' against single machines, but was mainly created for multiple machines),
' pings the machine to make sure it is online before proceeding, logs the
' machines that are offline.
'
' Requirements: pclist.txt for multiple machines (each machine listed on their own line)
'
' Created by: Chris Hatt
'========================================================================================
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oShell = WScript.CreateObject("WScript.Shell")
Const strScript = "Script Template" 'Title for the InputBox
Const PCL = "pclist.txt" 'List containing multiple machines to run script against
Const OFF = "offline.csv" 'Let me know if the machines are offline
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Force script to run in "CScript" mode
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
oShell.Run "cscript """ & WScript.ScriptFullName & """", 1, False
WScript.Quit
End If
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'-----------------------------------------------------------------------------
' User input to determine if script is ran against single or multiple machines.
'------------------------------------------------------------------------------
strComputer = InputBox("Which machine do you wish to run this script against?" & vbcrlf _
& vbcrlf & "Leave blank to run against " & chr(34) & PCL & chr(34),strScript,"")
If strComputer = "" Then
'--------------------------------------------------------------------------------------
'Check the date that pclist.txt was created to verify if we still want to use it or not.
'Select NO if you want to update the "pclist.txt"
'---------------------------------------------------------------------------------------
If oFS.FileExists(PCL)Then
Set file = oFS.GetFile(PCL)
myDate = file.DateLastModified
myOpt = MsgBox(PCL & " was created on " & myDate & vbcrlf & vbcrlf & "Would you still like to use it?",4,PCL & " check")
If myOpt = 7 Then
MsgBox "Script will now close.",0,"EXIT"
wscript.quit
End If
'-------------------------------------------------------------------------
'Grab the pclist and get a total count we are running the script against.
'This will give us our countdown in the Command Window as the script runs.
'-------------------------------------------------------------------------
Set file = oFS.GetFile(PCL)
Set pc = file.OpenAsTextStream(1,TristateUseDefault)
Do While pc.AtEndOfStream <> True
strComputer = Trim(pc.ReadLine)
strCount = strCount + 1
Loop
Set pc = file.OpenAsTextStream(1,TristateUseDefault)
Do While pc.AtEndOfStream <> True
strComputer = Trim(pc.ReadLine)
strCount = strCount - 1
wscript.echo strCount & " " & strComputer
Call PingMe(strComputer,strCount)
Loop
Else
'-----------------------------------------------------------
'No pclist.txt found and no machine name entered in InputBox
'-----------------------------------------------------------
wscript.echo "No " & PCL & " found!" & vbcr & "Script will now close."
wscript.quit
End If
Else
strCount = ""
Call PingMe(strComputer,strCount)
End If
wscript.echo "Finished"
'Functions
'------------------------------------------------------------------------------
Function PingMe(strComputer,strCount)
'-----------------------------------------------------------------------
'Verify that the machine is online before running the rest of the script
'-----------------------------------------------------------------------
Set cPingResults = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery("SELECT * FROM Win32_PingStatus WHERE Address = '" + strComputer + "'")
For Each oPingResult In cPingResults
If oPingResult.StatusCode = 0 Then
wscript.echo "call function required"
Else
call LogEvent(strComputer,"Offline")
End If
Next
End Function
'+++++++++++++++++++++++++++++++++++++++++++++++++
Function LogEvent(pc,msg)
'--------------------------------
'Machine is OFFLINE, create a log
'--------------------------------
Set strLogFile = oFS.OpenTextFile(OFF,8,True)
strLogFile.WriteLine pc & "," & msg
strLogFile.Close
End Function
'+++++++++++++++++++++++++++++++++++++++++++++++++
 
gmagerr, where do I call the script I need to run? Do I include it in this script as a function or call it from this script somewhere and have it run? I thought that there was a way to use different credentials to run a script without having to use the runas tool?? I know I've done it before. Is all I need to do is have my script run against 10 different servers with a local admin account that is the same on each server. This will keep me from having to run the script on each server and cut my time on patches down tremendously. Mardmac? Do you have any thoughts?

Thanks,

Ace
 
Markdmac, I tried using strComputer="server" and it puts the servers name with my local machines output?? What did I do wrong?

Thanks,

Ace
 
You need to look thorough your code. You have stuff like this:

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
".\root\default:StdRegProv")

The period before \root tells WMI to query the local machine. Replace the period with the machine name you want to query.

I hope you find this post helpful.

Regards,

Mark
 
Thanks Markdmac.....a star for you. I knew I was missing something. :) Could you tell me of a way that I can compile this as an EXE with a FREE tool so that I can use the RUNAS command so that I can run remotely? It would be much appreciated. Thank you sir.

Ace
 
Can't you RUNAS cscript.exe ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I've been trying but for some reason it doesn't like it. Let's say my script is called "script.vbs". What would the runas cscript command look like? Sorry but I've tried every combination and it looks like it is running but it isn't doing anything. I appreciate your help PHV.

Thanks,

Ace
 
I guess something like this:
runas /user:SomeUserName "cscript script.vbs //Nologo"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV...I got it. I had to use the /profile /env switches. Thanks!

Ace
 
PHV...would you happen to know how I can call this command line using a batch file? I would like it to ask for the domain/username. Something like below.

@echo off
start runas /profile /env /user:%userin% "cscript \"MS_Patch_Inventory_Check.vbs\" //NOLOGO"

But how do I get it to ask for the userin input and then wait for the input before asking for the password? I appreciate your help.

Thank you,

Ace
 
Okay....I got it but now how do I keep the cscript cmd window from displaying while the script is running? It is quite annoying. Below is my batch file to call my script and accept the username input. I appreciate anyone who can tell me this as the //B and //Nologo do not keep this from happening. Thanks in advance!

@echo off
REM ======================================================================
REM
REM NAME: Ace Williams
REM
REM
REM COMMENT: Run the MS_Patch_Inventory_Check.vbs Script
REM
REM ======================================================================
REM echo Please the domain/username or machine/username in the entry box...
:: See if I can find myself
If not exist %0 goto ERROR
:: Make the web page
type %0 | find " " | find /v " " | find /v "Not Me!" > %TEMP%\UserIn.htm
:: Make the VBS code
type %0 | find " " | find /v " " | find /v "Not Me!" > %TEMP%\UserIn.vbs
:: Run the vbs code
start /w wscript.exe %TEMP%\UserIn.vbs
:: At this point a batch file "%TEMP%\UserIn.bat" exists and you should
:: call it! If you don't call the batch file here and instead opt to
:: call it from another batch file, be sure NOT to delete it in the
:: "Clean up" code section below!
call %TEMP%\UserIn.bat
REM echo Your user name is %USERNAME%
:: Clean up
REM pause
start runas /profile /env /user:%USERNAME% "cscript \"MS_Patch_Inventory_Check.vbs\" //Nologo"
del %TEMP%\UserIn.vbs
del %TEMP%\UserIn.htm
del %TEMP%\UserIn.bat
goto DONE

:ERROR
cls
goto DONE

:HTML
:: All HTML code MUST be indented exactly four spaces.
:: NOTHING else in this batch file may be indented four spaces.
<html><body><form>Enter Username Example: domain/username or machine/username:
<br><input type=text name=username tabindex=1>
<br><input type=button
language=vbscript name=submit
value=OK onclick='submit.value="Done"'>
</form></body></html>

:VBS
:: All VBS code MUST be indented exactly five spaces.
:: NOTHING else in this batch file may be indented five spaces.
Set fs = CreateObject("Scripting.FileSystemObject")
strFile = fs.GetAbsolutePathName(fs.BuildPath(fs.GetSpecialFolder(2), "UserIn.htm"))
Set web = CreateObject("InternetExplorer.Application")
web.Offline = True
web.AddressBar = False
web.Height = 200
web.Width = 250
web.MenuBar = False
web.StatusBar = False
web.Silent = True
web.ToolBar = False
web.Navigate strFile
Do While web.Busy
Loop
On Error Resume Next
Set doc = Nothing
Do Until Not doc Is Nothing
Set doc = web.Document
Loop
doc.Forms(0).elements("username").focus
web.Visible = True
Err.Clear
Do Until doc.Forms(0).elements("submit").Value <> "OK"
Wscript.Sleep 100
If Err.Number <> 0 Then Exit Do
Loop
strFile = fs.GetAbsolutePathName(fs.BuildPath(fs.GetSpecialFolder(2), "UserIn.bat"))
Set ts = fs.OpenTextFile(strFile, 2, True)
ts.WriteLine "SET USERNAME=" & doc.Forms(0).elements("username").Value
ts.Close
web.Quit

:DONE
 
Have you tried start /B or start /min ?
What happens if you launch wscript instead of cscript ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Using wscript works except it pops up a window (cmd.exe) when displaying the html which I do not want either. I'll play around with it and post if I get it working like I want. If you think of anything else let me know and I'll try it. I appreciate it.

Thanks,

Ace
 
Okay...it looks like the problem is the runas command is creating a window to accept the password so it isn't the start or wscript/cscript commands as I thought. hmm....I'll play around with the runas command and see what I can do. Is what it does is popup and ask for the username in html and also at the same time the cmd.exe is popping up from the runas command. As soon as I type in the username the cmd.exe asks for the password. I wonder if I could just put the password to be entered in through the html window also and it would keep this from happening?

Thanks,

Ace
 
Playing with the not so reliable SendKeys method ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I'm just looking through all the additions to this post and it looks great.

acewilli, I'm not sure if I can help but the way in which I run this VBS is with a group policy with Active directory. This way it doesn't matter about permissions and it will run as a system account. The location where to write the file to is just a network share. The VBS script in store in the NETLOGON directory.

Now once all my users have logged on i have a text file based on every machine name with all the results! Fantastic!

If this helps and you don't know how to set it up just let me know.

STU
 
Hi STU, I appreciate your offer but maybe you can explain it to me in a little more detail. I work for a very large company with different groups and those groups are the only ones allowed to do anything to their servers. I only have 23 servers that I have admin access to and they are the only ones that I really need to use it for. Although, I do want to share this script with the other groups so that they can use it. My full script is in a few parts. I have two text files (serverslist.txt and MSPatches.txt). Serverslist.txt contains a list of all the servers to run the script against. MSPatches.txt contains the names of the patches to check for on the servers (returns installed or not installed). Then I have the VBS script which, thanks to Markdmac works perfectly with a few changes for what I needed. Then I have the batch file that is listed in an earlier post that allows me to run the VBS with the RUNAS command. With all that said I do not have access to change group policies (because of such as large company) but I would be interested in hearing how to do what you are saying and I think maybe it will be useful for others who visit this post. Thanks in advance!

Ace
 
For details on adding a script to a GPO just take a look at my FAQ. I give instructions for both Win2K and Win2k3.
faq329-5798


I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top