Re: use text file for strComputer in VBS script

From: MFelkins (MFelkins_at_discussions.microsoft.com)
Date: 10/05/04


Date: Tue, 5 Oct 2004 08:11:05 -0700

In this piece of code, try this;

' Specify the text file of computerNames.
strFilePath = "c:\servers.txt"

' Open the file for read access.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFilePath, 1)

' Read each line of the file and set strComputer.
Do Until objFile.AtEndOfStream
  strComputer = Trim(objFile.ReadLine)
  If strComputer<> "" Then
    On Error Resume Next
    Err.Clear
  End if
Loop

You want to call strComputer here, not strComputerName. We need to do the
End If and then Loop the DO While. I am having problems now with this as it
skipps all the way down to the last line of my imput file. I will work with
it some more.

Mike

"Brian" wrote:

> "Mike" <mikef2691@comcast.net> wrote in message news:<#scMnCmqEHA.1960@TK2MSFTNGP10.phx.gbl>...
> > Yet another one;
> >
> > ' Specify the text file of computerNames.
> > strFilePath = "c:\MyFolder\NewComputers.txt"
> >
> > ' Open the file for read access.
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > Set objFile = objFSO.OpenTextFile(strFilePath, 1)
> >
> > ' Read each line of the file and set strComputerName.
> > Do Until objFile.AtEndOfStream
> > strComputerName = Trim(objFile.ReadLine)
> > If strComputerName <> "" Then
> > On Error Resume Next
> > Err.Clear
> > ' Specify the actions to be taken here
> >
> > --
> >
> >
> > ---------------------------------------------------------------------
> > "Are you still wasting your time with spam?...
> > There is a solution!"
> >
> > Protected by GIANT Company's Spam Inspector
> > The most powerful anti-spam software available.
> > http://mail.spaminspector.com
> >
> >
> > "Gerry Hickman" <gerry666uk@yahoo.co.uk> wrote in message
> > news:OMa5KhkqEHA.1644@tk2msftngp13.phx.gbl...
> > > Hi Brian,
> > >
> > >> I am trying to scan my network using this script listed below to scan
> > >> a list of servers from a text file.
> > >
> > > This is what I use. It accepts the full path to a text file with a
> > > computer name on each line. It has the added bonus of letting you "comment
> > > out" computers you don't want, by beginning the line with a semi-colon. It
> > > won't crash if the file contains blank lines. Once you get the array, you
> > > can just iterate it and do what you like to each computer.
> > >
> > > I've since moved on from this though, and now get my computer lists direct
> > > from Active Directory.
> > >
> > > function _getComputers(strFileSpec) {
> > > // Accepts name of text file containing list (full path)
> > > // Returns an array of NetBIOS computer names
> > > // Skips blank lines and lines begenning with semicolon
> > >
> > > var kForReading = 1;
> > > var kForWriting = 2;
> > > var kTristateUseDefault = -2; // ASCII on most systems
> > >
> > > var re = /^;|^\s*$/;
> > > var strLine;
> > >
> > > var fso = new ActiveXObject("Scripting.FileSystemObject");
> > >
> > > var arrCmpNames = new Array();
> > > var f = fso.GetFile(strFileSpec);
> > > var ts = f.OpenAsTextStream(kForReading, kTristateUseDefault);
> > > while (!ts.atEndOfstream) {
> > > strLine = ts.ReadLine();
> > > if(!strLine.match(re)) {
> > > arrCmpNames.push(strLine);
> > > }
> > > }
> > > ts.Close();
> > > return arrCmpNames;
> > > }
> > >
> > > --
> > > Gerry Hickman (London UK)
>
>
>
> Here is the script i have now but seems to die at line 150. I can't
> figure out why it dies when running the Subroutines and Functions.
> Any other suggestions would be greatly apprciated.
>
> 'On Error Resume Next directive is specified so that users
> 'are not bothered by potential operational errors with the script.
> 'On Error Resume Next
> 'Using both of these flags of the WbemFlagEnum enumeration
> 'makes a semisynchronous WMI call. See "Making a Semisynchronous Call"
> 'in the WMI SDK for more information.
> Const wbemFlagReturnImmediately = 16
> Const wbemFlagForwardOnly = 32
> 'WbemCimtypeEnum Enumerations used in the script
> Const wbemCimtypeUint32 = 19
> Const wbemCimtypeSint64 = 20
> Const wbemCimtypeUint64 = 21
> 'Formatting and number conversion constants
> Const HR = "----"
> Const KB = 1024
> Const MB = 1048576
> Const GB = 1073741824
>
> 'Change this to the UNC path where inventory files should be created
> 'Anyone running this script must have read and write access to the
> 'path.
> strInvFilePath = "C:\hw\"
>
> ' Specify the text file of computerNames.
> strFilePath = "c:\servers.txt"
>
> ' Open the file for read access.
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile(strFilePath, 1)
>
> ' Read each line of the file and set strComputerName.
> Do Until objFile.AtEndOfStream
> strComputerName = Trim(objFile.ReadLine)
> If strComputerName <> "" Then
> On Error Resume Next
> Err.Clear
>
>
> 'Connect to WMI
> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
> "\root\cimv2")
>
> 'Determine the OS because not all classes listed here are supported
> 'on all versions of the Windows operating systems
> strProperties = "CreationClassName,Version,CSName,Caption"
> Set objOS = objWMIService.ExecQuery _
> ("SELECT " & strProperties & " FROM Win32_OperatingSystem",_
> ,wbemFlagReturnImmediately + wbemFlagForwardOnly)
>
> For Each Setting in objOS
> strCreationClass = Setting.CreationClassName
> intVersion = Setting.Version
> strCSName = Setting.CSName
> strCaption = Setting.Caption
> Next
>
> 'Check for a file named after the computer. If it doesn't
> 'exist, then create a file that contains the computer's
> 'hardware inventory.
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> strFileName = "HrdWrInv_" & strCSName & ".txt"
> strFullName = objFSO.BuildPath(strInvFilePath, strFileName)
> If (objFSO.FileExists(strFullName)) Then WScript.Quit
> Set objFile = objFSO.CreateTextFile(strFullName)
> 'Write the Operating System name and version but nothing more.
> 'The goal is to collect hardware inventory, not software inventory
> 'and configuration information.
> objFile.WriteLine("OSInformation:")
> objFile.WriteLine("CreationClassName: " & strCreationClass)
> objFile.WriteLine("ComputerName: " & strCSName)
> objFile.WriteLine("Caption: " & strCaption)
> objFile.WriteLine("Version: " & intVersion)
>
> 'The name property is automatically returned (because it's a key
> value), so
> 'it's not absolutely necessary to specify it in strProperties
> strProperties = _
> "CreationClassName,Manufacturer,Model,Name,NumberofProcessors," & _
> "SystemType,TotalPhysicalMemory"
> QueryInstances "Win32_ComputerSystem",strProperties,"None"
> 'Page file information.
> strProperties = _
> "Name,MaximumSize"
> QueryInstances "Win32_PageFileSetting",strProperties,"None"
> 'Physical memory information.
> 'Note, total memory is aggregated and listed with win32_computersystem
> so there's
> 'no need to query the memory classes, such as
> Win32_PhysicalMemoryArray or
> 'Win32_PhysicalMemory. However, if you need to find out how much
> memory
> 'a computer will hold, it's useful to query Win32_PhysicalMemoryArray
> as
> 'shown
> strProperties = _
> "MaxCapacity,CreationClassName"
> QueryInstances "Win32_PhysicalMemoryArray",strProperties,"None"
> 'SCSI Disk controller information
> strProperties = _
> "Name,Manufacturer,Description,CreationClassName"
> QueryInstances "Win32_SCSIController",strProperties,"None"
> 'IDE Disk controller information
> strProperties = _
> "Name,Manufacturer,Description,CreationClassName"
> QueryInstances "Win32_IDEController",strProperties,"None"
> 'Phyiscal Media information
> 'Note, this class is only in XP (ver5) and the Windows
> 'Server 2003 family (ver5).
> 'Win98SE reports a version number of 4 (4.10.2222)
> If Mid(intVersion,1,3) >= 5.1 Then
> strProperties = _
> "SerialNumber,CreationClassName"
> QueryInstances "Win32_PhysicalMedia",strProperties,"None"
> End If
> 'Logical Disk information
> strProperties = _
> "DriveType,Description,DeviceID,CreationClassName"
> QueryInstances "Win32_LogicalDisk",strProperties,_
> "DriveType!=3 AND DriveType !=4"
> 'Disk drive information
> strProperties = _
> "Caption,Description,DeviceID,InterfaceType," & _
> "Manufacturer,MediaType,Model,Partitions,Size,CreationClassName"
> QueryInstances "Win32_DiskDrive",strProperties,"None"
> 'Processor information
> strProperties = _
> "Manufacturer,MaxClockSpeed,ExtClock,ProcessorType," & _
> "Revision,Version,CreationClassName"
> QueryInstances "Win32_Processor",strProperties,"None"
> 'NIC information
> strProperties = _
> "Manufacturer,MACAddress,ProductName,AdapterType,CreationClassName"
> QueryInstances "Win32_NetworkAdapter",strProperties,_
> "AdapterType='Ethernet 802.3' AND ProductName !='Packet Scheduler
> Miniport'"
> 'Monitor information
> strProperties = _
> "Description,MonitorType,CreationClassName"
> QueryInstances "Win32_DesktopMonitor",strProperties,"None"
> 'Video adapter information
> strProperties = _
> "Name,MaxRefreshRate,AdapterRAM,CreationClassName"
> QueryInstances "Win32_VideoController",strProperties,"None"
> 'Motherboard information
> strProperties = _
> "Description,Manufacturer,Product,CreationClassName"
> QueryInstances "Win32_BaseBoard",strProperties,"None"
> 'BIOS information
> strProperties = _
> "Manufacturer, Name,SoftwareElementID,SMBIOSBIOSVersion," & _
> "SMBIOSMajorVersion,SMBIOSMinorVersion,Version"
> QueryInstances "Win32_BIOS",strProperties,"None"
>
> objFile.Close
>
> '*****Subroutines and Functions*********
> Sub QueryInstances(objClass,Properties,Conditions)
> If Conditions = "None" Then
> strSelect = "Select " & Properties & " From " & objClass
> Else
> strSelect = "Select " & Properties & " From " & objClass & _
> " Where " & Conditions
> End If
> Set objClassName = _
> objWMIService.ExecQuery _
> (strSelect,,wbemFlagReturnImmediately + wbemFlagForwardOnly)
> intCounter = 0
> ' Can use the following to determine the # of items in the
> sWbemObjectSet but
> ' it's processor intensive and it means that you can't use a
> semi-synchronous
> ' call because the count property doesn't work with
> wbemFlagForwardOnly.
> ' WScript.Echo "# of items in collection: " & objClassName.Count
> For Each objComponent in objClassName
> If intCounter = 0 Then
> objFile.WriteLine(HR)
> objFile.WriteLine(Mid(objClass,7) & ":")
> End If
> For Each objProperty in objComponent.Properties_
> 'Start by verifying that there's a value in the property.
> 'If not, it isn't necessary to perform the evaluation in this
> loop.
> If ISNull(objProperty.Value) Then
> objFile.WriteLine(objProperty.Name & ": Not available")
> Else
> If objProperty.CIMType <> wbemCimtypeUint32 And _
> objProperty.CIMType <> wbemCimtypeUint64 And _
> objProperty.CIMType <> wbemCimtypeSint64 Then
> objFile.WriteLine(objProperty.Name & ": " & _
> objProperty.Value & " " & _
> GetUnits(objClass,objProperty.Name))
> Else
> strUnits = GetUnits(objClass,objProperty.Name)
> intValue = _
> SizeFormat(objProperty.Name,objProperty.Value,strUnits)
> objFile.WriteLine(objProperty.Name & ": " & intValue)
> End If
> End If
> intCounter = intCounter + 1
> Next
> If intCounter > 1 Then objFile.WriteLine(HR)
> Next
> objFile.WriteLine(HR)
> objFile.WriteLine()
> End Sub
>
> Function GetUnits(ClassName,ClassProperty)
> Set objSchemaClass = objWMIService.Get(ClassName)
> For Each objClassProperty in objSchemaClass.Properties_
> If objClassProperty.Name = ClassProperty Then
> For Each objQualifier in objClassProperty.Qualifiers_
> If LCase(objQualifier.Name) = "units" Then
> GetUnits = LCase(objQualifier.Value)
> End If
> Next
> End If
> Next
> End Function
>
> Function SizeFormat(PropertyName,RawValue,Units)
> Select Case LCase(Units)
> Case "bytes"
> If int(RawValue/GB) >= 1 Then
> SizeFormat = Round((RawValue/GB),1) & " gigabyte(s)"
> ElseIf int(RawValue/MB) >= 1 Then
> SizeFormat = int(RawValue/MB) & " megabyte(s)"
> Else
> SizeFormat = RawValue & " byte(s)"
> End If
> Case "kilobytes"
> If int(RawValue/MB) >= 1 Then
> SizeFormat = Round((RawValue/MB),1) & " gigabyte(s)"
> ElseIf int(RawValue/KB) >= 1 Then
> SizeFormat = int(RawValue/1024) & " megabyte(s)"
> Else
> SizeFormat = RawValue & " kilobyte(s)"
> End If
> Case Else
> SizeFormat = RawValue & " " & Units
> End Select
> End Function
>
>
>
> This is the line that the script dies at:
>
> Sub QueryInstances(objClass,Properties,Conditions)
>
> Thanks
>



