The following is a code example using VBScript and WMI:
More examples can be found at:
http://www.planetmps.com/
Dim svc
Dim objs
Dim obj
Dim prop
Dim Name
Dim Value
Set svc = GetObject("winmgmts:\\.\root\cimv2")
Set objs = svc.InstancesOf("Win32_Bios")
For Each obj In objs
For Each prop In obj.Properties_
Name = prop.Name
Value = GetValue(prop.Name, obj)
Debug.Print Name & " " & Value
Next
Next
Private Function GetValue(ByVal Name, ByVal obj)
Dim pName
Dim pos
Dim tempstr
pName = Name & " = "
tempstr = obj.GetObjectText_
pos = InStr(tempstr, pName)
If pos <= 0 Then
GetValue = ""
Else
tempstr = Mid(tempstr, pos + Len(pName), Len(tempstr))
pos = InStr(tempstr, ";")
tempstr = Mid(tempstr, 1, pos - 1)
tempstr = Replace(tempstr, Chr(34), "")
tempstr = Replace(tempstr, "{", "")
tempstr = Replace(tempstr, "}", "")
tempstr = Trim(tempstr)
If obj.Properties_(Name).CIMType = 101 And Len(tempstr) > 14 Then
tempstr = Mid(tempstr, 5, 2) & "/" & Mid(tempstr, 7, 2) & "/" & Mid(tempstr, 1, 4) & " " & Mid(tempstr, 9, 2) & ":" & Mid(tempstr, 11, 2) & ":" & Mid(tempstr, 13, 2)
End If
GetValue = tempstr
End If
End Function
Tuesday, June 7, 2011
WMI Example using VB.Net and WMI
The following is an example of using VB.Net and WMI:
You can download more of these examples from:
http://www.planetmps.com/
Private Sub Form_Load()
Dim l
Dim svc
Dim objs
Dim obj
Dim prop
Dim Name
Dim Value
Set l = CreateObject("WbemScripting.SwbemLocator")
Set svc = l.ConnectServer(".", "\root\cimv2")
Set objs = svc.InstancesOf("Win32_Bios")
For Each obj In objs
For Each prop In obj.Properties_
Name = prop.Name
Value = GetValue(prop.Name, obj)
Debug.Print Name & " " & Value
Next
Next
End Sub
Private Function GetValue(ByVal Name As String, ByVal obj As SWbemObject)
Dim pName
Dim pos
Dim tempstr
pName = Name & " = "
tempstr = obj.GetObjectText_
pos = InStr(tempstr, pName)
If pos <= 0 Then
GetValue = ""
Else
tempstr = Mid(tempstr, pos + Len(pName), Len(tempstr))
pos = InStr(tempstr, ";")
tempstr = Mid(tempstr, 1, pos - 1)
tempstr = Replace(tempstr, Chr(34), "")
tempstr = Replace(tempstr, "{", "")
tempstr = Replace(tempstr, "}", "")
tempstr = Trim(tempstr)
If obj.Properties_(Name).CIMType = 101 And Len(tempstr) > 14 Then
tempstr = Mid(tempstr, 5, 2) & "/" & Mid(tempstr, 7, 2) & "/" & Mid(tempstr, 1, 4) & " " & Mid(tempstr, 9, 2) & ":" & Mid(tempstr, 11, 2) & ":" & Mid(tempstr, 13, 2)
End If
GetValue = tempstr
End If
End Function
You can download more of these examples from:
http://www.planetmps.com/
Private Sub Form_Load()
Dim l
Dim svc
Dim objs
Dim obj
Dim prop
Dim Name
Dim Value
Set l = CreateObject("WbemScripting.SwbemLocator")
Set svc = l.ConnectServer(".", "\root\cimv2")
Set objs = svc.InstancesOf("Win32_Bios")
For Each obj In objs
For Each prop In obj.Properties_
Name = prop.Name
Value = GetValue(prop.Name, obj)
Debug.Print Name & " " & Value
Next
Next
End Sub
Private Function GetValue(ByVal Name As String, ByVal obj As SWbemObject)
Dim pName
Dim pos
Dim tempstr
pName = Name & " = "
tempstr = obj.GetObjectText_
pos = InStr(tempstr, pName)
If pos <= 0 Then
GetValue = ""
Else
tempstr = Mid(tempstr, pos + Len(pName), Len(tempstr))
pos = InStr(tempstr, ";")
tempstr = Mid(tempstr, 1, pos - 1)
tempstr = Replace(tempstr, Chr(34), "")
tempstr = Replace(tempstr, "{", "")
tempstr = Replace(tempstr, "}", "")
tempstr = Trim(tempstr)
If obj.Properties_(Name).CIMType = 101 And Len(tempstr) > 14 Then
tempstr = Mid(tempstr, 5, 2) & "/" & Mid(tempstr, 7, 2) & "/" & Mid(tempstr, 1, 4) & " " & Mid(tempstr, 9, 2) & ":" & Mid(tempstr, 11, 2) & ":" & Mid(tempstr, 13, 2)
End If
GetValue = tempstr
End If
End Function
WMI Example code using VB
The following is a code example using VB with WMI:
More examples can be found at:
http://www.planetmps.com/
Private Sub Form_Load()
Dim l
Dim svc
Dim objs
Dim obj
Dim prop
Dim Name
Dim Value
Set l = CreateObject("WbemScripting.SwbemLocator")
Set svc = l.ConnectServer(".", "\root\cimv2")
Set objs = svc.InstancesOf("Win32_Bios")
For Each obj In objs
For Each prop In obj.Properties_
Name = prop.Name
Value = GetValue(prop.Name, obj)
Debug.Print Name & " " & Value
Next
Next
End Sub
Private Function GetValue(ByVal Name As String, ByVal obj As SWbemObject)
Dim pName
Dim pos
Dim tempstr
pName = Name & " = "
tempstr = obj.GetObjectText_
pos = InStr(tempstr, pName)
If pos <= 0 Then
GetValue = ""
Else
tempstr = Mid(tempstr, pos + Len(pName), Len(tempstr))
pos = InStr(tempstr, ";")
tempstr = Mid(tempstr, 1, pos - 1)
tempstr = Replace(tempstr, Chr(34), "")
tempstr = Replace(tempstr, "{", "")
tempstr = Replace(tempstr, "}", "")
tempstr = Trim(tempstr)
If obj.Properties_(Name).CIMType = 101 And Len(tempstr) > 14 Then
tempstr = Mid(tempstr, 5, 2) & "/" & Mid(tempstr, 7, 2) & "/" & Mid(tempstr, 1, 4) & " " & Mid(tempstr, 9, 2) & ":" & Mid(tempstr, 11, 2) & ":" & Mid(tempstr, 13, 2)
End If
GetValue = tempstr
End If
End Function
More examples can be found at:
http://www.planetmps.com/
Private Sub Form_Load()
Dim l
Dim svc
Dim objs
Dim obj
Dim prop
Dim Name
Dim Value
Set l = CreateObject("WbemScripting.SwbemLocator")
Set svc = l.ConnectServer(".", "\root\cimv2")
Set objs = svc.InstancesOf("Win32_Bios")
For Each obj In objs
For Each prop In obj.Properties_
Name = prop.Name
Value = GetValue(prop.Name, obj)
Debug.Print Name & " " & Value
Next
Next
End Sub
Private Function GetValue(ByVal Name As String, ByVal obj As SWbemObject)
Dim pName
Dim pos
Dim tempstr
pName = Name & " = "
tempstr = obj.GetObjectText_
pos = InStr(tempstr, pName)
If pos <= 0 Then
GetValue = ""
Else
tempstr = Mid(tempstr, pos + Len(pName), Len(tempstr))
pos = InStr(tempstr, ";")
tempstr = Mid(tempstr, 1, pos - 1)
tempstr = Replace(tempstr, Chr(34), "")
tempstr = Replace(tempstr, "{", "")
tempstr = Replace(tempstr, "}", "")
tempstr = Trim(tempstr)
If obj.Properties_(Name).CIMType = 101 And Len(tempstr) > 14 Then
tempstr = Mid(tempstr, 5, 2) & "/" & Mid(tempstr, 7, 2) & "/" & Mid(tempstr, 1, 4) & " " & Mid(tempstr, 9, 2) & ":" & Mid(tempstr, 11, 2) & ":" & Mid(tempstr, 13, 2)
End If
GetValue = tempstr
End If
End Function
WMI example code using Rexx
The following is a code example using Ruby with WMI:
Additional examples can be found at:
http://www.planetmps.com/
Additional examples can be found at:
http://www.planetmps.com/
locator = .OLEObject~new("WbemScripting.SWbemLocator") service = locator~ConnectServer(".", "root\cimv2") service~Security_~AuthenticationLevel = 0 service~Security_~ImpersonationLevel = 3 objs = service~ExecQuery("Select * From SMS_MPInformationEx") do obj over objs mystr = obj~GetObjetText_(0) exit end ie = .OLEObject~new("InternetExplorer.Application") ie~Visible = false ie~Navigate("about:blank") ie~document~ParentWindow~clipboarddata~SetData("text", mystr) ie~Quit() ::requires "OREXXOLE.CLS"
WMI Code example using Python
The following is an example of using Python with WMI:
More example code can be found at:
http://www.planetmps.com/
More example code can be found at:
http://www.planetmps.com/
import(win32com.client) import string def(GetValue(N, obj)) pos = 0 pos1 = 0 N = N + " = " Name = Str(N) s = Str(obj.GetObjectText_()) pos = s.find(Name) if pos > 0: pos = pos + len(Name) pos1 = len(s) s = s[pos : pos1] pos = s.find(";") s = s[0 : pos] s = s.replace("{", "") s = s.replace("}", "") s = s.replace('"', "") return (s) else: return ("") def(WMIDateStringToDate(dtmDate)) strDateTime = "" if (dtmDate[4] == 0): strDateTime = dtmDate[5] + '/' else: strDateTime = dtmDate[4] + dtmDate[5] + '/' if (dtmDate[6] == 0): strDateTime = strDateTime + dtmDate[7] + '/' else: strDateTime = strDateTime + dtmDate[6] + dtmDate[7] + '/' strDateTime = strDateTime + dtmDate[0] + dtmDate[1] + dtmDate[2] + dtmDate[3] + " " + dtmDate[8] + dtmDate[9] + ":" + dtmDate[10] + dtmDate[11] +':' + dtmDate[12] + dtmDate[13] return strDateTime strComputer = "." locator = win32com.client.Dispatch("WbemScripting.SWbemLocator") sService = locator.ConnectServer(strComputer, "root/CIMV2/ms_409") ob = service.Get("Win32_BIOS") objset = ob.Instances_ for obj in objset: for prop in obj.Properties_: value = GetValue(prop.Name, obj) if Len(value) > 0: if prop.CIMType == 101: value = WMIDateStringToDate(value) print(prop.Name + " " + value)
WMI Code example using Perl
The following is a code example using Perlscript:
More examples can be found at:
http://www.planetmps.com/
use Win32;
use Win32::OLE('in');
use constant wbemFlagReturnImmediately => 0x10;
use constant wbemFlagForwardOnly => 0x20;
$tempstr;
my $Locator = Win32::OLE->new("WbemScripting.SWbemLocator");
my $Services = $Locator->ConnectServer(".", "root\CIMV2\sms");
my $ob = $Services->Get("Win32_Process");
my $objset = $ob->Instances_;
foreach $obj (in $objset)
{
$propSet = $obj->{Properties_};
foreach $prop (in $obj->Properties_)
{
if(ref($prop->{Value}) ne "null")
{
if(ref($prop->{Value}) eq "ARRAY")
{
$tempstr = $tempstr . $prop->Name . ": " . join("," , (in $prop->{value})) . "\n";
}
else
{
if ($prop->CIMType == 101)
{
$tempstr = $tempstr . $prop->Name . ": " . convertDate($prop->{value}) . "\n";
}
else
{
$tempstr = $tempstr . $prop->Name . ": " . $prop->{value} . "\n";
}
}
}
}
print $tempstr;
last;
}
Sub convertDate()
{
my ($cdater) = @_;
if($cdater == "")
{
}
else
{
$tstr = substr($cdater, 4,2) . "/" . substr($cdater, 6,2) . "/" . substr($cdater, 0,4) . " " . substr($cdater, 8,2) . ":" . substr($cdater, 10,2) . ":" . substr($cdater, 12,2);
return $tstr;
}
}
More examples can be found at:
http://www.planetmps.com/
use Win32;
use Win32::OLE('in');
use constant wbemFlagReturnImmediately => 0x10;
use constant wbemFlagForwardOnly => 0x20;
$tempstr;
my $Locator = Win32::OLE->new("WbemScripting.SWbemLocator");
my $Services = $Locator->ConnectServer(".", "root\CIMV2\sms");
my $ob = $Services->Get("Win32_Process");
my $objset = $ob->Instances_;
foreach $obj (in $objset)
{
$propSet = $obj->{Properties_};
foreach $prop (in $obj->Properties_)
{
if(ref($prop->{Value}) ne "null")
{
if(ref($prop->{Value}) eq "ARRAY")
{
$tempstr = $tempstr . $prop->Name . ": " . join("," , (in $prop->{value})) . "\n";
}
else
{
if ($prop->CIMType == 101)
{
$tempstr = $tempstr . $prop->Name . ": " . convertDate($prop->{value}) . "\n";
}
else
{
$tempstr = $tempstr . $prop->Name . ": " . $prop->{value} . "\n";
}
}
}
}
print $tempstr;
last;
}
Sub convertDate()
{
my ($cdater) = @_;
if($cdater == "")
{
}
else
{
$tstr = substr($cdater, 4,2) . "/" . substr($cdater, 6,2) . "/" . substr($cdater, 0,4) . " " . substr($cdater, 8,2) . ":" . substr($cdater, 10,2) . ":" . substr($cdater, 12,2);
return $tstr;
}
}
WMI Code Example using Kixtart
The following is a code example using Kixtart:
Additional examples can be found at:
http://www.planetmps.com/
Additional examples can be found at:
http://www.planetmps.com/
$crlf = Chr(13) + Chr(10) $tempstr $Name $Value $locator = CreateObject("WbemScripting.SWbemLocator") $services = $locator.ConnectServer(".", "root\aspnet") $services.Security_.AuthenticationLevel = 0 $services.Security_.ImpersonationLevel = 3 $ob = $services.Get("ApplicationLifetimeEvent") $objset = $ob.Instances_ For each $obj in $objset for each $prop in $obj.Properties_ $Name = $prop.Name $Value = checkme($prop) $tempstr = $tempstr . $Name . " = " . $Value . "\n\r" next Messagebox($tempstr, "Kixtart") $tempstr = "" exit for next function checkme($prop) if vartype($Prop.Value) = -1 $checkme = "" else if $prop.IsArray $Value = Join($prop.Value, ",") $checkme = $Value else if $prop.CIMType = 101 if len($Prop.Value) > 0 $Value = $prop.Value $checkme = substr($Value, 5,2) + "/" + substr($Value, 7,2) + "/" + substr($Value, 1,4) + " " + substr($Value, 9,2) + ":" + substr($Value, 11,2) + ":" + substr($Value, 13,2) else $checkme = "" endif else if $prop.CIMType = 11 if $prop.Value = 0 $checkme = "False" else $checkme = "True" endif else $Value = $prop.Value $checkme = $Value endif endif endif endif endfunction
WMI Example Code Using JScript
Below is a code example using jscript with WMI.
More examples can be found at:
http://www.planetmps.com/
var wbemFlagReturnImmediately = 0x10;
var wbemFlagForwardOnly = 0x20;
var mystr = new String("");
var mysink;
var x=0;
function Sink_OnObjectReady(objsbemObject, objWbemContext
{
var objset = objWbemObject.Instances_();
var objEnum = new Enumerator(objset);
var enumItems = new Enumerator(objs);
for (; !enumItems.atEnd(); enumItems.moveNext())
{
var obj = enumItems.item();
var propset = obj.Properties_
var propEnum = new Enumerator(propset);
for (; !propEnum.atEnd(); propEnum.moveNext())
{
if (prop.Value != null)
{
if (prop.IsArray == true)
{
mystr = mystr + prop.Name + ": " + (prop.Value.toArray()).join(",") + "\n";
}
else
{
if (prop.CIMType == 101)
{
mystr = mystr + prop.Name + ": " + WMIDateStringToDate(prop.Value) + "\n";
}
else
{
mystr = mystr + prop.Name + ": " + prop.Value + "\n";
}
}
}
}
WScript.Echo(mystr);
mystr = "";
break;
}
x=1;
}
var locator = new ActiveXObject("WbemScripting.SWbemLocator");
var service = locator.connectServer('.', "root\\CIMV2\\ms_409");
mysink = WScript.CreateObject("WbemScripting.SWbemSink");
service.GetAsync(mysink, "Win32_Service");
while(x == 0)
{
WScript.Sleep(1000);
}
function WMIDateStringToDate(dtmDate)
{
if (dtmDate == null)
{
return "null date";
}
var strDateTime;
if (dtmDate.substr(4, 1) == 0)
{
strDateTime = dtmDate.substr(5, 1) + "/";
}
else
{
strDateTime = dtmDate.substr(4, 2) + "/";
}
if (dtmDate.substr(6, 1) == 0)
{
strDateTime = strDateTime + dtmDate.substr(7, 1) + "/";
}
else
{
strDateTime = strDateTime + dtmDate.substr(6, 2) + "/";
}
strDateTime = strDateTime + dtmDate.substr(0, 4) + " " +
dtmDate.substr(8, 2) + ":" +
dtmDate.substr(10, 2) + ":" +
dtmDate.substr(12, 2);
return(strDateTime);
}
More examples can be found at:
http://www.planetmps.com/
var wbemFlagReturnImmediately = 0x10;
var wbemFlagForwardOnly = 0x20;
var mystr = new String("");
var mysink;
var x=0;
function Sink_OnObjectReady(objsbemObject, objWbemContext
{
var objset = objWbemObject.Instances_();
var objEnum = new Enumerator(objset);
var enumItems = new Enumerator(objs);
for (; !enumItems.atEnd(); enumItems.moveNext())
{
var obj = enumItems.item();
var propset = obj.Properties_
var propEnum = new Enumerator(propset);
for (; !propEnum.atEnd(); propEnum.moveNext())
{
if (prop.Value != null)
{
if (prop.IsArray == true)
{
mystr = mystr + prop.Name + ": " + (prop.Value.toArray()).join(",") + "\n";
}
else
{
if (prop.CIMType == 101)
{
mystr = mystr + prop.Name + ": " + WMIDateStringToDate(prop.Value) + "\n";
}
else
{
mystr = mystr + prop.Name + ": " + prop.Value + "\n";
}
}
}
}
WScript.Echo(mystr);
mystr = "";
break;
}
x=1;
}
var locator = new ActiveXObject("WbemScripting.SWbemLocator");
var service = locator.connectServer('.', "root\\CIMV2\\ms_409");
mysink = WScript.CreateObject("WbemScripting.SWbemSink");
service.GetAsync(mysink, "Win32_Service");
while(x == 0)
{
WScript.Sleep(1000);
}
function WMIDateStringToDate(dtmDate)
{
if (dtmDate == null)
{
return "null date";
}
var strDateTime;
if (dtmDate.substr(4, 1) == 0)
{
strDateTime = dtmDate.substr(5, 1) + "/";
}
else
{
strDateTime = dtmDate.substr(4, 2) + "/";
}
if (dtmDate.substr(6, 1) == 0)
{
strDateTime = strDateTime + dtmDate.substr(7, 1) + "/";
}
else
{
strDateTime = strDateTime + dtmDate.substr(6, 2) + "/";
}
strDateTime = strDateTime + dtmDate.substr(0, 4) + " " +
dtmDate.substr(8, 2) + ":" +
dtmDate.substr(10, 2) + ":" +
dtmDate.substr(12, 2);
return(strDateTime);
}
WMI Code example using Javascript
Below is a code example using javascript with WMI.
More examples can be found at:
http://www.planetmps.com/
var wbemFlagReturnImmediately = 0x10;
var wbemFlagForwardOnly = 0x20;
var mystr = new String("");
var mysink;
var x=0;
function Sink_OnObjectReady(objsbemObject, objWbemContext
{
var objset = objWbemObject.Instances_();
var objEnum = new Enumerator(objset);
var enumItems = new Enumerator(objs);
for (; !enumItems.atEnd(); enumItems.moveNext())
{
var obj = enumItems.item();
var propset = obj.Properties_
var propEnum = new Enumerator(propset);
for (; !propEnum.atEnd(); propEnum.moveNext())
{
if (prop.Value != null)
{
if (prop.IsArray == true)
{
mystr = mystr + prop.Name + ": " + (prop.Value.toArray()).join(",") + "\n";
}
else
{
if (prop.CIMType == 101)
{
mystr = mystr + prop.Name + ": " + WMIDateStringToDate(prop.Value) + "\n";
}
else
{
mystr = mystr + prop.Name + ": " + prop.Value + "\n";
}
}
}
}
WScript.Echo(mystr);
mystr = "";
break;
}
x=1;
}
var locator = new ActiveXObject("WbemScripting.SWbemLocator");
var service = locator.connectServer('.', "root\\CIMV2\\ms_409");
mysink = WScript.CreateObject("WbemScripting.SWbemSink");
service.GetAsync(mysink, "Win32_Service");
while(x == 0)
{
WScript.Sleep(1000);
}
function WMIDateStringToDate(dtmDate)
{
if (dtmDate == null)
{
return "null date";
}
var strDateTime;
if (dtmDate.substr(4, 1) == 0)
{
strDateTime = dtmDate.substr(5, 1) + "/";
}
else
{
strDateTime = dtmDate.substr(4, 2) + "/";
}
if (dtmDate.substr(6, 1) == 0)
{
strDateTime = strDateTime + dtmDate.substr(7, 1) + "/";
}
else
{
strDateTime = strDateTime + dtmDate.substr(6, 2) + "/";
}
strDateTime = strDateTime + dtmDate.substr(0, 4) + " " +
dtmDate.substr(8, 2) + ":" +
dtmDate.substr(10, 2) + ":" +
dtmDate.substr(12, 2);
return(strDateTime);
}
More examples can be found at:
http://www.planetmps.com/
var wbemFlagReturnImmediately = 0x10;
var wbemFlagForwardOnly = 0x20;
var mystr = new String("");
var mysink;
var x=0;
function Sink_OnObjectReady(objsbemObject, objWbemContext
{
var objset = objWbemObject.Instances_();
var objEnum = new Enumerator(objset);
var enumItems = new Enumerator(objs);
for (; !enumItems.atEnd(); enumItems.moveNext())
{
var obj = enumItems.item();
var propset = obj.Properties_
var propEnum = new Enumerator(propset);
for (; !propEnum.atEnd(); propEnum.moveNext())
{
if (prop.Value != null)
{
if (prop.IsArray == true)
{
mystr = mystr + prop.Name + ": " + (prop.Value.toArray()).join(",") + "\n";
}
else
{
if (prop.CIMType == 101)
{
mystr = mystr + prop.Name + ": " + WMIDateStringToDate(prop.Value) + "\n";
}
else
{
mystr = mystr + prop.Name + ": " + prop.Value + "\n";
}
}
}
}
WScript.Echo(mystr);
mystr = "";
break;
}
x=1;
}
var locator = new ActiveXObject("WbemScripting.SWbemLocator");
var service = locator.connectServer('.', "root\\CIMV2\\ms_409");
mysink = WScript.CreateObject("WbemScripting.SWbemSink");
service.GetAsync(mysink, "Win32_Service");
while(x == 0)
{
WScript.Sleep(1000);
}
function WMIDateStringToDate(dtmDate)
{
if (dtmDate == null)
{
return "null date";
}
var strDateTime;
if (dtmDate.substr(4, 1) == 0)
{
strDateTime = dtmDate.substr(5, 1) + "/";
}
else
{
strDateTime = dtmDate.substr(4, 2) + "/";
}
if (dtmDate.substr(6, 1) == 0)
{
strDateTime = strDateTime + dtmDate.substr(7, 1) + "/";
}
else
{
strDateTime = strDateTime + dtmDate.substr(6, 2) + "/";
}
strDateTime = strDateTime + dtmDate.substr(0, 4) + " " +
dtmDate.substr(8, 2) + ":" +
dtmDate.substr(10, 2) + ":" +
dtmDate.substr(12, 2);
return(strDateTime);
}
WMI Code Using Java
The following example uses Java and WMI.
Additional examples can be found at:
http://www.planetmps.com/
Additional examples can be found at:
http://www.planetmps.com/
import java.io.Console; import org.jawin.DispatchPtr; import org.jawin.UnknownPtr; import org.jawin.IUnknown; import org.jawin.IEnumVariant; import org.jawin.win32.*; import org.jawin.COMPtr; import java.io.IOException; try { Ole32.CoInitialize(); DispatchPtr locator = new DispatchPtr("WbemScripting.SWbemLocator"); DispatchPtr service = (DispatchPtr)locator.invoke("ConnectServer", ".", "root\\aspnet"); DispatchPtr ob = (DispatchPtr)service.invoke("Get", "ApplicationLifetimeEvent"); DispatchPtr objs = (DispatchPtr)ob.invoke("Instances_"); IUnknown oEnum = (IUnknown)objs.get("_NewEnum"); IEnumVariant objEnum = (IEnumVariant)oEnum.queryInterface(IEnumVariant.class); int c =1; Object[] tempobj = new Object[1]; while (objEnum.Next(c, tempobj) == 1) { DispatchPtr obj = (DispatchPtr)tempobj[0]; DispatchPtr propSet = (DispatchPtr)obj.get("Properties_"); IUnknown pEnum = (IUnknown)propSet.get("_NewEnum"); IEnumVariant propEnum = (IEnumVariant)pEnum.queryInterface(IEnumVariant.class); int d =1; Object[] tempProp = new Object[1]; while (propEnum.Next(d, tempProp) == 1) { DispatchPtr prop = (DispatchPtr)tempProp[0]; String Name = prop.get("Name").toString(); Object Value = prop.get("Value"); if(Value == null) { println Name + ":"; } else { if(prop.getClass().isArray() == true) { String tempstr = ""; Object[] a = new Object[1]; int l = prop.get("Value").toString().length()-1; for(int x in 0..l) { a[0] = x; tempstr = tempstr + prop.getN("Value", a).toString(); } System.out.println(Name + ":" + tempstr); } Else { if(prop.get("CIMType") == 101) { println Name + ":" + prop.get("Value").toString().substring(4,6) + "/" + prop.get("Value").toString().substring(6,8) + "/" + prop.get("Value").toString().substring(0,4) + " " + + prop.get("Value").toString().substring(8,10) + ":" + prop.get("Value").toString().substring(10,12) + ":" + prop.get("Value").toString().substring(12,14); } else { println Name + ":" + prop.get("Value").toString(); } } } } break; } Ole32.CoUninitialize(); } catch(Exception e) { e.printStackTrace(); }
WMI Example using Groovy
The following example uses Java code with Groovy and WMI.
More code examples can be found at:
http://www.planetmps.com/
More code examples can be found at:
http://www.planetmps.com/
import java.io.Console; import org.jawin.DispatchPtr; import org.jawin.UnknownPtr; import org.jawin.IUnknown; import org.jawin.IEnumVariant; import org.jawin.win32.*; import org.jawin.COMPtr; import java.io.IOException; try { Ole32.CoInitialize(); DispatchPtr locator = new DispatchPtr("WbemScripting.SWbemLocator"); DispatchPtr service = (DispatchPtr)locator.invoke("ConnectServer", ".", "root\\aspnet"); DispatchPtr ob = (DispatchPtr)service.invoke("Get", "ApplicationLifetimeEvent"); DispatchPtr objs = (DispatchPtr)ob.invoke("Instances_"); IUnknown oEnum = (IUnknown)objs.get("_NewEnum"); IEnumVariant objEnum = (IEnumVariant)oEnum.queryInterface(IEnumVariant.class); int c =1; Object[] tempobj = new Object[1]; while (objEnum.Next(c, tempobj) == 1) { DispatchPtr obj = (DispatchPtr)tempobj[0]; DispatchPtr propSet = (DispatchPtr)obj.get("Properties_"); IUnknown pEnum = (IUnknown)propSet.get("_NewEnum"); IEnumVariant propEnum = (IEnumVariant)pEnum.queryInterface(IEnumVariant.class); int d =1; Object[] tempProp = new Object[1]; while (propEnum.Next(d, tempProp) == 1) { DispatchPtr prop = (DispatchPtr)tempProp[0]; String Name = prop.get("Name").toString(); Object Value = prop.get("Value"); if(Value == null) { println Name + ":"; } else { if(prop.getClass().isArray() == true) { String tempstr = ""; Object[] a = new Object[1]; int l = prop.get("Value").toString().length()-1; for(int x in 0..l) { a[0] = x; tempstr = tempstr + prop.getN("Value", a).toString(); } System.out.println(Name + ":" + tempstr); } Else { if(prop.get("CIMType") == 101) { println Name + ":" + prop.get("Value").toString().substring(4,6) + "/" + prop.get("Value").toString().substring(6,8) + "/" + prop.get("Value").toString().substring(0,4) + " " + + prop.get("Value").toString().substring(8,10) + ":" + prop.get("Value").toString().substring(10,12) + ":" + prop.get("Value").toString().substring(12,14); } else { println Name + ":" + prop.get("Value").toString(); } } } } break; } Ole32.CoUninitialize(); } catch(Exception e) { e.printStackTrace(); }
WMI Example Code using Delphi.Net
The following is a coding example using Delphi.Net.
More examples can be found at:
http://www.planetmps.com/
More examples can be found at:
http://www.planetmps.com/
function GetValue(Name : System.String; obj : ManagementObject): System.String; var tempstr : System.&String; pos : integer; args : array of System.&Object; begin SetLength(args, 1); args[0] := 1; tempstr := System.&String(obj.GetText(TextFormat.Mof)); pos := 0; pos := tempstr.IndexOf(Name); If (pos > 0) Then begin pos := pos + Name.Length; pos := pos + 3; tempstr := tempstr.Substring(pos, tempstr.Length - pos); pos := tempstr.IndexOf(';'); tempstr := tempstr.Substring(0, pos); tempstr := tempstr.Replace('{', ''); tempstr := tempstr.Replace('}', ''); tempstr := tempstr.Replace(char(34), ''); getvalue := tempstr; end; If pos < 1 Then begin getvalue := ''; end; end; procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs); var FSO : Scripting.FileSystemObject; txtstream : Scripting.TextStream; mos : ManagementObjectSearcher; moc : ManagementObjectCollection; objEnum : System.Collections.IEnumerator; obj : ManagementObject; objEnum1 : ManagmentObjectCollection.ManagementObjectEnumerator; obj1 : ManagementObject; PropSet : PropertyDataCollection; propEnum : ProeprtyDataCollection.ProeprtyDataEnumerator; Prop : PropertyData; x : Integer; y : Integer; w : Integer; v : Integer; begin x := 0; y := 0; w := 0; mos:= ManagementObjectSearcher.Create('Win32_Terminal'); mos.Scope.Path.Path='root\\CIMV2\\TerminalServices'); moc := mos.Get(); objEnum := moc.GetEnumerator(); while (objEnum.MoveNext())do begin mo := System.Management.ManagementObject(objEnum.Current); PropSet := mo.Properties; propEnum := PropSet.GetEnumerator(); while (propEnum.MoveNext()) do begin Prop := System.Management.PropertyData(propEnum.Current); end; end;
WMI using Delphi example
The following is an example of using WMI with Delphi.
More examples can be found at:
http://www.planetmps.com/
var
locator : TSWbemLocator;
Services : ISWbemServices;
ObjSet : ISWbemObjectSet;
ob : ISWbemObject;
Obj : ISWbemObject;
PropSet : ISWbemPropertySet;
Prop : ISWbemProperty;
propEnum : IEnumVariant;
objEnum : IEnumVariant;
tempObj : OleVariant;
tempObj1 : OleVariant;
Count : Cardinal;
Value : Cardinal;
sValue : String;
sValue : String;
begin
locator := TSWbemlocator.Create(self);
Services := locator.ConnectServer('', 'root\CIMV2\ms_409', '', '', '', '', 0, nil);
ob := Services.Get('Win32_BIOS');
ObjSet := ob.Instances_;
Enum := IUnknown(ObjSet._NewEnum ) as IEnumVariant;
while (Enum.Next(1, tempObj, Value) = S_OK) do
begin
Obj := IUnknown(tempObj) as SWBemObject;
PropSet := lSObject.Properties_;
propEnum := IUnknown(PropSet._NewEnum) as IEnumVariant;
while (propEnum.Next(1, tempObj1, Count) = S_OK) do
begin
Prop := IUnknown(tempObj1) as SWBemProperty;
if(y == 2) then do
begin
objexcel.Cells(1, x).Value := prop.Name;
}
objexcel.Cells(y, x).Value := GetValue(prop.Name, mo);
x=x+1;
}
x=1;
y=y+1;
end;
break;
end;
More examples can be found at:
http://www.planetmps.com/
var
locator : TSWbemLocator;
Services : ISWbemServices;
ObjSet : ISWbemObjectSet;
ob : ISWbemObject;
Obj : ISWbemObject;
PropSet : ISWbemPropertySet;
Prop : ISWbemProperty;
propEnum : IEnumVariant;
objEnum : IEnumVariant;
tempObj : OleVariant;
tempObj1 : OleVariant;
Count : Cardinal;
Value : Cardinal;
sValue : String;
sValue : String;
begin
locator := TSWbemlocator.Create(self);
Services := locator.ConnectServer('', 'root\CIMV2\ms_409', '', '', '', '', 0, nil);
ob := Services.Get('Win32_BIOS');
ObjSet := ob.Instances_;
Enum := IUnknown(ObjSet._NewEnum ) as IEnumVariant;
while (Enum.Next(1, tempObj, Value) = S_OK) do
begin
Obj := IUnknown(tempObj) as SWBemObject;
PropSet := lSObject.Properties_;
propEnum := IUnknown(PropSet._NewEnum) as IEnumVariant;
while (propEnum.Next(1, tempObj1, Count) = S_OK) do
begin
Prop := IUnknown(tempObj1) as SWBemProperty;
if(y == 2) then do
begin
objexcel.Cells(1, x).Value := prop.Name;
}
objexcel.Cells(y, x).Value := GetValue(prop.Name, mo);
x=x+1;
}
x=1;
y=y+1;
end;
break;
end;
WMI Code Example Using C#
The following Code Example is one of many that can be found at:
http://www.planetmps.com/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WbemScripting;
namespace WindowsApplication10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string tempstr;
private void Sink_OnObjectReady(SWbemObject objWbemObject, SWbemNamedValueSet objWbemAsyncContext)
{
SWbemObjectSet objset = objWbemObject.Instances_(0, null);
System.Collections.IEnumerator objEnum = objset.GetEnumerator();
while (objEnum.MoveNext())
{
SWbemObject obj = (SWbemObject)objEnum.Current;
SWbemPropertySet propset = obj.Properties_;
System.Collections.IEnumerator propEnum = propset.GetEnumerator();
while (propEnum.MoveNext())
{
SWbemProperty prop = (SWbemProperty)propEnum.Current;
String Name = prop.Name;
string Value = checkme(prop);
tempstr = tempstr + Name + " = " + Value + "\n\r";
}
MessageBox.Show(tempstr);
tempstr = "";
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
SWbemLocator locator = new SWbemLocatorClass();
SWbemServices svc = locator.ConnectServer(".", "root\\CIMV2", "", "", "", "", 0, null);
SWbemSink Sink = new SWbemSinkClass();
svc.GetAsync(Sink, "Win32_Processor", 0, null, null);
Sink.OnObjectReady += new ISWbemSinkEvents_OnObjectReadyEventHandler(Sink_OnObjectReady);
}
private string checkme(SWbemProperty prop)
{
String value = "";
if(prop.get_Value() == null)
{
return "";
}
else
{
if(prop.IsArray == true)
{
Object[] myarray = (Object[])prop.get_Value();
for (int x = 0; x < myarray.GetLength(0) - 1; x++)
{
if (value != "")
{
value = value + ",";
}
value = value + myarray[x];
}
return value;
}
else
{
if(prop.CIMType == WbemCimtypeEnum.wbemCimtypeDatetime)
{
value = prop.get_Value().ToString();
if (value != "")
{
value = value.Substring(4, 2) + "/" + value.Substring(6, 2) + "/" + value.Substring(0, 4) + " " + value.Substring(8, 2) + ":" + value.Substring(10, 2) + ":" + value.Substring(12, 2);
}
return value;
}
else
{
return prop.get_Value().ToString();
}
}
}
}
}
http://www.planetmps.com/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WbemScripting;
namespace WindowsApplication10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string tempstr;
private void Sink_OnObjectReady(SWbemObject objWbemObject, SWbemNamedValueSet objWbemAsyncContext)
{
SWbemObjectSet objset = objWbemObject.Instances_(0, null);
System.Collections.IEnumerator objEnum = objset.GetEnumerator();
while (objEnum.MoveNext())
{
SWbemObject obj = (SWbemObject)objEnum.Current;
SWbemPropertySet propset = obj.Properties_;
System.Collections.IEnumerator propEnum = propset.GetEnumerator();
while (propEnum.MoveNext())
{
SWbemProperty prop = (SWbemProperty)propEnum.Current;
String Name = prop.Name;
string Value = checkme(prop);
tempstr = tempstr + Name + " = " + Value + "\n\r";
}
MessageBox.Show(tempstr);
tempstr = "";
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
SWbemLocator locator = new SWbemLocatorClass();
SWbemServices svc = locator.ConnectServer(".", "root\\CIMV2", "", "", "", "", 0, null);
SWbemSink Sink = new SWbemSinkClass();
svc.GetAsync(Sink, "Win32_Processor", 0, null, null);
Sink.OnObjectReady += new ISWbemSinkEvents_OnObjectReadyEventHandler(Sink_OnObjectReady);
}
private string checkme(SWbemProperty prop)
{
String value = "";
if(prop.get_Value() == null)
{
return "";
}
else
{
if(prop.IsArray == true)
{
Object[] myarray = (Object[])prop.get_Value();
for (int x = 0; x < myarray.GetLength(0) - 1; x++)
{
if (value != "")
{
value = value + ",";
}
value = value + myarray[x];
}
return value;
}
else
{
if(prop.CIMType == WbemCimtypeEnum.wbemCimtypeDatetime)
{
value = prop.get_Value().ToString();
if (value != "")
{
value = value.Substring(4, 2) + "/" + value.Substring(6, 2) + "/" + value.Substring(0, 4) + " " + value.Substring(8, 2) + ":" + value.Substring(10, 2) + ":" + value.Substring(12, 2);
}
return value;
}
else
{
return prop.get_Value().ToString();
}
}
}
}
}
WMI Coding Example Using C++
The following Code example is one of many that can be found at:
http://www.planetmps.com/
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
String^ tempstr = "";
String^ v = "";
ManagementObjectSearcher^ mos = gcnew ManagementObjectSearcher("ApplicationLifetimeEvent");
mos->Scope->Path->Path ="root\aspnet";
ManagementObjectCollection^ moc = mos->Get();
ManagementObjectCollection::ManagementObjectEnumerator^ mocEnum = moc->GetEnumerator();
while (mocEnum->MoveNext())
{
ManagementObject^ mo = dynamic_cast<ManagementObject^>(mocEnum->Current);
PropertyDataCollection^ propSet = mo->Properties;
PropertyDataCollection::PropertyDataEnumerator^ pEnum = propSet->GetEnumerator();
while (pEnum->MoveNext())
{
PropertyData^ prop = dynamic_cast<PropertyData^>(pEnum->Current);
if (prop->Value == nullptr)
{
tempstr = tempstr + "" + prop->Name + ":\n";
}
else
{
tempstr = tempstr + "" + prop->Name + ":" + GetValue(prop->Name, mo) + "\n";
}
}
}
MessageBox::Show(tempstr);
tempstr = "";
}
private: System::String^ GetValue(String^ Name, ManagementObject^ mo)
{
String^ tstr = mo->GetText(TextFormat::Mof);
Name = Name + " = ";
int pos = 0;
pos = tstr->IndexOf(Name);
if (pos > -1)
{
pos = pos + Name->Length;
tstr = tstr->Substring(pos, (tstr->Length - pos));
tstr = tstr->Substring(0, tstr->IndexOf(";"));
tstr = tstr->Replace("{", "");
tstr = tstr->Replace("}", "");
tstr = tstr->Replace("\x022", "");
tstr = tstr->Trim();
return tstr;
}
else
{
return "";
}
}
http://www.planetmps.com/
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
String^ tempstr = "";
String^ v = "";
ManagementObjectSearcher^ mos = gcnew ManagementObjectSearcher("ApplicationLifetimeEvent");
mos->Scope->Path->Path ="root\aspnet";
ManagementObjectCollection^ moc = mos->Get();
ManagementObjectCollection::ManagementObjectEnumerator^ mocEnum = moc->GetEnumerator();
while (mocEnum->MoveNext())
{
ManagementObject^ mo = dynamic_cast<ManagementObject^>(mocEnum->Current);
PropertyDataCollection^ propSet = mo->Properties;
PropertyDataCollection::PropertyDataEnumerator^ pEnum = propSet->GetEnumerator();
while (pEnum->MoveNext())
{
PropertyData^ prop = dynamic_cast<PropertyData^>(pEnum->Current);
if (prop->Value == nullptr)
{
tempstr = tempstr + "" + prop->Name + ":\n";
}
else
{
tempstr = tempstr + "" + prop->Name + ":" + GetValue(prop->Name, mo) + "\n";
}
}
}
MessageBox::Show(tempstr);
tempstr = "";
}
private: System::String^ GetValue(String^ Name, ManagementObject^ mo)
{
String^ tstr = mo->GetText(TextFormat::Mof);
Name = Name + " = ";
int pos = 0;
pos = tstr->IndexOf(Name);
if (pos > -1)
{
pos = pos + Name->Length;
tstr = tstr->Substring(pos, (tstr->Length - pos));
tstr = tstr->Substring(0, tstr->IndexOf(";"));
tstr = tstr->Replace("{", "");
tstr = tstr->Replace("}", "");
tstr = tstr->Replace("\x022", "");
tstr = tstr->Trim();
return tstr;
}
else
{
return "";
}
}
WMI Coding Example Using Borland C Builder
This code snippet is one of many that can be found at:
http://www.planetmps.com/
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
String GetValue(String N, String tempstr)
{
int pos = 0;
String Ne = N + " = ";
pos = AnsiPos(Ne, tempstr);
if(pos > 0)
{
pos = pos + Ne.Length();
int l = tempstr.Length();
tempstr = tempstr.SubString(pos, l);
pos = AnsiPos(";", tempstr);
tempstr = tempstr.SubString(2, pos -3);
return tempstr;
}
else
{
return 0x20 + 0x20;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
LPUNKNOWN punkEnum;
LPUNKNOWN punkEnum1;
LPUNKNOWN pEnum;
IEnumVARIANT * mocEnum=NULL;
IEnumVARIANT * mocEnum1=NULL;
IEnumVARIANT * propEnum=NULL;
VARIANT obj;
VARIANT obj1;
VariantInit(&obj);
int nIndex;
unsigned long c;
unsigned long cc;
String mystr;
VARIANT prop;
VariantInit(&prop);
int nIndex1;
unsigned long cc1;
VariantInit(&obj1);
int nIndex2;
unsigned long cc2;
Variant l = Variant::CreateObject("Wbemscripting.SwbemLocator");
Variant svc = l.OleFunction("ConnectServer", ".", "root\\cimv2");
Variant objs = svc.OleFunction("instancesOf", "Win32_Process");
punkEnum = objs.OlePropertyGet("_NewEnum");
punkEnum->QueryInterface(IID_IEnumVARIANT, (LPVOID far*)&mocEnum);
mocEnum->AddRef();
punkEnum->Release();
while(mocEnum->Next(1, &obj, &c) == S_OK)
{
Variant obj1 = (Variant(obj.pdispVal));
Variant PropSet = obj1.OlePropertyGet("Properties_");
pEnum = PropSet.OlePropertyGet("_NewEnum");
IEnumVARIANT * propEnum=NULL;
tagVARIANT tprop;
unsigned long d = 0;
if(pEnum->QueryInterface(IID_IEnumVARIANT, (LPVOID far*)&propEnum) == S_OK)
{
while(propEnum->Next(1, &tprop, &d) == S_OK)
{
String Name = Variant(tprop).OlePropertyGet("Name");
String OT = obj1.OleFunction("GetObjectText_");
mystr = mystr + Name + ": " + GetValue(Name, OT) + "\n";
}
Variant wsh = Variant::CreateObject("WScript.Shell");
wsh.OleFunction("Popup", OleVariant(mystr));
mystr="";
break;
}
}
}
http://www.planetmps.com/
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
String GetValue(String N, String tempstr)
{
int pos = 0;
String Ne = N + " = ";
pos = AnsiPos(Ne, tempstr);
if(pos > 0)
{
pos = pos + Ne.Length();
int l = tempstr.Length();
tempstr = tempstr.SubString(pos, l);
pos = AnsiPos(";", tempstr);
tempstr = tempstr.SubString(2, pos -3);
return tempstr;
}
else
{
return 0x20 + 0x20;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
LPUNKNOWN punkEnum;
LPUNKNOWN punkEnum1;
LPUNKNOWN pEnum;
IEnumVARIANT * mocEnum=NULL;
IEnumVARIANT * mocEnum1=NULL;
IEnumVARIANT * propEnum=NULL;
VARIANT obj;
VARIANT obj1;
VariantInit(&obj);
int nIndex;
unsigned long c;
unsigned long cc;
String mystr;
VARIANT prop;
VariantInit(&prop);
int nIndex1;
unsigned long cc1;
VariantInit(&obj1);
int nIndex2;
unsigned long cc2;
Variant l = Variant::CreateObject("Wbemscripting.SwbemLocator");
Variant svc = l.OleFunction("ConnectServer", ".", "root\\cimv2");
Variant objs = svc.OleFunction("instancesOf", "Win32_Process");
punkEnum = objs.OlePropertyGet("_NewEnum");
punkEnum->QueryInterface(IID_IEnumVARIANT, (LPVOID far*)&mocEnum);
mocEnum->AddRef();
punkEnum->Release();
while(mocEnum->Next(1, &obj, &c) == S_OK)
{
Variant obj1 = (Variant(obj.pdispVal));
Variant PropSet = obj1.OlePropertyGet("Properties_");
pEnum = PropSet.OlePropertyGet("_NewEnum");
IEnumVARIANT * propEnum=NULL;
tagVARIANT tprop;
unsigned long d = 0;
if(pEnum->QueryInterface(IID_IEnumVARIANT, (LPVOID far*)&propEnum) == S_OK)
{
while(propEnum->Next(1, &tprop, &d) == S_OK)
{
String Name = Variant(tprop).OlePropertyGet("Name");
String OT = obj1.OleFunction("GetObjectText_");
mystr = mystr + Name + ": " + GetValue(Name, OT) + "\n";
}
Variant wsh = Variant::CreateObject("WScript.Shell");
wsh.OleFunction("Popup", OleVariant(mystr));
mystr="";
break;
}
}
}
Subscribe to:
Posts (Atom)