######################################################### # Script Name: PS_Remote_WMI_Installed_Applications.ps1 # Version: 1.0 # Description: Using WMI Remotely Queries # Systems for Installed Software ######################################################### #Array for Reporting Installed Software $installedApps = @(); #Array for Registry Paths to Installed Apps $appRegPaths = @("Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall", "Software\Microsoft\Windows\CurrentVersion\Uninstall"); #Array of System Names to Run Against $computers = @("SERVER01","SERVER02","SERVER03"); foreach($computer in $computers) { #Ping System First if(Test-Connection -ComputerName $computer -Quiet) { #Connect to WMI Registry Class $uReg = [wmiclass]"\\$computer\root\default:StdRegProv"; foreach($regPath in $appRegPaths) { #Pull the Application Registry Keys $iAppKeys = $uReg.EnumKey(2147483650,$regPath); #Null Check on Application Registry Keys if($iAppKeys) { #Loop Through Each Application Key foreach($appKey in $iAppKeys.sNames) { #Construct Key Path $keyPath = $regPath + "\" + $appKey.ToString(); #Pull the Key DisplayName String Value $keyDisplayName = $uReg.GetStringValue(2147483650,$keyPath,"DisplayName"); if(![string]::IsNullOrEmpty($keyDisplayName.sValue)) { #Local Vars Used for Reporting [string]$displayName = $keyDisplayName.sValue.ToString(); [string]$displayVersion = ""; #Pull the Key DisplayVersion String Value $keyDisplayVersion = $uReg.GetStringValue(2147483650,$keyPath,"DisplayVersion"); if(![string]::IsNullOrEmpty($keyDisplayVersion.sValue)) { $displayVersion = $keyDisplayVersion.sValue.ToString(); } #Create Custom PSObject and Add to Reporting Array $app = New-Object PSObject; $app | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $computer; $app | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $displayName; $app | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $displayVersion; $installedApps += $app; }#End of Null\Empty Check on DisplayName String Value }#End of Foreach $iAppKeys }#End of Null Check on $iAppKeys }#End of Foreach Reg Path }#End of Test-Connection }#End of Foreach Computer $installedApps | Sort-Object ComputerName,DisplayName | Format-Table -AutoSize;
Showing posts with label WMI. Show all posts
Showing posts with label WMI. Show all posts
Wednesday, September 5, 2012
PowerShell: WMI to Report Installed Applications on Remote Systems
Recently, I've been working with more PowerShell Remoting. Earlier this week I wrote a script that queries a list of systems for installed applications using Remoting. Tomorrow, I'm demo'ing that script (which I will post after this one) and needed to show how you would do it using just WMI in PowerShell. Since the listing of installed applications is stored in the Registery it took a good amount of time trying to figure out how to access a remote registry via PowerShell just using WMI. Below is the comparison script. Enjoy.
Labels:
Installed Applications,
PowerShell,
Remote Registry,
WMI
Friday, August 10, 2012
PowerShell: WMI Basics
During my lunch hour today, I showed a few colleagues how to use of PowerShell with WMI. We mainly covered the Get-WMIObject command and a lot of fun things you can do with it. Below are the examples we went over. Enjoy.
############################################################ # WMI PowerShell Commands ############################################################ #Use -ComputerName Option with Hostname, FQDN, or IP Address in Command for Remote Systems #For Example to Get the BIOS Settings On a System Called DeanTestServer Get-WmiObject -Query "SELECT * FROM Win32_BIOS" -ComputerName "DeanTestServer" #Or Use the IP Address Get-WmiObject -Query "SELECT * FROM Win32_BIOS" -ComputerName "192.168.2.25" #Get-WMIObject Can Use A WOL Query, Filter, or PowerShell Where Statement to Limit Results #For Example the Following Three Commands Have the Same Result Get-WmiObject -Class Win32_Share | Where-Object { $_.Name -eq "C$" } Get-WmiObject -Class Win32_Share -Filter "Name='C$'" Get-WmiObject -Query "SELECT * FROM Win32_Share WHERE Name='C$'" #Get All Win_32 Classes in the CIMV2 Namespace Get-WmiObject -Namespace "root\cimv2" -List | Where-Object { $_.Name -like "Win32_*" } | Select-Object Name | Sort-Object Name | Out-File WMI_CIMV2_Classes.txt #Get All Properties and Methods for the WMI Class Get-WmiObject Win32_Volume | Get-Member #Get Basic System Information Get-WmiObject -Query "SELECT * FROM Win32_ComputerSystem" #Get Local Accounts and Groups on a System Get-WmiObject -Query "SELECT * FROM Win32_Account" | Select-Object Name,SID | Sort-Object Name #Get Disk Information (Model and Size) Get-WmiObject -Query "SELECT * FROM Win32_DiskDrive" #Get Processor Information Get-WmiObject -Query "SELECT * FROM Win32_Processor" | Select-Object Name,Description,NumberOfCores | Sort-Object Name #Get Operating System Info Get-WmiObject -Query "SELECT * FROM Win32_OperatingSystem" #Get MAC Addresses of All Network Adapters Get-WmiObject -Query "SELECT * FROM Win32_NetworkAdapter WHERE MACAddress IS NOT NULL" | Select-Object Name,MACAddress | Sort-Object Name; #Get All Assigned IPs Get-WmiObject -Query "SELECT * FROM Win32_NetworkAdapterConfiguration" | Where-Object { $_.IPAddress -ne $null} | Select-Object Description,IPAddress; #List Number of Memory Slots on a System Write-Output ("Number of Memory Slots: " + (Get-WmiObject -Query "SELECT * FROM win32_PhysicalMemoryArray").MemoryDevices); #Retrieve Memory Slot Allocations Get-WMIObject -Query "SELECT * FROM Win32_PhysicalMemory" | ForEach-Object { Write-Output ($_.DeviceLocator.ToString() + " " + ($_.Capacity/1GB) + "GB") }; #Retrieve Disk Volume Sizes (Including Mount Points, Excluding Pass Through Drives) $sysVolumes = Get-WmiObject –Query "Select * FROM Win32_Volume WHERE DriveType=3 AND NOT Name LIKE '%?%'" | Sort-Object Name; foreach($sv in $sysVolumes) { #Var for Volume Size $vSize = "{0:N2}" -f ($sv.Capacity/1GB); #Var for Free Space $vFS = "{0:N2}" -f ($sv.FreeSpace/1GB); #Var for Percentage Free Space $vPF = "{0:N2}" -f (($sv.FreeSpace/$sv.Capacity)*100); #Var for Drive Letter $vLetter = $sv.Name.ToString().TrimEnd("\"); $VolumeStatus = "$vLetter | Size(GB): $vSize | Free Space(GB): $vFS | Percentage Free: $vPF"; Write-Output $VolumeStatus; }
Wednesday, April 1, 2009
Remote WMI (System.Management) and Wake on LAN in an ASP.NET C#
Below is a solution I came up with for a request of an ASP.NET site that would allow a network admin to perform basic admin actions on AD Windows systems. Using the System.Management and System.Net.Sockets namespaces I was able to create a site that allows a user to power on, reboot, and query info (Drive Size, Processer info, User Logged On, etc...) from those systems. I like to refer to it as my poor man's version of SMS.
Reference Links for C# Remote Command Line and Wake on LAN:
http://www.dalun.com/blogs/05.09.2007.htm
http://www.codeproject.com/KB/IP/cswol.aspx
-------------RWMI.aspx.cs-------------------
using System;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Data;
using System.Text;
using System.IO;
using System.Management;
public partial class RWMI : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Load DataTable and GridView with Demo System Info...Configure Application
//to Pull Real Data from Either SQL or Local Text File
//Column 0 = Computer Name
//Column 1 = IP Address
//Column 2 = MAC Address (Format xx:xx:xx:xx:xx:xx)
DataTable dt = new DataTable();
dt.Columns.Add("system", typeof(System.String));
dt.Columns.Add("ipaddress", typeof(System.String));
dt.Columns.Add("macaddress", typeof(System.String));
DataRow dr = dt.NewRow();
dr[0] = "System One";
dr[1] = "192.168.1.100";
dr[2] = "00:1c:23:53:e4:38";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1[0] = "System Two";
dr1[1] = "192.168.1.101";
dr1[2] = "00:b0:d0:07:6f:e0";
dt.Rows.Add(dr1);
gvComputers.DataSource = dt;
gvComputers.DataBind();
}
protected void CheckStatus(object sender, GridViewRowEventArgs e)
{
//Upon Databound Event, Ping IP to See If It's Up...Disable Power On Button
//If Not Then Disable Restart and Info Buttons
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btn = new Button();
btn = (Button)e.Row.Cells[3].Controls[0];
Button btn1 = new Button();
btn1 = (Button)e.Row.Cells[4].Controls[0];
Button btn2 = new Button();
btn2 = (Button)e.Row.Cells[5].Controls[0];
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply replyPing = pingSender.Send(e.Row.Cells[1].Text, timeout, buffer, options);
if (replyPing.Status != IPStatus.Success)
{
e.Row.CssClass = "NotGood";
btn.Enabled = true;
btn1.Enabled = false;
btn2.Enabled = false;
}
else
{
btn.Enabled = false;
btn1.Enabled = true;
btn2.Enabled = true;
}
}
}
protected void gvComputer_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
//Finding Selected Row and Passing that Info to Function
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvComputers.Rows[index];
//Creating Connection Options
ConnectionOptions coWMI = new ConnectionOptions();
//User Account Settings for Remote WMI Connection...Store Password in Web.Config for Better Security
coWMI.Username = "AdminUserID"; //AD Account That is an Admin on Local Systems
coWMI.Password = "AdminPassword"; //Password for that Account
coWMI.Authority = "NTLMDOMAIN:XXX"; //XXX is Your Domain
//Hide System Info Cell
tc2.Visible = false;
//Switch Statement for Button Command
switch (e.CommandName)
{
case "PowerOn":
TurnOnMAC(row.Cells[2].Text.ToString());
break;
case "Restart":
//Creates Remote Process on Selected System to Restart it in One Minute
ManagementScope msRP = new ManagementScope("\\\\" + row.Cells[1].Text.ToString() + "\\root\\cimv2", coWMI);
msRP.Connect();
ObjectGetOptions ogoRP = new ObjectGetOptions();
ManagementPath mpRP = new ManagementPath("Win32_Process");
ManagementClass mcRP = new ManagementClass(msRP, mpRP, ogoRP);
ManagementBaseObject inParams = mcRP.GetMethodParameters("Create");
inParams["CommandLine"] = @"shutdown /r /t 60";
ManagementBaseObject outParams = mcRP.InvokeMethod("Create", inParams, null);
break;
case "Info":
ArrayList alSoftware = new ArrayList();
ArrayList alDrives = new ArrayList();
ArrayList alProcess = new ArrayList();
//Setup Connection to Remote Systems root\cimv2
ManagementScope msWMI = new ManagementScope("\\\\" + row.Cells[1].Text.ToString() + "\\root\\cimv2", coWMI);
msWMI.Connect();
//WMI Query all Software Installed on System...Not Complete Listing...Only Software that writes to Certain Area in Registry
ObjectQuery oqSoftware = new ObjectQuery("Select Name from Win32_Product");
ManagementObjectSearcher mosSoftware = new ManagementObjectSearcher(msWMI, oqSoftware);
foreach (ManagementObject oReturn in mosSoftware.Get())
{
alSoftware.Add(oReturn["Name"].ToString());
}
alSoftware.Sort();
rptSoftware.DataSource = alSoftware.ToArray();
rptSoftware.DataBind();
//WMI Query for Computer Name, OS, RAM, and Last Bootup Time
ObjectQuery oqComputer = new ObjectQuery("Select * from Win32_OperatingSystem");
ManagementObjectSearcher mosComputer = new ManagementObjectSearcher(msWMI, oqComputer);
foreach (ManagementObject oReturn in mosComputer.Get())
{
lblComputerName.Text = oReturn["CSName"].ToString();
lblOS.Text = oReturn["Caption"].ToString() + " " + oReturn["CSDVersion"].ToString();
Decimal dRam = Convert.ToDecimal(oReturn["TotalVisibleMemorySize"].ToString());
dRam = dRam / 1000000M;
lblRam.Text = dRam.ToString("0.000") + " GBs";
lblBootTime.Text = ManagementDateTimeConverter.ToDateTime(oReturn["LastBootUpTime"].ToString()).ToString();
}
//WMI Query for Local Hard Drives...Calculate Free and Total Space
ObjectQuery oqDrives = new ObjectQuery("Select * from Win32_LogicalDisk WHERE DriveType=3");
ManagementObjectSearcher mosDrives = new ManagementObjectSearcher(msWMI, oqDrives);
foreach (ManagementObject oDrive in mosDrives.Get())
{
Decimal dFree = Convert.ToDecimal(oDrive["FreeSpace"].ToString());
dFree = dFree / 1073741824M;
Decimal dSize = Convert.ToDecimal(oDrive["Size"].ToString());
dSize = dSize / 1073741824M;
alDrives.Add("Drive: " + oDrive["DeviceID"].ToString() + " Free Space: " + dFree.ToString("0.00") + " GBs Total Size: " + dSize.ToString("0.00") + " GBs");
}
alDrives.Sort();
rptDisks.DataSource = alDrives.ToArray();
rptDisks.DataBind();
//WMI Query for Processor Information
ObjectQuery oqProcessor = new ObjectQuery("Select * from Win32_Processor");
ManagementObjectSearcher mosProcessor = new ManagementObjectSearcher(msWMI, oqProcessor);
foreach (ManagementObject oProcess in mosProcessor.Get())
{
alProcess.Add(oProcess["DeviceID"].ToString() + ": " + oProcess["Name"].ToString());
}
alProcess.Sort();
rptProcessor.DataSource = alProcess.ToArray();
rptProcessor.DataBind();
//WMI Query for Locally Logged On User...Will Display Admin Account If No One Logged On
ObjectQuery oqUsers = new ObjectQuery("Select * from Win32_ComputerSystem");
ManagementObjectSearcher mosUsers = new ManagementObjectSearcher(msWMI, oqUsers);
foreach (ManagementObject oUser in mosUsers.Get())
{
lblUser.Text = oUser["UserName"].ToString();
}
tc2.Visible = true;
break;
}
}
catch
{
Response.Write("Error Accessing System");
}
}
protected void TurnOnMAC(string macAddress)
{
//Wake On LAN...Take MAC Address (Format xx:xx:xx:xx:xx:xx)
//Convert to Byte...Send a UDP Packet to Wake Up System
UdpClient client = new UdpClient();
client.Connect(IPAddress.Broadcast, 40000);
Byte[] datagram = new byte[102];
for (int i = 0; i <= 5; i++)
{
datagram[i] = 0xff;
}
string[] macDigits = macAddress.Split(':');
for (int i = 1; i <= 16; i++)
{
for (int x = 0; x < 6; x++)
{
datagram[i * 6 + x] = (byte)Convert.ToInt32(macDigits[x], 16);
}
}
client.Send(datagram, datagram.Length);
}
}
--------------------Portion of RWMI.aspx--------------------------------
< p> < strong> Remote WMI (Windows Systems Only)< /strong> < /p>
< asp:Table ID="tb1" runat="server" CellSpacing="5">
< asp:TableRow>
< asp:TableCell ID="tc1" VerticalAlign="Top" runat="server">
< asp:GridView ID="gvComputers" runat="server" SelectedIndex="0" Font-Size="Small" AutoGenerateColumns="false" OnRowCommand="gvComputer_RowCommand" OnRowDataBound="CheckStatus" CellPadding="5" BorderWidth="2" GridLines="Both" >
< Columns>
< asp:BoundField DataField="system" HeaderText="Computer Name" />
< asp:BoundField DataField="ipaddress" HeaderText="IP Address" />
< asp:BoundField DataField="macaddress" HeaderText="MAC Address" />
< asp:buttonfield buttontype="Button" commandname="PowerOn" text="Power On"/>
< asp:buttonfield buttontype="Button" commandname="Restart" text="Restart"/>
< asp:buttonfield buttontype="Button" commandname="Info" text="Info"/>
< /Columns>
< /asp:GridView>
< /asp:TableCell>
< asp:TableCell ID="tc2" VerticalAlign="Top" Visible="false" runat="server">
< table cellpadding="5" cellspacing="2" border="2">
< tr> < td> < strong> Computer Info for < asp:Label ID="lblComputerName" runat="server" /> < /strong> < /td> < /tr>
< tr> < td> < strong> Logged On User:< /strong> < asp:Label ID="lblUser" runat="server" /> < /td> < /tr>
< tr> < td> < strong> Last Bootup Time:< /strong> < asp:Label ID="lblBootTime" runat="server" /> < /td> < /tr>
< tr> < td> < strong> OS:< /strong> < asp:Label ID="lblOS" runat="server" /> < /td> < /tr>
< tr> < td> < strong> RAM:< /strong> < asp:Label ID="lblRam" runat="server" /> < /td> < /tr>
< tr>
< td> < strong> Processor(s):< /strong> < br />
< asp:Repeater ID="rptProcessor" runat="server">
< ItemTemplate>
< %# Container.DataItem %>
< /ItemTemplate>
< SeparatorTemplate>
< br />
< /SeparatorTemplate>
< /asp:Repeater>
< /td>
< /tr>
< tr>
< td> < strong> Local Disk(s):< /strong> < br />
< asp:Repeater ID="rptDisks" runat="server">
< ItemTemplate>
< %# Container.DataItem %>
< /ItemTemplate>
< SeparatorTemplate>
< br />
< /SeparatorTemplate>
< /asp:Repeater>
< /td>
< /tr>
< tr>
< td> < strong> Installed Applications:< /strong> < br />
< asp:Repeater ID="rptSoftware" runat="server">
< ItemTemplate>
< %# Container.DataItem %>
< /ItemTemplate>
< SeparatorTemplate>
< br />
< /SeparatorTemplate>
< /asp:Repeater>
< /td>
< /tr>
< /table>
< /asp:TableCell>
< /asp:TableRow>
< /asp:Table>
Reference Links for C# Remote Command Line and Wake on LAN:
http://www.dalun.com/blogs/05.09.2007.htm
http://www.codeproject.com/KB/IP/cswol.aspx
-------------RWMI.aspx.cs-------------------
using System;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Data;
using System.Text;
using System.IO;
using System.Management;
public partial class RWMI : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Load DataTable and GridView with Demo System Info...Configure Application
//to Pull Real Data from Either SQL or Local Text File
//Column 0 = Computer Name
//Column 1 = IP Address
//Column 2 = MAC Address (Format xx:xx:xx:xx:xx:xx)
DataTable dt = new DataTable();
dt.Columns.Add("system", typeof(System.String));
dt.Columns.Add("ipaddress", typeof(System.String));
dt.Columns.Add("macaddress", typeof(System.String));
DataRow dr = dt.NewRow();
dr[0] = "System One";
dr[1] = "192.168.1.100";
dr[2] = "00:1c:23:53:e4:38";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1[0] = "System Two";
dr1[1] = "192.168.1.101";
dr1[2] = "00:b0:d0:07:6f:e0";
dt.Rows.Add(dr1);
gvComputers.DataSource = dt;
gvComputers.DataBind();
}
protected void CheckStatus(object sender, GridViewRowEventArgs e)
{
//Upon Databound Event, Ping IP to See If It's Up...Disable Power On Button
//If Not Then Disable Restart and Info Buttons
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btn = new Button();
btn = (Button)e.Row.Cells[3].Controls[0];
Button btn1 = new Button();
btn1 = (Button)e.Row.Cells[4].Controls[0];
Button btn2 = new Button();
btn2 = (Button)e.Row.Cells[5].Controls[0];
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply replyPing = pingSender.Send(e.Row.Cells[1].Text, timeout, buffer, options);
if (replyPing.Status != IPStatus.Success)
{
e.Row.CssClass = "NotGood";
btn.Enabled = true;
btn1.Enabled = false;
btn2.Enabled = false;
}
else
{
btn.Enabled = false;
btn1.Enabled = true;
btn2.Enabled = true;
}
}
}
protected void gvComputer_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
//Finding Selected Row and Passing that Info to Function
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvComputers.Rows[index];
//Creating Connection Options
ConnectionOptions coWMI = new ConnectionOptions();
//User Account Settings for Remote WMI Connection...Store Password in Web.Config for Better Security
coWMI.Username = "AdminUserID"; //AD Account That is an Admin on Local Systems
coWMI.Password = "AdminPassword"; //Password for that Account
coWMI.Authority = "NTLMDOMAIN:XXX"; //XXX is Your Domain
//Hide System Info Cell
tc2.Visible = false;
//Switch Statement for Button Command
switch (e.CommandName)
{
case "PowerOn":
TurnOnMAC(row.Cells[2].Text.ToString());
break;
case "Restart":
//Creates Remote Process on Selected System to Restart it in One Minute
ManagementScope msRP = new ManagementScope("\\\\" + row.Cells[1].Text.ToString() + "\\root\\cimv2", coWMI);
msRP.Connect();
ObjectGetOptions ogoRP = new ObjectGetOptions();
ManagementPath mpRP = new ManagementPath("Win32_Process");
ManagementClass mcRP = new ManagementClass(msRP, mpRP, ogoRP);
ManagementBaseObject inParams = mcRP.GetMethodParameters("Create");
inParams["CommandLine"] = @"shutdown /r /t 60";
ManagementBaseObject outParams = mcRP.InvokeMethod("Create", inParams, null);
break;
case "Info":
ArrayList alSoftware = new ArrayList();
ArrayList alDrives = new ArrayList();
ArrayList alProcess = new ArrayList();
//Setup Connection to Remote Systems root\cimv2
ManagementScope msWMI = new ManagementScope("\\\\" + row.Cells[1].Text.ToString() + "\\root\\cimv2", coWMI);
msWMI.Connect();
//WMI Query all Software Installed on System...Not Complete Listing...Only Software that writes to Certain Area in Registry
ObjectQuery oqSoftware = new ObjectQuery("Select Name from Win32_Product");
ManagementObjectSearcher mosSoftware = new ManagementObjectSearcher(msWMI, oqSoftware);
foreach (ManagementObject oReturn in mosSoftware.Get())
{
alSoftware.Add(oReturn["Name"].ToString());
}
alSoftware.Sort();
rptSoftware.DataSource = alSoftware.ToArray();
rptSoftware.DataBind();
//WMI Query for Computer Name, OS, RAM, and Last Bootup Time
ObjectQuery oqComputer = new ObjectQuery("Select * from Win32_OperatingSystem");
ManagementObjectSearcher mosComputer = new ManagementObjectSearcher(msWMI, oqComputer);
foreach (ManagementObject oReturn in mosComputer.Get())
{
lblComputerName.Text = oReturn["CSName"].ToString();
lblOS.Text = oReturn["Caption"].ToString() + " " + oReturn["CSDVersion"].ToString();
Decimal dRam = Convert.ToDecimal(oReturn["TotalVisibleMemorySize"].ToString());
dRam = dRam / 1000000M;
lblRam.Text = dRam.ToString("0.000") + " GBs";
lblBootTime.Text = ManagementDateTimeConverter.ToDateTime(oReturn["LastBootUpTime"].ToString()).ToString();
}
//WMI Query for Local Hard Drives...Calculate Free and Total Space
ObjectQuery oqDrives = new ObjectQuery("Select * from Win32_LogicalDisk WHERE DriveType=3");
ManagementObjectSearcher mosDrives = new ManagementObjectSearcher(msWMI, oqDrives);
foreach (ManagementObject oDrive in mosDrives.Get())
{
Decimal dFree = Convert.ToDecimal(oDrive["FreeSpace"].ToString());
dFree = dFree / 1073741824M;
Decimal dSize = Convert.ToDecimal(oDrive["Size"].ToString());
dSize = dSize / 1073741824M;
alDrives.Add("Drive: " + oDrive["DeviceID"].ToString() + " Free Space: " + dFree.ToString("0.00") + " GBs Total Size: " + dSize.ToString("0.00") + " GBs");
}
alDrives.Sort();
rptDisks.DataSource = alDrives.ToArray();
rptDisks.DataBind();
//WMI Query for Processor Information
ObjectQuery oqProcessor = new ObjectQuery("Select * from Win32_Processor");
ManagementObjectSearcher mosProcessor = new ManagementObjectSearcher(msWMI, oqProcessor);
foreach (ManagementObject oProcess in mosProcessor.Get())
{
alProcess.Add(oProcess["DeviceID"].ToString() + ": " + oProcess["Name"].ToString());
}
alProcess.Sort();
rptProcessor.DataSource = alProcess.ToArray();
rptProcessor.DataBind();
//WMI Query for Locally Logged On User...Will Display Admin Account If No One Logged On
ObjectQuery oqUsers = new ObjectQuery("Select * from Win32_ComputerSystem");
ManagementObjectSearcher mosUsers = new ManagementObjectSearcher(msWMI, oqUsers);
foreach (ManagementObject oUser in mosUsers.Get())
{
lblUser.Text = oUser["UserName"].ToString();
}
tc2.Visible = true;
break;
}
}
catch
{
Response.Write("Error Accessing System");
}
}
protected void TurnOnMAC(string macAddress)
{
//Wake On LAN...Take MAC Address (Format xx:xx:xx:xx:xx:xx)
//Convert to Byte...Send a UDP Packet to Wake Up System
UdpClient client = new UdpClient();
client.Connect(IPAddress.Broadcast, 40000);
Byte[] datagram = new byte[102];
{
datagram[i] = 0xff;
}
string[] macDigits = macAddress.Split(':');
for (int i = 1; i <= 16; i++)
{
for (int x = 0; x < 6; x++)
{
datagram[i * 6 + x] = (byte)Convert.ToInt32(macDigits[x], 16);
}
}
client.Send(datagram, datagram.Length);
}
}
--------------------Portion of RWMI.aspx--------------------------------
< p> < strong> Remote WMI (Windows Systems Only)< /strong> < /p>
< asp:Table ID="tb1" runat="server" CellSpacing="5">
< asp:TableRow>
< asp:TableCell ID="tc1" VerticalAlign="Top" runat="server">
< asp:GridView ID="gvComputers" runat="server" SelectedIndex="0" Font-Size="Small" AutoGenerateColumns="false" OnRowCommand="gvComputer_RowCommand" OnRowDataBound="CheckStatus" CellPadding="5" BorderWidth="2" GridLines="Both" >
< Columns>
< asp:BoundField DataField="system" HeaderText="Computer Name" />
< asp:BoundField DataField="ipaddress" HeaderText="IP Address" />
< asp:BoundField DataField="macaddress" HeaderText="MAC Address" />
< asp:buttonfield buttontype="Button" commandname="PowerOn" text="Power On"/>
< asp:buttonfield buttontype="Button" commandname="Restart" text="Restart"/>
< asp:buttonfield buttontype="Button" commandname="Info" text="Info"/>
< /Columns>
< /asp:GridView>
< /asp:TableCell>
< asp:TableCell ID="tc2" VerticalAlign="Top" Visible="false" runat="server">
< table cellpadding="5" cellspacing="2" border="2">
< tr> < td> < strong> Computer Info for < asp:Label ID="lblComputerName" runat="server" /> < /strong> < /td> < /tr>
< tr> < td> < strong> Logged On User:< /strong> < asp:Label ID="lblUser" runat="server" /> < /td> < /tr>
< tr> < td> < strong> Last Bootup Time:< /strong> < asp:Label ID="lblBootTime" runat="server" /> < /td> < /tr>
< tr> < td> < strong> OS:< /strong> < asp:Label ID="lblOS" runat="server" /> < /td> < /tr>
< tr> < td> < strong> RAM:< /strong> < asp:Label ID="lblRam" runat="server" /> < /td> < /tr>
< tr>
< td> < strong> Processor(s):< /strong> < br />
< asp:Repeater ID="rptProcessor" runat="server">
< ItemTemplate>
< %# Container.DataItem %>
< /ItemTemplate>
< SeparatorTemplate>
< br />
< /SeparatorTemplate>
< /asp:Repeater>
< /td>
< /tr>
< tr>
< td> < strong> Local Disk(s):< /strong> < br />
< asp:Repeater ID="rptDisks" runat="server">
< ItemTemplate>
< %# Container.DataItem %>
< /ItemTemplate>
< SeparatorTemplate>
< br />
< /SeparatorTemplate>
< /asp:Repeater>
< /td>
< /tr>
< tr>
< td> < strong> Installed Applications:< /strong> < br />
< asp:Repeater ID="rptSoftware" runat="server">
< ItemTemplate>
< %# Container.DataItem %>
< /ItemTemplate>
< SeparatorTemplate>
< br />
< /SeparatorTemplate>
< /asp:Repeater>
< /td>
< /tr>
< /table>
< /asp:TableCell>
< /asp:TableRow>
< /asp:Table>
Subscribe to:
Posts (Atom)