Relevant Pages

  • Re: use text file for strComputer in VBS script
    ... ' Read each line of the file and set strComputer. ... > 'are not bothered by potential operational errors with the script. ... > ' Specify the text file of computerNames. ...
    (microsoft.public.windows.server.scripting)
  • Re: use text file for strComputer in VBS script
    ... ' Read each line of the file and set strComputer. ... > 'are not bothered by potential operational errors with the script. ... > ' Specify the text file of computerNames. ...
    (microsoft.public.win32.programmer.wmi)
  • Re: use text file for strComputer in VBS script
    ... ' Read each line of the file and set strComputer. ... > 'are not bothered by potential operational errors with the script. ... > ' Specify the text file of computerNames. ...
    (microsoft.public.scripting.vbscript)
  • Re: Finetuning: Remote Shutdown with WMI, some errors occur.
    ... Allows shutdown, poweroff, or reboot of multiple remote systems. ... - generic unhandled script compile or runtime error; ... "Specify 1 and only one shutdown action" ... Dim erNumber, erDesc ...
    (microsoft.public.windows.server.scripting)
  • Re: running scripts on multiple computers
    ... 'A basic template for reading from a file and writing the results ... ' Specify the text file of computerNames. ... >> I'm sure this is a pretty basic thing, but I'm very new at scripting. ... Is there a way to use the strComputer command so>> that the script will run on either multiple computers or on all computers> in ...
    (microsoft.public.windows.server.scripting)

Loading