Re: Memory output format



Rich,
below is my script with your scritpt in the middle. I cannot seem to
get the description to populate for each server. the Samaccount shows,
but not the description. Any help would be great.

Thanks
Rick

Option Explicit
'On Error Resume Next
Dim arrTxtArray()
Dim SourceFile, OutputFile, OutputFile1
Dim strNextLine, strComputer, strPingResults
Dim objTextFile, objFile, objFile1, objFSO, objScriptExec
Dim objWMIService, colItems, objItem
Dim strComputerName, strComputerModel, strComputerManufacturer
Dim strComputerMemory, strComputerProcessors, strCpuSpeed, strNetworkIP
Dim strOsCaption, strnetworkDescription
Dim dtmSystemUptime, dtmLastBootUpTime
Dim colOperatingSystems, objOS, strCpu, strTotalPhysicalMemory
Dim objRecordSet, objConnection, objCommand
Dim arrDesc, strLine, strDescription, strName
Dim intSize, i
Dim oWshShell : Set oWshShell = CreateObject("WScript.Shell")

Const ForReading = 1
Const ForWriting = 2

intSize = 0



'ADO Headers
'==========================================================================

Const ADS_SCOPE_SUBTREE = 2


Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"


Set objCOmmand.ActiveConnection = objConnection

'==========================================================================


'Files to read and write to
'==========================================================================
SourceFile = ".\txt\SystemInfoInput.txt"
OutputFile = ".\txt\SystemInfoOutputUp.txt"
OutputFile1 = ".\txt\SystemInfoInputDown.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (SourceFile, ForReading)
Set objFile = objFSO.CreateTextFile(OutputFile)
objFile.Close
Set objFile1 = objFSO.CreateTextFile(OutputFile1)
objFile1.Close

Set objFile = objFSO.OpenTextFile(OutputFile, ForWriting)
Set objFile1 = objFSO.OpenTextFile(OutputFile1, ForWriting)
'==========================================================================

'Writes headers to output file
'====================================================================================
objfile.WriteLine "Network Name" & ";" & "ModelName" & ";" & "CPU" &
";" &_
"Memory" & ";" & "Network Description" & ";" & "Manufacturer" & ";" &
"OS Version"
'====================================================================================

' Read and Search Input file
'==========================================================================
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
ReDim Preserve arrTxtArray(intSize) 'Resize the array
arrTxtArray(intSize) = strNextLine
intSize = intSize +1
Loop
objTextFile.close
'==========================================================================

'Pings systems in SystemInfoInput.txt
'==========================================================================
For i = LBound(arrTxtArray) To UBound(arrTxtArray)
strcomputer = arrTxtArray(i)
Set objScriptExec = oWshShell.Exec("ping -n 2 -w 1000 " & strcomputer)
strPingResults = LCase(objScriptExec.StdOut.ReadAll)
'==========================================================================

'Finds Computers that are UP and writes them to SystemInfoOutputUp.txt
'==========================================================================
If InStr(strPingResults, "ttl") Then

'Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\cimv2")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _
& strComputer & "\root\cimv2")

