Several WMI examples (primary) - 4

zhaozj2021-02-16  47

Read logging:

Imports system

Imports System.Management

Module Module1

Public SUB

Main

()

DIM strComputer = "MagicDog"

DIM WBEMSERVICES, WBEMOBJECTSET, WBEMOBJECT

WbemServices = getObject ("WinMgmts: //" & struct

WbemObjectSet = WBEMSERVICES.INSTANCESOF ("Win32_NTLOGEVENT")

For Each WBemObject in WbemObjectset

Console.writeline ("log file:" & wbemObject.logfile)

Console.WriteLine ("Record No .: & wbemObject.Recordnumber)

Console.writeline ("Type:" & wbemObject.type)

Console.writeline ("Generation Time:" & WbemObject.timeGenerated)

Console.Writeline ("Source Name:" & wbemObject.SourceName)

Console.writeline ("Event code:" & wbemObject.eventcode)

Console.writeLine ("User" & WbemObject.user)

Console.WriteLine ("Computer Name:" & wbemObject.computername)

Console.writeline ("Information:" & wbemObject.Message)

NEXT

End Sub

End module

-------------------------------------------------- -------------------------------------

Delete log files: (Please back up first)

Imports system

Imports System.Management

Module Module1

Public SUB

Main

()

DIM STRComputer = "."

Dim Objwmiservice, Objlogfiles, Objlogfile, Logs, MYLOGS (3)

Objwmiservice = getObject ("WinMgmts: {ImpersonationLevel = Impersonate, (Backup)}! //" & strcomputer & "root / cimv2")

Mylogs (1) = "Application"

MYLOGS (2) = "system"

MYLOGS (3) = "Security"

For Each Logs in Mylogs

Objlogfiles = objwmiservice.execQuery ("SELECT * WIN32_NTEVENTLOGFILE WHERE LOGFILENAME = '" & logs & "'")

For Each Objlogfile in Objlogfiles

Objlogfile.cleareventlog ()

NEXT

NEXT

End Sub

End module

-------------------------------------------------- -------------------------------------

Use performance counters:

Imports system

Imports System.Management

Module Module1

Public SUB

Main

()

DIM STRComputer As string = ".", I, intValue as integer

Dim Colitems, Objwmiservice, Objitem

Objwmiservice = getObject ("WinMgmts: //" & strComputer & "/ root / cimv2")

FOR i = 1 to 5

Thieading.thread.sleep (1000)

Colitems = Objwmiservice.execQuery ("SELECT * WIN32_PERFRAWDATA_PERFPROC_PROCESS WHERE Name = 'devenv'")

For Each Objitem in Colitems

INTVALUE = Objitem.Handlecount

Console.writeLine ("Opened Handle Number:" & INTVALUE)

NEXT

NEXT

End Sub

End module

Creating an object using the getObject function is used in the script, so the parameter is the tagname "Winmgmts:" and target computer name of the WMI script library, the above STRComputer = "." It is equivalent to localhost, if it is executed on the remote computer. " "Change this corresponding computer name.

among them:

ManagementObject: single management object

ManagementClass: Single Management Class

ManagementObjectSearcher: Query the collection of management objects and management classes

Managementeventwatcher: Monitoring events from WMI

ManagementQuery: Used as the basis for all query classes.

You can also create from the script:

SET OLOCATOR = CreateObject ("wbemscripting.swbemlocator") 'new swbemlocatorif err.Number <> 0 THEN MSGBOX "Windows Management (WMI) cannot be created" & Err.Description wscript.quit (0) end if

-------------------------------------------------- ---------------------

set locator = createobject ( "WbemScripting.SwbemLocator") set server = locator.ConnectServer ( "//192.168.0.1/root/cimv2","username","password")set p_obj = server.execquery (" select * form Win32_operatingsystem ") for each p_objswbem in p_obj p_objswbem.reboot 'For overview of newly starting NextWMI, please refer to MSDN or http://www.9cbs.net/develop/Article/19/19157.shtm

转载请注明原文地址:https://www.9cbs.com/read-23135.html

New Post(0)