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!

Need a bit of help creating log file

Status
Not open for further replies.

Sasstraliss

Programmer
Apr 7, 2009
21
AU
How can I convert the following script so that it doesn't bring up messages about the user running the process, but instead creates a log file in a location you can specifiy?

Thanks in advance!

Code:
strComputer = (InputBox(" Computer name:", "Internet Explorer (Not Responding)"))
If strComputer <> "" And strComputer <> "q" And strComputer <> "w" And strComputer <> "e" And strComputer <> "r" And strComputer <> "t" And strComputer <> "y" And strComputer <> "u" And strComputer <> "i" And strComputer <> "o" And strComputer <> "p" And strComputer <> "a" And strComputer <> "s" And strComputer <> "d" And strComputer <> "f" And strComputer <> "g" And strComputer <> "h" And strComputer <> "j" And strComputer <> "k" And strComputer <> "l" And strComputer <> "z" And strComputer <> "x" And strComputer <> "c" And strComputer <> "v" And strComputer <> "b" And strComputer <> "n" And strComputer <> "m" Then

Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Process")
For Each objProcess in colProcessList
 colProperties = objProcess.GetOwner(strNameOfUser,strUserDomain)
 Wscript.Echo "Process " & objProcess.Name & " is owned by " _
 & strUserDomain & "\" & strNameOfUser & "."
Next

End if
 
I'm too new to VBScript, I'm going to need someone to actually provide me some code. (Sorry...)
I shall try though.
 
I cannot seem to make this script work, can someone please help me with it?

Thankyou.
 
Here you go. I've commented this throughout so you can learn from it.

Code:
[green]
'==========================================================================
'
' NAME: ReportProcesses.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL   : [URL unfurl="true"]http://www.thespidersparlor.com[/URL]    
' COPYRIGHT (C) 2009 All rights reserved
' DATE  : 5/29/2009
'
' COMMENT: Creates a list of running processes.
'
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'==========================================================================
[/green]
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set WSHShell = CreateObject("Wscript.Shell")
[green]
'Get computer name[/green]
strComputer = (InputBox("Enter Computer name:", "Enter Computer To List Processes On"))[green]
'Verify computer is online[/green]
If PingStatus(strComputer) <> "Success" Then
	WScript.Echo "Computer offline or invalid computer name."
End If
[green]
'Get running processes[/green]
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Process")
For Each objProcess in colProcessList
 colProperties = objProcess.GetOwner(strNameOfUser,strUserDomain)[green]
 'Build a report of running processes[/green]
 DomSpace = MyTab(strUserDomain,0)
 Report = Report & "Process " & objProcess.Name & MyTab(objProcess.Name,4) & " is owned by " _
 & strUserDomain & "\" & strNameOfUser & MyTab(strNameOfUser,-5) & DomSpace &" Process ID:" & objProcess.ProcessId & "." & vbCrLf
Next
[green]
'Create a text file[/green]
Set ts = fso.CreateTextFile ("ProcessList" & UCase(strComputer) & ".txt", ForWriting)
[green]
'Write our report[/green]
ts.write report
[green]
'Close the file[/green]
ts.Close
[green]
'Open the file in notepad[/green]
WSHShell.Run("notepad.exe ProcessList" & UCase(strComputer) & ".txt")

[green]'The below function checks if the computer is online[/green]
Function PingStatus(strComputer)
    On Error Resume Next
    Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colPings = objWMIService.ExecQuery _
      ("SELECT * FROM Win32_PingStatus WHERE Address = '" & strComputer & "'")
    For Each objPing in colPings
        Select Case objPing.StatusCode
            Case 0 PingStatus = "Success"
            Case Else PingStatus = "Failure"
        End Select
    Next
End Function
[green]
'The below function adds extra space to give us nice columns in the report.[/green]
Function MyTab(TextString,ExtraSpace)
	On Error Resume Next
	Offset = Len(TextString)
	Spaces = 24-Offset+ExtraSpace
	If Err.Number <> 0 Then
		MyTab = Space(24+ExtraSpace)
		Err.Clear
	Else
		MyTab = Space(Spaces)
	End If
End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
In every place where you currently use WSCript.Echo, instead write to a file. The link that PHV provided shows you how to write to a file. Try some code and post back here with details if you cannot get it to work.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top