<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>API Programming &#187; Windows Management Instrumentation (WMI)</title>
	<atom:link href="http://www.brangle.com/wordpress/tag/windows-management-instrumentation-wmi/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brangle.com/wordpress</link>
	<description>Just another computer weblog</description>
	<lastBuildDate>Wed, 14 Dec 2011 07:35:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Send eMail and SMS Text Messages with PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/08/send-email-messages-with-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/send-email-messages-with-powershell/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 05:03:04 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows Management Instrumentation (WMI)]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=358</guid>
		<description><![CDATA[Thanks to the .NET library sending emails with PowerShell is surprisingly very simple! One thing to remember is line breaks, when you need a new line, make sure to use the escape characters <i>`n</i> . In the first example, I'll send just a regular email, in the second I'll send a SMS text message. [...]]]></description>
			<content:encoded><![CDATA[<p>Thanks to the .NET library sending emails with PowerShell is surprisingly very simple!  One thing to remember is line breaks, when you need a new line, make sure to use the escape characters <i>`n</i> . In the first example, I&#8217;ll send just a regular email, in the second I&#8217;ll send a SMS text message.<span id="more-358"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$emailFrom</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;sender@example.com&quot;</span>
<span style="color: #800080;">$emailTo</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;recipient@example.com&quot;</span>
&nbsp;
<span style="color: #800080;">$subject</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Subject&quot;</span>
<span style="color: #800080;">$body</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;This is my automated email<span style="color: #008080; font-weight: bold;">`n</span>This is line number 2&quot;</span>
<span style="color: #800080;">$smtpServer</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost&quot;</span>
&nbsp;
<span style="color: #800080;">$smtp</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> Net.Mail.SmtpClient<span style="color: #000000;">&#40;</span><span style="color: #800080;">$smtpServer</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$smtp</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800080;">$emailFrom</span><span style="color: pink;">,</span> <span style="color: #800080;">$emailTo</span><span style="color: pink;">,</span> <span style="color: #800080;">$subject</span><span style="color: pink;">,</span> <span style="color: #800080;">$body</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Now let&#8217;s send a SMS text massage alert, in this message we will include how much free space we have on our C: drive.  Most cell carriers limit text messages to 160 characters, so we want to keep them as short and useful as possible. Check the list below to see see what the email address of your phone is and make sure to replace the <i>$to</i> variable.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$freeSpace</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">gwmi</span> win32_logicaldisk <span style="color: pink;">-</span><span style="color: #0000FF;">filter</span> <span style="color: #800000;">&quot;DeviceID=<span style="color: #008080; font-weight: bold;">`&quot;</span>C:<span style="color: #008080; font-weight: bold;">`&quot;</span>&quot;</span> <span style="color: pink;">|</span> <span style="color: pink;">%</span> <span style="color: #000000;">&#123;</span> <span style="color: #000080;">$_</span>.freespace<span style="color: pink;">/</span>1GB <span style="color: #000000;">&#125;</span>
<span style="color: #800080;">$freeSpace</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Math<span style="color: #000000;">&#93;</span>::round<span style="color: #000000;">&#40;</span><span style="color: #800080;">$FreeSpace</span><span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #800080;">$from</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;from@example.com&quot;</span>
<span style="color: #800080;">$to</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;1234567890@example.com&quot;</span>
&nbsp;
<span style="color: #800080;">$subject</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span>
<span style="color: #800080;">$body</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C: free space is &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$freeSpace</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; GB&quot;</span>
&nbsp;
<span style="color: #800080;">$smtpServer</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost&quot;</span>
<span style="color: #800080;">$smtp</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> Net.Mail.SmtpClient<span style="color: #000000;">&#40;</span><span style="color: #800080;">$smtpServer</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$smtp</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800080;">$from</span><span style="color: pink;">,</span> <span style="color: #800080;">$to</span><span style="color: pink;">,</span> <span style="color: #800080;">$subject</span><span style="color: pink;">,</span> <span style="color: #800080;">$body</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Now we need to figure out what email address to use so that we can send the SMS address, below are a few examples for several US carriers.  Make sure to replace 1234567890 with your ten digit phone number.</p>
<p>Alltel<br />
1234567890@message.alltel.com</p>
<p>AT&#038;T<br />
1234567890@txt.att.net</p>
<p>Boost Mobile<br />
1234567890@myboostmobile.com</p>
<p>MetroPCS<br />
1234567890@mymetropcs.com</p>
<p>Nextel<br />
1234567890@messaging.nextel.com</p>
<p>Sprint PCS<br />
1234567890@messaging.sprintpcs.com</p>
<p>T-Mobile<br />
1234567890@tmomail.net</p>
<p>Verizon<br />
1234567890@vtext.com</p>
<p>Virgin Mobile<br />
1234567890@vmobl.com</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/send-email-messages-with-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pass Credentials via PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/08/pass-credentials-via-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/pass-credentials-via-powershell/#comments</comments>
		<pubDate>Sun, 23 Aug 2009 20:33:07 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows Management Instrumentation (WMI)]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=269</guid>
		<description><![CDATA[I've often found it necessary to pass my Administrator domain or Domain Admin (DomAdmin) credentials to run a PowerShell WMI command. Fortunately PowerShell provides 3 different ways of doing just that, not counting using Windows built in runas command. The three methods I'm going to go over are <ol><li>Entering your own unique credentials</li> <li>Entering a service account password, ie: the same username (or hard coding a username) <li>Hard coding a user name and password</li></ol> [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve often found it necessary to pass my Administrator domain or Domain Admin (DomAdmin) credentials to run a PowerShell WMI command.  Fortunately PowerShell provides 3 different ways of doing just that, not counting using Windows built in runas command.  The three methods I&#8217;m going to go over are</p>
<ol>
<li>Entering your own unique credentials</li>
<li>Entering a service account password, ie: the same username (or hard coding a username)
<li>Hard coding a user name and password</li>
</ol>
<p><span id="more-269"></span></p>
<p><br /><br /></p>
<p><b>Method 1:</b><div id="attachment_297" class="wp-caption alignleft" style="width: 310px"><a href="http://www.brangle.com/wordpress/wp-content/uploads/2009/08/WindowsPowerShellCredentialsRequest.jpg"><img src="http://www.brangle.com/wordpress/wp-content/uploads/2009/08/WindowsPowerShellCredentialsRequest-300x233.jpg" alt="Windows PowerShell Credentials Request Box" title="Windows PowerShell Credentials Request" width="300" height="233" class="size-medium wp-image-297" /></a><p class="wp-caption-text">Windows PowerShell Credentials Request Box</p></div> Entering your own unique credentials, this will popup the standard windows login, let&#8217;s store our credentials to a variable called <i>$cred</i>, the benefits of doing this if we want to run multiple commands all under this account we can just recall our credentials without having to enter them every single time.  In this example, after I retrieve my stored credentials I&#8217;ll check to see who is logged onto a computer named dev-ghost.  I&#8217;ll be using <i>gwmi</i> cmdlet which is just an alias for the <i>Get-WmiObject</i> cmdlet.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> C:\<span style="color: pink;">&gt;</span> <span style="color: #800080;">$cred</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Credential</span>
&nbsp;
cmdlet <span style="color: #008080; font-weight: bold;">Get-Credential</span> at command pipeline position <span style="color: #804000;">1</span>
Supply values <span style="color: #0000FF;">for</span> the following parameters:
Credential
<span style="color: #008080; font-weight: bold;">PS</span> C:\<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">gwmi</span> win32_LoggedOnUser <span style="color: pink;">-</span>computer <span style="color: #800000;">&quot;dev-ghost&quot;</span> <span style="color: #008080; font-style: italic;">-credential</span> $cred</pre></td></tr></table></div>

<p><br /><br /><br />
<b>Method 2</b><br />
In this example, we are going to do the same as above, except we will specify the username, this is perfect for whenever you use service accounts.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> C:\<span style="color: pink;">&gt;</span>PS C:\<span style="color: pink;">&gt;</span> <span style="color: #800080;">$cred</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Credential</span> <span style="color: #800000;">&quot;dev-ghost\administrator&quot;</span>
<span style="color: #008080; font-weight: bold;">PS</span> C:\<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">gwmi</span> win32_LoggedOnUser <span style="color: pink;">-</span>computer <span style="color: #800000;">&quot;dev-ghost&quot;</span> <span style="color: #008080; font-style: italic;">-credential</span> $cred</pre></td></tr></table></div>

<p>Now let&#8217;s do the same as above, but instead of saving our credentials to a variable, we will just use it once and call it inline.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> C:\<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">gwmi</span> win32_LoggedOnUser <span style="color: pink;">-</span>computer <span style="color: #800000;">&quot;dev-ghost&quot;</span> <span style="color: #008080; font-style: italic;">-credential</span> <span style="color: #800000;">&quot;dev-ghost\administrator&quot;</span></pre></td></tr></table></div>

<p><br /><br /><br />
<b>Method 3</b><br />
I don&#8217;t think password should ever be hard coded into scripts, especially ones that can be decrypted.  PowerShell provides a SecureString, this is nothing more than a joke. The reason I call it a joke is because anyone can decrypt a SecureString if they know a little bit of PS.  But ignorance is bliss right? So let&#8217;s get to it&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> C:\windows\system32\windowspowershell\v1.0<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">Read-Host</span> <span style="color: #008080; font-style: italic;">-AsSecureString</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">ConvertFrom-SecureString</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out<span style="color: #FF0000;">-File</span></span> C:\PowerShell\MyPassword.txt</pre></td></tr></table></div>

<p>Once you press enter just type in your password, this will save your password to a file called MyPassword.txt. So now that the password is saved as a &quot;SecureString&quot; we can now use these saved credentials.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$password</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">type</span> C:\PowerShell\MyPassword.txt <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">ConvertTo-SecureString</span>
<span style="color: #800080;">$cred</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-TypeName</span> System.Management.Automation.PSCredential <span style="color: #008080; font-style: italic;">-argumentlist</span> “administrator”<span style="color: pink;">,</span>$password</pre></td></tr></table></div>

<p> and that&#8217;s it, your all set to start impersonating your credentials so that you can run scripts or executable under your service accounts.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/pass-credentials-via-powershell/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Accessing WMI Information From PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/07/accessing-wmi-information-from-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/07/accessing-wmi-information-from-powershell/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 06:51:09 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows Management Instrumentation (WMI)]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=8</guid>
		<description><![CDATA[I'm starting to learn how powerful the Windows Management Instrumentation aka Windows WMI is. One of the simpliest ways I found of accessing the information is via Windows PowerShell. Here is a simple PowerShell command that will return the Win32_OperatingSystem information... [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m starting to learn how powerful the Windows Management Instrumentation aka Windows WMI is. One of the simpliest ways I found of accessing the information is via Windows PowerShell. Here is a simple PowerShell command that will return the Win32_OperatingSystem information.<span id="more-8"></span></p>
<pre><span>Get-WmiObject Win32_OperatingSystem</span></pre>
<p>The following command returned the following on my Windows XP box, and yes, anywhere you see XXXXX I censored the data for my personal privacy&#8230;<br />
<span><br />
</span></p>
<pre><span>SystemDirectory : C:\WINDOWS\system32
Organization    : XXXXX
BuildNumber     : 2600
RegisteredUser  : XXXXX
SerialNumber    : XXXXX-XXX-XXXXXXX-XXXXX
Version         : 5.1.2600</span></pre>
<p>If we use a pipe command, we can return the information in a table<br />
<span><br />
</span></p>
<pre><span>PS H:\powershell&gt; Get-WmiObject Win32_OperatingSystem | format-table

SystemDirecto Organization  BuildNumber  RegisteredUs SerialNumber Version
ry                                       er
------------- ------------  -----------  ------------ ------------ -------
C:\WINDOWS... XXXXX...      2600         XXXXX...     XXXXX-XXX... 5.1.2600</span></pre>
<p>Of course, what if we just wanted to return the Windows Version and not all of the miscellaneous/irrelevant data? We can pipe the Get-Member command to see all of the properties that are hidden in that class.</p>
<pre><span>Get-WmiObject Win32_OperatingSystem | Get-Member</span></pre>
<p>Running this command shows us a list of MemberType, if you see a MemberType of Property we can query to see what is contained within that property field by using the Format Table command &#8220;ft.&#8221; For example</p>
<pre><span>PS H:\powershell&gt; Get-WmiObject Win32_OperatingSystem | ft Version, ServicePackMajorVersion

Version                                                 ServicePackMajorVersion
-------                                                 -----------------------
5.1.2600                                                                      3</span></pre>
<p>Now, what if we want to access information on a remote client or server? Simple! just concat the computer name to the WMI call. Just make sure to change &#8220;ComputerName&#8221; to the client/server name.</p>
<pre><span>PS H:\powershell&gt; Get-WmiObject Win32_OperatingSystem -computer "ComputerName" | ft Version, ServicePackMajorVersion</span></pre>
<p>That&#8217;s great, but if you don&#8217;t have access to client/server under your normal Windows or AD credentials, we can authenticate as a different user. To do this we are going to assign our credentials to the variable $cred (short for credentials), you can change this name to whatever you want. Once we&#8217;ve logged in, we will pass those to the client or server that we are trying to authenticate against to login and view the WMI system info. When we call Get-Credential this causes a Windows popup asking us to login.</p>
<pre><span>PS H:\powershell&gt; $cred = Get-Credential

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential

PS H:\powershell&gt; Get-WmiObject Win32_OperatingSystem -computer dev-ghost -Credential $cred

SystemDirectory : C:\WINDOWS\system32
Organization    : XXXXX
BuildNumber     : 3790
RegisteredUser  : XXXXX
SerialNumber    : XXXXX-XXX-XXXXXXX-XXXXX
Version         : 5.2.3790</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/07/accessing-wmi-information-from-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

