I would like to monitor some services with WMI:
1. Test if the service is running
2. If not running restart it
3. If I can't restart it send an email
Could someone help me deal with this please?
strComputer = "."
srv= " WSearch, wuauserv "
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_Service where Name='srv'")
For Each objService in colItems
Wscript.Echo "Service Name: " & objService.Name & VBNewLine _
& "State: " & objService.State
if objService.State="Stopped" then
objService.StartService()
Wscript.Echo "Started service "
else
REM here the code for email if the service didn't start
end if
Next
Related
Hi Guys this script requires you to manually enter computer name to pull up Inventory.
If I can have all the computer's name in a text file that will be great.
I tried something like
strComputer = fso1.OpenTextFile("c:\Computers.txt",1)
But doesn't seems to work any idea?
Dim strIP, strSubnet, strDescription, lnX, strcomputer, objwmiservice, colitems, objitems
strComputer = InputBox ("Enter the Computer name to get its Inventory:-")
lnX = 1
' ********************************************************************************************
'Section to change a filename using timestamps
strPath = "C:\"
strMonth = DatePart("m", Now())
strDay = DatePart("d",Now())
if Len(strMonth) = 1 then
strMonth = "0" & strMonth
else
strMonth = strMonth
end if
if Len(strDay) = 1 then
strDay = "0" & strDay
else
strDay = strDay
end if
strFileName = DatePart("yyyy",Now()) & strMonth & strDay
strFileName = Replace(strFileName,":","")
' ********************************************************************************************
'Variable Declarations
Const ForAppending = 8
'Get CompName
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel = impersonate}!\\" & strComputer & "\root\cimv2")
' ********************************************************************************************
'Get Operation System & Processor Information
' ********************************************************************************************
Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor")
For Each objItem in colItems
CompName = objItem.SystemName
Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
if objFSO.FileExists(strPath & CompName & "_" & "_Inventory.txt") then
WScript.Quit
end if
'Set the file location
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strPath & CompName & "_" & "Inventory.txt", ForAppending, True)
' ********************************************************************************************
'Print HEADER
' ********************************************************************************************
objTextFile.Write "********************************************************************************************" & VBCRLF & VBCRLF
objTextFile.Write " COMPUTER INVENTORY " & VBCRLF
objTextFile.Write " DATE: " & FormatDateTime(Now(),1) & " " & VBCRLF
objTextFile.Write " TIME: " & FormatDateTime(Now(),3) & " " & VBCRLF & VBCRLF
objTextFile.Write "******************************************************************************************** " & VBCRLF & VBCRLF
' ********************************************************************************************
'Get Processor Information
' ********************************************************************************************
objTextFile.Write "COMPUTER" & VBCRLF
Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor")
For Each objItem in colItems
If lnX = 1 Then
objTextFile.Write " COMPUTER NAME: " & objItem.SystemName & VBCRLF
objTextFile.Write " PROCESSOR: " & objItem.Name & vbCrLf
lnX = lnX + 1
End If
Next
' ********************************************************************************************
'Get Computer Manufacturer and RAM details
' ********************************************************************************************
Set colSystems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each objSystems In colSystems
objTextFile.WriteLine " MANUFACTURER: " & objSystems.Manufacturer
objTextFile.WriteLine " MODEL: " & objSystems.Model
objTextFile.WriteLine " DOMAIN: " & objsystems.Domain
objTextFile.Write " RAM: " & Round (objSystems.TotalPhysicalMemory / 1048576, 0) & " MB "& VBCRLF
Next
' ********************************************************************************************
' Get the Serial number/ Service Tag of the system
' ********************************************************************************************
Set colSMBIOS = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure")
For Each objSMBIOS in colSMBIOS
objTextFile.Write " SERVICE TAG: " & objSMBIOS.SerialNumber & VBCRLF
Next
' ********************************************************************************************
'Get BIOS Information
' ********************************************************************************************
objTextFile.Write VBCRLF & "BIOS INFO" & vbCrLf
Set colBIOS = objWMIService.ExecQuery("Select * from Win32_BIOS")
For each objItem in colBIOS
objTextFile.WriteLine " BIOS MANUFACTURER: " & objItem.Manufacturer
objTextFile.WriteLine " BIOS VERSION: " & objItem.Name
Next
' ********************************************************************************************
'Get OS Information
' ********************************************************************************************
objTextFile.Write VBCRLF & "OS INFO" & vbCrLf
Set colSettings = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colSettings
objTextFile.Write " OPERATING SYSTEM: " & objOperatingSystem.Caption & " {Enter the product key}" & VBCRLF
objTextFile.Write " SERVICE PACK: " & objOperatingSystem.Caption & " Service Pack "& objOperatingSystem.ServicePackMajorVersion & "." & objOperatingSystem.ServicePackMinorVersion & VBCRLF
Next
' ********************************************************************************************
'Get Logical Disk Size and Partition Information
' ********************************************************************************************
objTextFile.Write VBCRLF & "MEMORY" & VBCRLF
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = 3")
For Each objDisk in colDisks
intFreeSpace = objDisk.FreeSpace
intTotalSpace = objDisk.Size
pctFreeSpace = intFreeSpace / intTotalSpace
objTextFile.Write objDisk.Name & "\ (" & objDisk.FileSystem & ") " & Round((objDisk.Size/1000000000),4) & " GB ("& Round((intFreeSpace/1000000000)*1.024,4) & " GB Free Space)" & VBCRLF
Next
' ********************************************************************************************
'Get NETWORK ADAPTERS information
' ********************************************************************************************
objTextFile.Write VBCRLF & "NETWORK" & VBCRLF
Set colNicConfigs = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objNicConfig In colNicConfigs
strDescription = objNicConfig.Caption
strMACAddress = objNicConfig.MACAddress
strDHCP = objNicConfig.DHCPEnabled
For Each strIPAddress In objNicConfig.IPAddress
strIP = strIPAddress 'Assign IP Address to variable
For Each strIPSubnet In objNicConfig.IPSubnet
strSubnet = strIPSubnet 'Assign Subnet to variable
Next
objTextFile.Write " NETWORK ADAPTER: " & strDescription & VBCRLF
objTextFile.Write " IP ADDRESS: " & strIP & VBCRLF
objTextFile.Write " MAC ADDRESS: " & strMACAddress & vbCrLf
objTextFile.Write " DHCP ENABLED: " & strDHCP & vbCrLf & vbCrLf
Next
Next
Set colNicConfigs = NOTHING
' ********************************************************************************************
'Get GRAPHICS ADAPTERS information
' ********************************************************************************************
objTextFile.Write VBCRLF & "GRAPHICS CARD" & VBCRLF
Set colGraphics = objWMIService.ExecQuery ("Select * from Win32_DisplayControllerConfiguration")
For Each objGraphics in colGraphics
objTextFile.WriteLine " GRAPHICS CARD: " & objGraphics.Name & VBCRLF
Next
' ********************************************************************************************
' Get the list of Installed software
' ********************************************************************************************
objTextFile.Write VBCRLF & "SOFTWARE INSTALLED" & VBCRLF
Set colSoftware = objWMIService.ExecQuery("Select * from Win32_Product")
For Each objSoftware in colSoftware
objTextFile.WriteLine objSoftware.Description
Next
'Close text file after writing logs
objTextFile.Write VbCrLf
objTextFile.Close
'Clean Up
WScript.Echo "Inventory Complete "
The .OpenTextFile() call in your
strComputer = fso1.OpenTextFile("c:\Computers.txt",1)
just opens the file, but does not read the file's content that should be assigned to strComputer. So use
strComputer = fso1.OpenTextFile("c:\Computers.txt").ReadLine()
We have powershell scripts setup for preforming offline backups. The executing of these scripts was failing due to an incorrect script execution causing powershell to prompt what user wants to do. Due to this the scripts timed out.
To detect script execution timeouts i'm trying to query the event log with a VBS script, but this allways returns 0 results.
I tried with this code:
LOG_FILE = "C:\jsco\Scripts\output.txt"
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NTLogEvent WHERE Logfile = 'Microsoft-Windows-TaskScheduler/Operational' AND EventCode = '332'")
For Each objEvent in colItems
writeLog "Category: " & objEvent.Category
writeLog "Category String: " & objEvent.CategoryString
writeLog "Computer Name: " & objEvent.ComputerName
writeLog "Data: " & objEvent.Data
writeLog "Event Code: " & objEvent.EventCode
writeLog "Event Identifier: " & objEvent.EventIdentifier
writeLog "Insertion Strings: " & objEvent.InsertionStrings
writeLog "Logfile: " & objEvent.Logfile
writeLog "Message: " & objEvent.Message
writeLog "Record Number: " & objEvent.RecordNumber
writeLog "Source Name: " & objEvent.SourceName
writeLog "Time Generated: " & objEvent.TimeGenerated
writeLog "Time Written: " & objEvent.TimeWritten
writeLog "Type: " & objEvent.Type
writeLog "User: " & objEvent.User
Next
Sub writeLog(strText)
Dim objFSO, objLogFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile(LOG_FILE, 8, True)
objLogFile.WriteLine strText
objLogFile.Close
End Sub
I allso tried this with:
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,(Security)}!\\" & strComputer & "\root\cimv2")
And:
Set objWMIService = GetObject("winmgmts:{(Security)}\\" & strComputer & "\root\cimv2")
As shown on the screencap, the event is defenatly in the the Event Log.
(source: jef at 149.210.139.155)
Does anyone have any idea what causes this and on how to fix this. Thanks for your time.
Try to list the logged on users to a windows terminal server in a workgroup via vbscript.
I have the below but it does not seem to return the logged on users?
strComputer = "."
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colSessions = objWMI.ExecQuery _
("Select * from Win32_LogonSession Where LogonType = 10")
If colSessions.Count = 0 Then
Wscript.Echo "No interactive users found"
Else
WScript.Echo "RDP Sessions:"
For Each objSession in colSessions
Set colList = objWMI.ExecQuery("Associators of " _
& "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _
& "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )
For Each objItem in colList
WScript.Echo "Username: " & objItem.Name & " FullName: " & objItem.FullName
Next
Next
End If
You must run it as Administrator!
run CMD as administrator and run the command CSCRIPT
Other option is to check owners of explorer.exe process.
This will list you all users having open session on server.
strComputer = "."
Set colProcesses = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}!\\" & strComputer & _
"\root\cimv2").ExecQuery("Select * from Win32_Process where name = 'explorer.exe'")
For Each objProcess in colProcesses
who = objProcess.GetOwner(strNameOfUser)
Wscript.Echo "Process " _
& objProcess.Name & " is owned by " _
& strNameOfUser & "."
Next
Example of result:
D:\TEMP>cscript get_explorer_owner.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.
Process explorer.exe is owned by john.
Process explorer.exe is owned by peter.
Process explorer.exe is owned by bob.
How can I check OS version in a batch file or through a vbs in an Windows 2k/2k3 environment ?
You know ... Something like ... : "If winver Win2k then ... or if winver Win2k3 then ....
Run the ver command and parse the string output that it gives you.
you can use vbscript
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOS in colOSes
Wscript.Echo "Computer Name: " & objOS.CSName
Wscript.Echo "Caption: " & objOS.Caption 'Name
Wscript.Echo "Version: " & objOS.Version 'Version & build
Next
use the Caption to capture the string you want.
You should use ver and find.
Example:
ver | find "XP"
As part of a very simple cmd.exe install script, I need to run a program the next time the machine reboots. I don't want it to run after that (it's a one-shot configuration tool).
The program will actually be another cmd.exe script but any example should do since I can run cmd /c on the script itself.
What's the best way to go about doing this?
You could use the SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce key
This VB script could help. Extract:
workfile = ifile.ReadLine
strcomputer = ucase(left(workfile,instr(workfile,",")-1))
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strcomputer & "\root\default:StdRegProv")
if err.number <> 0 then
ofile.WriteLine "[" & now() & "] " & strcomputer & " will NOT run once. Failed to set runonce install with error: " & Err.Number & "/" & left(Err.Description,17)
else
sKeyPathEnv = "SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"
sValueName = "Set_RunOnce"
sKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"
sValueName = "SystemRoot"
oReg.GetExpandedStringValue HKLM, sKeyPath, sValueName, sSystemRoot
oReg.SetStringValue HKLM, sKeyPathEnv, "Set_RunOnce", vRunOnce
if Err.Number <> 0 then
ofile.WriteLine "[" & now() & "] " & strcomputer & " will NOT run once. Failed to set runonce install with error: " & Err.Number & "/" & left(Err.Description,17)
else
ofile.WriteLine "[" & now() & "] " & strcomputer & " will run once via runonce at next reboot. "
end if
end if