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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SO CLOSE YET SO FAR HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Status
Not open for further replies.

tltechie

Technical User
Apr 25, 2011
17
US
I cant get it to log to the text file. I had it before and then i broke it into pieces and put it in a nice order so it can be followed. Now it will not work right and i know its because i reordered everything. At work i do not ahve a script writer im just using notepad and im really not that great. This is what i have so far. Also i would like to add that the file it dumps to is there a way that i can have it dump on who evers desktop who happens to be signed in while using it.












Dim objFileSystem, objOutputFile
Dim strOutputFile


strOutputFile = "c:\output.txt"
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
ForAppending = 8
Set objOutputFile = objFileSystem.OpenTextFile(strOutputFile, ForAppending, True)



RemoteMachine = InputBox("Enter the Machine Name or IP Address you wish to Query:")
Set objWMIService = GetObject("winmgmts:\\" & RemoteMachine)
Set objLocaldrive = objWMIService.Get ("Win32_LogicalDisk.DeviceID='c:'")


Set objSWbemServices = Getobject("winmgmts:\\" & RemoteMachine)
Set colServices = objSWbemServices.InstancesOf("Win32_NetworkLoginProfile")

For Each objService in colServices
STRNAME = objService.Name
Next
Y = LEN(STRNAME)
X = Y - 8
STRUNAME = MID(STRNAME, 10, X)





Const CONVERSION_FACTOR = 1048576
Const WARNING_THRESHOLD = 500




FreeMegaBytes = objLocaldrive.Freespace / CONVERSION_FACTOR

If RemoteMachine = "" Then
MsgBox ("You Must Enter A Machine Name or IP Address.") : WScript.quit
Else

End If

If FreeMegaBytes < WARNING_THRESHOLD Then
Wscript.Echo "C: is low on disk space."
ELSE
End If

Set objLocaldrive = objWMIService.Get ("Win32_LogicalDisk.DeviceID='D:'")
FreeMegaBytes1 = objLocaldrive.Freespace / CONVERSION_FACTOR

If RemoteMachine = "" Then

Else


End If

If FreeMegaBytes1 < WARNING_THRESHOLD1 Then
Wscript.Echo "D: is low on disk space."
ELSE

End If

On Error Resume Next

Set WshShell = WScript.CreateObject("WScript.Shell")
Set CommandLine = CreateObject("WScript.Shell")
Set filesys = CreateObject("Scripting.FileSystemObject")








GetObject("winmgmts:\\" & RemoteMachine & "\root\CIMV2")
Set colOperatingSystems=objWMIService.ExecQuery _
("Select * From Win32_PerfFormattedData_PerfOS_System")
For Each objOS in colOperatingSystems
intSystemUptime=Int(objOS.SystemUpTime)
TimedAt=FormatDateTime(Date(),2) &", " &FormatDateTime(Time(),4)

Next
Function UpTime(S)
M=S\60 : S=S mod 60 : H=M\60 : M=M mod 60 : D=H\24
UpTime=D &" Days, " & H MOD 24 &" Hours, " &M &" Minutes"
End Function