' Sets Name, Model, Manufacturer and Memory to Variables
'==========================================================================
Set colItems = objWMIService.ExecQuery("Select * from
Win32_ComputerSystem",,48)
For Each objItem in colItems
strComputerName = objItem.Name
strComputerModel = objItem.Model
strComputerManufacturer = objItem.Manufacturer
' strComputerProcessors = objItem.NumberOfProcessors


Next
'==========================================================================


' Collects Computer Description
'==========================================================================

objCommand.CommandText = _
"Select Name, sAMAccountName, operatingSystem, description " _
& "from 'LDAP://DC=mydomain,DC=com' " _
& "Where objectCategory='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst


Do Until objRecordSet.EOF

'strComputerName = objRecordSet.Fields("Name").Value
strName = objRecordSet.Fields("sAMAccountName").Value
arrDesc = objRecordSet.Fields("description").Value

If IsNull(arrDesc) Then
strnetworkDescription = ""
Else
For Each strLine In arrDesc
strnetworkDescription = strLine


Next
End If

objRecordSet.MoveNext
Loop
objRecordSet.Close
'objConnection.Close

'==========================================================================


' Sets TotalPhysicalMemory to a Variable and Formats with ","
'==========================================================================
Set colItems = objWMIService.ExecQuery("Select * from
Win32_ComputerSystem",,48)


For Each objItem in colItems
strTotalPhysicalMemory = objItem.TotalPhysicalMemory
strComputerMemory = FormatNumber(strTotalPhysicalMemory, 0)

Next
'==========================================================================


' Sets OS to a Variable
'==========================================================================
Set colItems = objWMIService.ExecQuery("Select * from
Win32_OperatingSystem",,48)
For Each objItem in colItems
strOsCaption = objItem.Caption

Next
'==========================================================================


' Sets CurrentClockSpeed to a Variable if 5 or greater then will put
' decimal in front and use the number, if less then 5 will use 1st 2
characters for display
'==========================================================================

Set colItems = objWMIService.ExecQuery("Select * from
Win32_Processor",,48)
For Each objItem in colItems
strCpu = objItem.CurrentClockSpeed

If Left(strCpu,1) >=5 then
strCpuSpeed = "." & strCpu
else
strCpuSpeed = Left(strCpu,1)& "." & mid(strCpu,2,1)
End if

Next
'==========================================================================



WScript.Echo "strComputerName :" & strComputerName
WScript.Echo "strnetworkDescription :" & strnetworkDescription

'Writes to SystemInfoOutputUp.txt
'==========================================================================
objFile.WriteLine strComputerName & ";" & strComputerModel & ";" &
strCpuSpeed &_
";" & strComputerMemory & ";" & strnetworkDescription & ";" & _
strComputerManufacturer & ";" & strOsCaption

'==========================================================================

'Finds Computers that are DOWN and writes them to file
'==========================================================================
Else
WScript.Echo strcomputer & " DOWN!"
objFile1.WriteLine strcomputer
'==========================================================================
End If


Next





=============================================================

Richard Mueller wrote:
Because ADO returns description as an array, you still need the "For Each"
loop to retrieve the value. And you need to set strDescription to a blank
string if the array is Null, or you will retain the value from the previous
computer. All values must be retrieved in the "Do Until" loop, so you get
new values for each computer. If I understand, the following should work:
===================
Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select Name, sAMAccountName, operatingSystem, description " _
& "from 'LDAP://DC=<mydomain>,DC=com' " _
& "Where objectCategory='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
Wscript.Echo "Computer OS: " &
objRecordSet.Fields("operatingSystem").Value

strName = objRecordSet.Fields("sAMAccountName").Value
arrDesc = objRecordSet.Fields("description").Value
If IsNull(arrDesc) Then
strDescription = ""
Else
For Each strLine In arrDesc
strDescription = strLine
Next
End If
Wscript.Echo "NetBIOS Name: " & strName
Wscript.Echo "Description: " & strDescription
objRecordSet.MoveNext
Loop
objRecordSet.Close
objConnection.Close

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net

"Rick" <drummer10980@xxxxxxxxx> wrote in message
news:1165583831.263805.53630@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Richard,
I am using your script in the middle of a script pulling wmi info from
a list of servers in a text file. I put this script in it and took out
the Do loop
for the arrdesc. this gives me the description of the first server in
AD. how do I zero in on the current server in my list that I am
collecting information on and writing out to a file.

thanks again
Rick

Richard Mueller wrote:
Rick,

The error is raised when the program attempts to retrieve the value of the
sAMAccountName attribute, but that attribute is not listed in the query as
one of the attribute values to retrieve. Also, you retrieve Name and
operatingSystem outside the "Do Until" loop. This will retrieve these
values
only for the first computer in the recordset. These statements should be
in
the "Do Until" loop. I would suggest something similar to below. I added
sAMAccountName to the list of attribute values to retrieve, and moved the
retrieval of all values into the "Do Until" loop.

' ...

objCommand.CommandText = _
"Select Name, operatingSystem, sAMAccountName, description " _
& "from 'LDAP://DC=<mydomain>,DC=com' " _
& "Where objectClass='computer'"
' ...

Do Until objRecordSet.EOF
Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
Wscript.Echo "Computer OS: " &
objRecordSet.Fields("operatingSystem").Value
Wscript.Echo "NetBIOS Name: " &
objRecordSet.Fields("sAMAccountName").Value
arrDesc = objRecordSet.Fields("description").Value
If IsNull(arrDesc) Then
strDescription = ""
Else
For Each strLine In arrDesc
strDescription = strLine
Next
End If
Wscript.Echo "Description: " & strDescription
objRecordSet.MoveNext
Loop

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net

"Rick" <drummer10980@xxxxxxxxx> wrote in message
news:1165413359.742443.248070@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Thanks Richard, that worked.

I'm having another issue with my script. the script I'm using is doing
WMI calls for different WS items. Memory, OS etc. I need to grab the
computer description from AD and found one of your scripts to do this.


I am trying to run the following script. and I get the following error.


Item cannot be found in the collection corresponding to the requested
name or ordinal.


I get the computer name and os, but is seems to fail on pulling the
computer description.


I've done a search on the error, but no luck.


any ideas?


Thanks


Rick


Const ADS_SCOPE_SUBTREE = 2


Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"


Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select Name, operatingSystem, description from
'LDAP://DC=<mydomain>,DC=com' " _
& "Where objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst


Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
Wscript.Echo "Computer OS: " &
objRecordSet.Fields("operatingSystem").Value


Do Until objRecordSet.EOF
strName = objRecordSet.Fields("sAMAccountName").Value
arrDesc = objRecordSet.Fields("description").Value
If IsNull(arrDesc) Then
strDescription = ""
Else
For Each strLine In arrDesc
strDescription = strLine
Next
End If
Wscript.Echo strName & ", " & strDescription
objRecordSet.MoveNext
Loop


Reply »




Richard Mueller wrote:
FormatNumber is a function, but you have used it as sub. Try:

For Each objItem in colItems
strTotalPhysicalMemory = objItem.TotalPhysicalMemory
WScript.Echo FormatNumber(strTotalPhysicalMemory, 0)
Next

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net

"Rick" <drummer10980@xxxxxxxxx> wrote in message
news:1165343414.537645.133610@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Richard,
thanks for the response, I'm new at this and apprieciate the help. I
am
using the following inside a script when I run the formatnumber with
parentheses I get the following error.

Cannot use parentheses when calling a Sub

I take out the parentheses, the script runs but does not format the
number. what could I look at?

Rick

Set colItems = objWMIService.ExecQuery("Select * from
Win32_ComputerSystem",,48)
For Each objItem in colItems


strTotalPhysicalMemory = objItem.TotalPhysicalMemory
FormatNumber strTotalPhysicalMemory
WScript.Echo strTotalPhysicalMemory
Richard Mueller wrote:
Rick wrote:

I'm trying to format a string in a VB Script for amount of memory
I
am
getting from a server. the string is 1073197056, I need to have it
show
as a readable number, say 1 gig, I also will have the problem for
systems with 512 and 2 gig etc. Is there a conversion I can do on
these
numbers

You can display the number with commas, which makes it much easier to
read,
with the FormatNumber function:

lngValue = 1073197056
Wscript.Echo FormatNumber(lngValue, 0)

You can also convert bytes to gigabytes by dividing by 2^30
(1,073,741,824).

Wscript.Echo FormatNumber(lngValue/2^30, 3)

The second parameter in the FormatNumber function is the number of
digits
to
display after the decimal.

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net


.



Relevant Pages

  • Re: VBScript and Out Put to Excel Worksheet
    ... removed them from the list and now the script works. ... Dim strComputer, strDN ... On Error GoTo 0 ...
    (microsoft.public.windows.server.scripting)
  • Re: Need Help..
    ... strComputer, strName, strModel, etc. works, then I can only guess the ... this part of the script is working. ... The part where we must add disconnected computers to the database i can't ... Dim strComputerName ' The Computer Name to be queried via WMI ...
    (microsoft.public.windows.server.scripting)
  • Re: Script to change password for Local Admin account question
    ... The second script reads a list of computer names. ... strComputer = objItem.sAMAccountName ... Dim inFile, outFile ... Const ForReading = 1 ...
    (microsoft.public.scripting.vbscript)
  • Lezte Anmeldung
    ... (das Script sollte in die jeweilige GPO unter Scripts ... Dim strUser, strComputer ... Dim fLog, fso, strLogFile, f1 ... ' Eigenliche Funktion aufrufen und Daten in Datei ...
    (microsoft.public.de.german.windows.server.active_directory)
  • Local Time
    ... In Script Center website, I've got the following script to get the local ... strComputer = "mantweb22" ... "For Each objItem in colItems" ...
    (microsoft.public.windows.server.scripting)