Adv Oop Lect19

Embed Size (px)

Citation preview

  • 8/13/2019 Adv Oop Lect19

    1/3

    Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

    Lecture (20 Jan)

    Windows Management Instrumentation (WMI)

    WMI allows developers to use a simple, consistent mechanism to query forinformation or configure settings on computers across an enterprise. The amountof information available through the WMI interface is amazinghardwaresettings, performance information, driver configuration, BIOS information,application settings, event log information, and much more.

    usi ng Syst em;usi ng Syst em. Management ;publ i c cl ass Remot eConnect{

    publ i c stat i c voi d Mai n( ){

    / / Bui l d an opt i ons obj ect f or t he remote connect i on/ / i f you pl an to connect t o t he remot e/ / comput er wi t h a di f f er ent user name/ / and password t han t he one you are curr ent l y usi ng./ / Thi s exampl e uses t he def aul t val ues.

    Connect i onOpt i ons opt i ons =new Connect i onOpt i ons( ) ;

    / / Make a connect i on t o a r emot e comput er ./ / Repl ace t he "Ful l ComputerName" sect i on of t he/ / st r i ng "\ \ \ \ Ful l Comput er Name\ \ r oot \ \ ci mv2" wi t h/ / t he f ul l comput er name or I P addr ess of t he

    / / r emote computer .

    / / opt i ons. Username = "" ;/ / opt i ons. Passwor d = "" ;

    Management Scope scope =new Management Scope("\ \ \ \ osman\ \ r oot \ \ ci mv2", opt i ons) ;

    scope. Connect ( ) ;

    / / Quer y system f or Oper at i ng System i nf or mat i onObj ect Quer y quer y = new Obj ect Quer y(

    "SELECT * FROM Wi n32_Oper at i ngSyst em") ;Management Obj ect Searcher sear cher =

    new Management Obj ect Searcher ( scope, quer y) ;

    Management Obj ect Col l ect i on quer yCol l ect i on = sear cher . Get ( ) ;f or each ( Management Obj ect m i n quer yCol l ect i on){

    / / Di spl ay t he r emote comput er i nf ormat i onConsol e. Wr i t eLi ne( "Comput er Name : {0}",

    m[ "csname"] ) ;

    Downloaded from: www.onspot.pk

  • 8/13/2019 Adv Oop Lect19

    2/3

    Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

    Consol e. Wr i t eLi ne( "Wi ndows Di r ect or y : {0}",m[ "Wi ndowsDi r ect ory"] ) ;

    Consol e. Wr i t eLi ne( "Oper at i ng Syst em: {0}",m[ "Capt i on" ] ) ;

    Consol e. Wr i t eLi ne( "Ver si on: {0}", m[ "Ver si on"] ) ;Consol e. Wr i t eLi ne( "Manuf actur er : {0}",

    m[ "Manuf acturer"] ) ;}

    }}

    ConnectionOptions ClassSpecifies all settings required to make a WMI connection.

    ManagementScope ClassRepresents a scope (namespace) for management operations.

    Connect Connects this ManagementScopeto the actual WMI scope

    ObjectQuery ClassRepresents a management query that returns instances or classes.

    ManagementObjectSearcher Class

    Retrieves a collection of management objects based on a specified query.

    This class is one of the more commonly used entry points to retrieving managementinformation.

    For example, it can be used to enumerate all disk drives, network adapters,processes and many more management objects on a system, or to query for all networkconnections that are up, services that are paused, and so on.

    When instantiated, an instance of this class takes as input a WMI query representedin an ObjectQuery or its derivatives, and optionally a ManagementScope representing

    the WMI namespace to execute the query in.

    It can also take additional advanced options in an EnumerationOptions. When theGet()()() method on this object is invoked, the ManagementObjectSearcherexecutesthe given query in the specified scope and returns a collection of management objectsthat match the query in a ManagementObjectCollection.

    Downloaded from: www.onspot.pk

  • 8/13/2019 Adv Oop Lect19

    3/3

    Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

    ManagementObjectCollection Class

    Represents different collections of management objects retrieved through WMI. Theobjects in this collection are of ManagementBaseObject-derived types, includingManagementObject and ManagementClass. The collection can be the result of a WMIquery executed through a ManagementObjectSearcher

    EXAMPLES

    sel ect Name FROM Wi n32_Computer Syst em

    sel ect Capt i on, Descr i pt i on, Devi ceI D, Number Of Funct i onKeys, St at usf r omwi n32_keyboar d

    sel ect Capt i on, Manuf act ur er , St at us f r om Wi n32_Fl oppyDr i ve

    Downloaded from: www.onspot.pk