Set objWMIService = GetObject("winmgmts:\\" & RemoteMachine & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'")
For Each objItem in colItems
next






const HKEY_LOCAL_MACHINE = &H80000002


strKeyPath = "SYSTEM\ControlSet001\Control\Session Manager\Memory Management"
iValues = Array("string1", "string2")
Set oReg=GetObject( _
"winmgmts:{impersonationLevel=impersonate}!\\" & _
RemoteMachine & "\root\default:StdRegProv")
MultValueName = "PagingFiles"
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,_
MultValueName,iValues
For Each strValue In iValues

Next








Set oReg=GetObject( _
"winmgmts:{impersonationLevel=impersonate}!\\" &_
RemoteMachine & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\ControlSet001\Services\Tcpip\Parameters"
strvaluename = "searchlist"
oReg.GetStringValue _
HKEY_LOCAL_MACHINE,strKeyPath,strValueName,svalue



ArrComputer = ","
ArrServices = Array("Smart Card", "ActivClient Middleware Service")

For Each RemoteMachine In ArrComputer
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")
For Each objItem in colItems
For Each Service In ArrServices
If Service = objItem.DisplayName Then

End If
Next
Next
Next




Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(RemoteMachine,"root\CIMV2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkConnection",,48)

For Each objItem in colItems
StrDrivePath = StrDrivePath & "LocalName: " & objItem.LocalName & VbCrLf
StrDrivePath = StrDrivePath & "RemotePath: " & objItem.RemotePath & VbCrLf
CommandLine.run "C:\MAP_DRIVES.BAT " & UCASE(objItem.LocalName) & " " & UCASE(objItem.RemotePath) & " " & UCASE(StrPROPATH)
Next






Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=Impersonate}!\\" & RemoteMachine & "\root\cimv2")
Set ColInstalledPrinters = objWMIService.ExecQuery ("Select * From Win32_Printer")

For Each objPrinter in ColInstalledPrinters
Select Case objPrinter.PrinterStatus
Case 1
StrPrinterStatus = "Other"
Case 2
StrPrinterStatus = "Unknown"
Case 3
StrPrinterStatus = "Idle"
Case 4
StrPrinterStatus = "Printing"
Case 5
StrPrinterStatus = "Warmup"
End Select
StrPrintStatus = StrPrintStatus & "Name: " & objPrinter.Name & VbCrLf
StrPrintStatus = StrPrintStatus & "Printer Status: " & VbCrLf
StrPrintStatus = StrPrintStatus & VbCrLf


next



On Error Resume Next

If RemoteMachine = "" Then
MsgBox ("You Must Enter in a Machine Name or IP Address.") : WScript.quit
Else
Set objSWbemServices = GetObject("winmgmts:\\" & RemoteMachine)
Set colSWbemObjectSet = objSWbemServices.InstancesOf("Win32_LogicalMemoryConfiguration")

wscript.echo "RUNNING NOW"


For Each objSWbemObject in colSWbemObjectSet
objOutputFile.WriteLine date & " " & Time & VbCrLf & _
STRUNAME & VbCrLf & "Hard Drive Space" & VbCrLf & _
"Megabytes of Free Disk Space on c:\ " & Int(FreeMegaBytes) & VbCrLf & _
"Megabytes of Free Disk Space on D:\ " & Int(FreeMegaBytes1) & VBCrLF & VBCrLF & _
"Last Restarted" & vbCrLf & UpTime(intSystemUptime) & VBCrLF & VBCrLF & _
"CPU Usage" & VBCrLF & objItem.PercentProcessorTime & " %" & VBCrLF & VBCrLF & _
"Page File Set To" & VBCrLF & strValue & VBCrLF & VBCrLF & _
"DNS Search Order: " & VBCrLF & svalue & VBCrLF & VBCrLF & _
"DisplayName: " & objItem.DisplayName & " State: " & objItem.State & VbCrLf & VbCrLf & _
"NETWORK DRIVES CURRENTLY MAPPED: " & VbCrLf & VbCrLf & StrDrivePath & VbCrLf & VbCrLf & _
"PRINTERS CURRENTLY MAPPED: " & VbCrLf & VbCrLf & StrPrintStatus & VbCrLf & VbCrLf

next
end if
objOutputFile.Close

Set objFileSystem = Nothing

WScript.Quit(0)



 
NOTE: This code is a mess. It looks as though you copied examples and just pasted them randomly. Aside from seemingly having no order, there are syntax errors and duplicate code all over the place (hence, I assume, the on error resume next).

That being said, I didn't see anything that stuck out. Get rid of the "on error resume next". It's hiding any errors that may occur and makes it impossible to troubleshoot.

As far as getting the current user,
Code:
set objWMI = GetObject("winmgmts:\\" & RemoteMachine)
set colComputer = objWMI.ExecQuery("Select * from Win32_ComputerSystem")
 
for each objComputer in colComputer
    strUser = objComputer.UserName
next

-Geates



"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
And for the desktop:
Code:
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
 
I worked on each script seperately and combined them all to make on large one. I am not good at scripting but ive learned this much in about a month.
 
Ive gone so far how would i clean it up to make it easy for yall to fix? i can paste in all the code that works?
 
Nice. Using a like template can help clarify functionality. I like to first define as many objects and vars as I can (scope specific). Then functions, and finally the main code. Others have other methodologies.

Code:
'****************************************
'AUTHOR:      Name
'DESCRIPTION: This script does nothing
'DATE:        May 1st, 2011
'UPDATE:      May 3rd, 2011
'****************************************

'****************************************
' GLOBAL OBJECT AND VARIABLE DEFINITION
'****************************************

dim objFSO
dim objShell
dim strMyVar

set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
set objShell = Wscript.CreateObject("WScript.Shell")

strMyVar = "some value"

'****************************************
' FUNCTIONS
'****************************************

function getPrinters
   'insert code
end function

function getMemory
   'insert code
end function

function writeLog
   'insert code
end function

'****************************************
' MAIN
'****************************************

getPrinters
getMemory
writeLog

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
There's higher level "How to program" issue here, but let's just roll with you being able to think logically for now.

Comment heads of sections, so that you remember what you were trying to do.

Make functions for "sections" of your code.
How to create a function to "block" your code:

If you have:
Code:
If RemoteMachine = "" Then
   'do something
   'your code here
Else

End If

[COLOR=green]'becomes[/color]
If RemoteMachine = "" Then
   'do something
   'your code here
End If

You do not need the ELSE portion, as this is misleading, thinking that you did at one point have separate behavors and now don't.

Lodlaiden

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 


RemoteMachine = InputBox("Enter the Machine Name or IP Address you wish to Query:")

If RemoteMachine = "" Then
MsgBox ("You Must Enter A Machine Name or IP Address.") : WScript.quit
End If

'****************************************
' GLOBAL OBJECT AND VARIABLE DEFINITION
'****************************************

const HKEY_LOCAL_MACHINE = &H80000002
Const CONVERSION_FACTOR = 1048576
Const WARNING_THRESHOLD = 500

Set objWMIService = GetObject("winmgmts:\\" & RemoteMachine)
Set colOperatingSystems=objWMIService.ExecQuery _
("Select * From Win32_PerfFormattedData_PerfOS_System")
strKeyPath = "SYSTEM\ControlSet001\Control\Session Manager\Memory Management"
iValues = Array("string1", "string2")
MultValueName = "PagingFiles"


strKeyPath1 = "SYSTEM\ControlSet001\Services\Tcpip\Parameters"
strvaluename = "searchlist"

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


Dim objFileSystem, objOutputFile
Dim strOutputFile





strOutputFile = "c:\output.txt"


Set objFileSystem = CreateObject("Scripting.fileSystemObject")
ForAppending = 8
Set objOutputFile = objFileSystem.OpenTextFile(strOutputFile, ForAppending, True)



'================
Set objWMIService = GetObject("winmgmts:\\" & RemoteMachine & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'")





GetObject("winmgmts:\\" & RemoteMachine & "\root\CIMV2")

For Each objOS in colOperatingSystems
intSystemUptime=Int(objOS.SystemUpTime)
TimedAt=FormatDateTime(Date(),2) &", " &FormatDateTime(Time(),4)
Msgbox UpTime(intSystemUptime)
Next

Function UpTime(S)
M=S\60 : S=S mod 60 : H=M\60 : M=M mod 60 : D=H\24
UpTime=D &" Days, " & H MOD 24 &" Hours, " &M &" Minutes"
End Function




For Each objItem in colItems
Wscript.Echo objItem.PercentProcessorTime & " %"
next


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



oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,_
MultValueName,iValues
For Each strValue In iValues
WScript.echo strValue
Next


oReg.GetStringValue _
HKEY_LOCAL_MACHINE,strKeyPath1,strValueName,svalue
WScript.Echo "DNS Search Order: " & svalue



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


Set objWMIService = GetObject("winmgmts:\\" & remotemachine & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")


ArrServices = Array("Smart Card","Print Spooler")
For Each objItem in colItems
For Each Service In ArrServices
If Service = objItem.DisplayName Then
Wscript.echo "DisplayName: " & objItem.DisplayName & " State: " & objItem.State & VbCrLf

end if
Next
Next

'==========



Set objWMIService = GetObject("winmgmts:\\" & RemoteMachine)
Set objLocaldrive = objWMIService.Get ("Win32_LogicalDisk.DeviceID='c:'")
Set objLocaldrive = objWMIService.Get ("Win32_LogicalDisk.DeviceID='D:'")






FreeMegaBytes = objLocaldrive.Freespace / CONVERSION_FACTOR



If FreeMegaBytes < WARNING_THRESHOLD Then
Wscript.Echo "C: is low on disk space."

End If

FreeMegaBytes1 = objLocaldrive.Freespace / CONVERSION_FACTOR

If RemoteMachine = "" Then




End If

If FreeMegaBytes1 < WARNING_THRESHOLD1 Then
Wscript.Echo "D: is low on disk space."


End If



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




Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(RemoteMachine,"root\CIMV2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkConnection",,48)
For Each objItem in colItems
StrDrivePath = StrDrivePath & "LocalName: " & objItem.LocalName & VbCrLf
StrDrivePath = StrDrivePath & "RemotePath: " & objItem.RemotePath & VbCrLf

Next



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


Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=Impersonate}!\\" & RemoteMachine & "\root\cimv2")
Set ColInstalledPrinters = objWMIService.ExecQuery ("Select * From Win32_Printer")

For Each objPrinter in ColInstalledPrinters
Select Case objPrinter.PrinterStatus
Case 1
StrPrinterStatus = "Other"
Case 2
StrPrinterStatus = "Unknown"
Case 3
StrPrinterStatus = "Idle"
Case 4
StrPrinterStatus = "Printing"
Case 5
StrPrinterStatus = "Warmup"
End Select
StrPrintStatus = StrPrintStatus & "Name: " & objPrinter.Name & VbCrLf
StrPrintStatus = StrPrintStatus & "Printer Status: " & VbCrLf
StrPrintStatus = StrPrintStatus & VbCrLf


next





objOutputFile.WriteLine date & " " & Time & VbCrLf & VbCrLf & _
"Hard Drive Space" & VbCrLf & _
"Megabytes of Free Disk Space on c:\ " & Int(FreeMegaBytes) & VbCrLf & _
"Megabytes of Free Disk Space on D:\ " & Int(FreeMegaBytes1) & VBCrLF & VBCrLF & _
"Last Restarted" & vbCrLf & UpTime(intSystemUptime) & VBCrLF & VBCrLF & _
"CPU Usage" & VBCrLF & objItem.PercentProcessorTime & " %" & VBCrLF & VBCrLF & _
"Page File Set To" & VBCrLF & strValue & VBCrLF & VBCrLF & _
"DNS Search Order: " & VBCrLF & svalue & VBCrLF & VBCrLF & _
"DisplayName: " & objItem.DisplayName & " State: " & objItem.State & VbCrLf & VbCrLf & _
"NETWORK DRIVES CURRENTLY MAPPED: " & VbCrLf & VbCrLf & StrDrivePath & VbCrLf & VbCrLf & _
"PRINTERS CURRENTLY MAPPED: " & VbCrLf & VbCrLf & StrPrintStatus & VbCrLf & VbCrLf























 
line 198
char 6
object required objitem

 
Which line is 198?

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
tltechie (3 May 11 9:22) said:
im just using notepad...
I highly recommend notepad++ for script development. It will report line numbers which makes tracking down problems easier and it has a plug in for many popular script languages (VBscript included) that will color key words much like the IDE does for the .net languages. It also offers a tabbed interface which cuts down the clutter when you have many files open and the search function is great. All this and its free too!

 
objOutputFile.WriteLine date & " " & Time & VbCrLf & VbCrLf & _
198
i use excel to get my line numbers. I know its silly but there are reasons i have to do it this way.
 
You probably that each section has a similar flow: Create an object, run a WMI query, use a FOR/EACH statement to scroll through the collection returned, then output the information you want.

Now look at the section around line 198... you have the last part (output the information), but none of the other parts... Maybe an errant copy/paste?
 
Decent flow example similar to what you are trying to do.
-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
= trim(yourProgram)

-Geates


"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top