<?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>The Ryan and Jeff Show &#187; Desktop</title>
	<atom:link href="http://www.ryanandjeffshow.com/blog/category/desktop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ryanandjeffshow.com/blog</link>
	<description>...Relax, we brought the towel (:P)</description>
	<lastBuildDate>Tue, 18 Aug 2015 05:23:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>PSG2013 Advanced Event 0: System Uptime</title>
		<link>http://www.ryanandjeffshow.com/blog/2013/04/22/psg2013-advanced-event-0-system-uptime/</link>
		<comments>http://www.ryanandjeffshow.com/blog/2013/04/22/psg2013-advanced-event-0-system-uptime/#comments</comments>
		<pubDate>Mon, 22 Apr 2013 06:14:34 +0000</pubDate>
		<dc:creator><![CDATA[Jeff Liford]]></dc:creator>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Server]]></category>

		<guid isPermaLink="false">http://www.ryanandjeffshow.com/blog/?p=216</guid>
		<description><![CDATA[This is the Advanced version of Event 0 for the 2013 PowerShell Scripting Games Goal: A Script that reports system uptime for a collection of computers. It must accept one or more computers by parameter. May be either name or<span class="ellipsis">&#8230;</span><div class="read-more"><a href="http://www.ryanandjeffshow.com/blog/2013/04/22/psg2013-advanced-event-0-system-uptime/">Read more &#8250;</a></div><!-- end of .read-more -->]]></description>
				<content:encoded><![CDATA[<p>This is the <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2013/04/18/advanced-practice-for-2013-scripting-games.aspx">Advanced version of Event 0</a> for the <a href="http://scriptinggames.org/home.php">2013 PowerShell Scripting Games</a></p>
<p><em>Goal:</em> A Script that reports system uptime for a collection of computers.  It must accept one or more computers by parameter.  May be either name or IP Address.  The parameter must accept input from the pipeline by object or string array, or direct string input.  The script must prompt for names if none are provided.  The script must output an object that has the name of the machine (even if only IP was provided) as well as total hours, minutes, seconds of uptime as individual properties.  It must optionally offer status/connection information as it runs.  It must control for connection errors and report a suitable error message on the shell. It must have a switch that enables logging of failed connections to a file.  Without the switch, there should be no logging.  The output must be compatible with CSV/XML/HTML output from the shell.</p>
<p>Result: Success!  This one took a little time.  I went the extra mile to ensure I had help information, and make sure everything works just right.  This script (.\Get-Uptime) can take string arrays, direct input, or piped input (with aliases for object ins) from the shell or pipeline, and puts out a timespan object.  Most of the actual work is done by the same code from the beginner track with slight editing so I can do error handling, so I wont re-explain it here.  I realized I made a mistake on the beginner track, as I alloted for days instead of total hours.  This information was available to me I just didn&#8217;t grab it.  It&#8217;s corrected here in the expression Hours expressed as {[int]$_.totalhours}.  This script appropriate expresses total hours (as a rounded integer) along with minutes/seconds.  This results in a table output by default (5 props defaults to a list).</p>
<p><span id="more-216"></span></p>
<p>Edit: Special thanks to mjolinor over at <a href="http://PowerShell.org">PowerShell.org</a> for pointing out the math error in my script.  I cast total hours to integer, which rounds (sometimes up).  What I should have done was either round down deliberately before casting, or otherwise truncate the number.  This has been corrected in the script below by using the math::truncate.  I also could have cast it as a string to correct it by simple discarding the decimal.</p>
<p>Here&#8217;s the script:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">&lt;#  
&nbsp;
.SYNOPSIS  
Get-Uptime collects and displays the uptime (hours, minutes, seconds) of a given collection of computers.  
&nbsp;
.DESCRIPTION  
This script utilizes the Get-WMIObject cmdlet to collect the Win32_OperatingSystem LastBootUpTime and 
CSName properties and returns a modified timespan object selected with Name, Hours, Minutes, and Seconds
of uptime properties.
&nbsp;
.PARAMETER ComputerName 
This mandatory parameter indicates the computer(s) that should be queried for uptime. It is accepted as position 0
both IP Addresses or ComputerNames are valid.  The script will always return a name for an online host.
&nbsp;
.PARAMETER FailedFile
This optional parameter works in conjuction with the -LogFail switch.  If no value is specified, the log file 
is created locally as .\NoUptime.txt
&nbsp;
.PARAMETER LogFail
This switch enables logging of machines that cannot be contacted to a file.  The file is configurable by the 
FailedFile parameter and defaults to .\NoUptime.txt
&nbsp;
.NOTES  
This script accepts input objects on the pipeline, or a string (or array of strings) from the pipeline in 
Position 0 or from the shell.  
&nbsp;
This script supports the verbose switch, which will indicate connection progress
&nbsp;
This script supports a -FailLog switch which will divert a list of failed computers to a specified text file.
&nbsp;
.EXAMPLE  
Get-UpTime localhost
&nbsp;
Collects the uptime of a single specified computer
&nbsp;
.EXAMPLE  
Get-Uptime localhost,computer1,computer2 -LogFail -FailedFile .\UptimeFailed.txt
&nbsp;
Collects the uptime of a specified list of computers, logs computers offline to the file .\UptimeFailed.txt
&nbsp;
.EXAMPLE  
Get-Uptime (Get-Content file.txt)
&nbsp;
Collects the uptime of a list of computers as specified in the file file.txt
&nbsp;
.EXAMPLE  
Get-ADComputer -Filter * | Get-Uptime -verbose
&nbsp;
Collects the uptime of every computer in the domain (could take a while...) and reports connection status
via Verbose as it goes.
&nbsp;
#&gt;</span>  
<span style="color: #000000;">&#91;</span>CmdletBinding<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0000FF;">Param</span> <span style="color: #000000;">&#40;</span>
	<span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: pink;">,</span> Position<span style="color: pink;">=</span><span style="color: #804000;">0</span><span style="color: pink;">,</span> ValueFromPipeline<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: pink;">,</span> ValueFromPipelineByPropertyName<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
	<span style="color: #000000;">&#91;</span>Alias<span style="color: #000000;">&#40;</span><span style="color: #800000;">'Computer'</span><span style="color: pink;">,</span> <span style="color: #800000;">'CSName'</span><span style="color: pink;">,</span> <span style="color: #800000;">'Server'</span><span style="color: pink;">,</span> <span style="color: #800000;">'__ServerName'</span><span style="color: pink;">,</span> <span style="color: #800000;">'__Server'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>ValidateNotNullOrEmpty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$ComputerName</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #0000FF;">switch</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$LogFail</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$FailedFile</span><span style="color: pink;">=</span><span style="color: #800000;">&quot;.\NoUptime.txt&quot;</span><span style="color: #000000;">&#41;</span>
Process <span style="color: #000000;">&#123;</span>
	<span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$computer</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$ComputerName</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-weight: bold;">Write-Verbose</span> <span style="color: #800000;">&quot;Trying to connect to $computer&quot;</span>
		<span style="color: #800080;">$item</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> Win32_OperatingSystem <span style="color: pink;">-</span>computer <span style="color: #800080;">$computer</span> <span style="color: #008080; font-style: italic;">-ErrorAction</span> SilentlyContinue 
			<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000080;">$?</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #008080; font-weight: bold;">Write-Verbose</span> <span style="color: #800000;">&quot;Succesfully Connected to $computer&quot;</span>
				<span style="color: #008080; font-weight: bold;">New-TimeSpan</span> <span style="color: #008080; font-style: italic;">-start</span> <span style="color: #800080;">$item</span>.ConvertToDateTime<span style="color: #000000;">&#40;</span><span style="color: #800080;">$item</span>.LastBootUpTime<span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-End</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Date</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> 
				<span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: #008080; font-style: italic;">-MemberType</span> NoteProperty <span style="color: #008080; font-style: italic;">-Name</span> Computername <span style="color: #008080; font-style: italic;">-Value</span> <span style="color: #800080;">$item</span>.CSName <span style="color: #008080; font-style: italic;">-passthru</span> <span style="color: pink;">|</span>
				<span style="color: #008080; font-weight: bold;">Select</span> ComputerName<span style="color: pink;">,</span> <span style="color: pink;">@</span><span style="color: #000000;">&#123;</span>Name<span style="color: pink;">=</span><span style="color: #800000;">'Hours'</span>;Expression<span style="color: pink;">=</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#91;</span>system.math<span style="color: #000000;">&#93;</span>::truncate<span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.TotalHours<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span><span style="color: pink;">,</span> Minutes<span style="color: pink;">,</span> Seconds
				<span style="color: #000000;">&#125;</span> <span style="color: #008000;">#End if</span>
			<span style="color: #0000FF;">else</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-weight: bold;">Write-Error</span> <span style="color: #800000;">&quot;Couldn't Connect to $computer, offline or unavailable?&quot;</span> <span style="color: #008080; font-style: italic;">-Category</span> OperationTimeout <span style="color: #008080; font-style: italic;">-RecommendedAction</span> <span style="color: #800000;">&quot;Check if Online?&quot;</span> 
			<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$LogFail</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> <span style="color: #800080;">$computer</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out<span style="color: #FF0000;">-File</span></span> <span style="color: #800080;">$FailedFile</span> <span style="color: #008080; font-style: italic;">-Append</span> <span style="color: #000000;">&#125;</span> 
			<span style="color: #000000;">&#125;</span> <span style="color: #008000;">#End Else</span>
	<span style="color: #000000;">&#125;</span> <span style="color: #008000;">#End ForEach</span>
<span style="color: #000000;">&#125;</span> <span style="color: #008000;">#End Process</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ryanandjeffshow.com/blog/2013/04/22/psg2013-advanced-event-0-system-uptime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PSG2013 Beginner Event 0: System Uptime.</title>
		<link>http://www.ryanandjeffshow.com/blog/2013/04/21/psg2013-beginner-event-0-system-uptime/</link>
		<comments>http://www.ryanandjeffshow.com/blog/2013/04/21/psg2013-beginner-event-0-system-uptime/#comments</comments>
		<pubDate>Sun, 21 Apr 2013 10:09:07 +0000</pubDate>
		<dc:creator><![CDATA[Jeff Liford]]></dc:creator>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Server]]></category>

		<guid isPermaLink="false">http://www.ryanandjeffshow.com/blog/?p=207</guid>
		<description><![CDATA[This is Beginner Event 0 (the warm up) for the 2013 PowerShell Scripting Games I will attempt to answer both the beginner and advanced versions of this exercise. This event centers on calculating system uptime, which is a realistic administrative<span class="ellipsis">&#8230;</span><div class="read-more"><a href="http://www.ryanandjeffshow.com/blog/2013/04/21/psg2013-beginner-event-0-system-uptime/">Read more &#8250;</a></div><!-- end of .read-more -->]]></description>
				<content:encoded><![CDATA[<p><a href="http://blogs.technet.com/b/heyscriptingguy/archive/2013/04/18/beginner-practice-for-2013-scripting-games.aspx">This is Beginner Event 0 </a>(the warm up) for the <a href="http://scriptinggames.org/home.php">2013 PowerShell Scripting Games</a></p>
<p>I will attempt to answer both the beginner and advanced versions of this exercise.</p>
<p>This event centers on calculating system uptime, which is a realistic administrative task that I&#8217;ve written into a couple of different scripts I&#8217;ve developed at work.  I&#8217;m not particularly fond of any of my implementations, so I&#8217;m going to start this from scratch.</p>
<p><em>Goal:</em> A one-liner that returns the system uptime for each name or IP address specified in a text file.  The return should be a pipeable/sortable object that contains the computer name (regardless of whether it was initially specified) as well as independent properties for uptime in days, hours, minutes, seconds.</p>
<p>In the Beginner track, the input is specified as a list of names or ip addresses in a text file.  Emphasis placed on the most concise answer possible (speculated one-liner), so I&#8217;m going to have to find built-in/pipeable cmdlets to do my work.</p>
<p>Success!  Here&#8217;s the Single line command to accomplish this task.  (Our code engine doesn&#8217;t wrap long single lines, so I&#8217;ve put it in twice so you can see the whole thing without scrolling or copy the un-ticked version)</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">#Broken Lines</span>
<span style="color: #008080; font-weight: bold;">Get-WMIObject</span> <span style="color: #008080; font-style: italic;">-class</span> Win32_OperatingSystem <span style="color: #008080; font-style: italic;">-computername</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Content</span> computers.txt<span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> 
<span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><span style="color: #008080; font-weight: bold;">New-TimeSpan</span> <span style="color: #008080; font-style: italic;">-start</span> <span style="color: #000080;">$_</span>.ConvertToDateTime<span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.lastbootuptime<span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-end</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Date</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> 
<span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: #008080; font-style: italic;">-MemberType</span> NoteProperty <span style="color: #008080; font-style: italic;">-Name</span> Computername <span style="color: #008080; font-style: italic;">-Value</span> <span style="color: #000080;">$_</span>.CSName <span style="color: #008080; font-style: italic;">-passthru</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> 
<span style="color: #008080; font-weight: bold;">Select</span> ComputerName<span style="color: pink;">,</span> <span style="color: pink;">@</span><span style="color: #000000;">&#123;</span>Name<span style="color: pink;">=</span><span style="color: #800000;">'Hours'</span>;Expression<span style="color: pink;">=</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#91;</span>system.math<span style="color: #000000;">&#93;</span>::truncate<span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.TotalHours<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span><span style="color: pink;">,</span> Minutes<span style="color: pink;">,</span> Seconds
&nbsp;
<span style="color: #008000;">#Single Line</span>
<span style="color: #008080; font-weight: bold;">Get-WMIObject</span> <span style="color: #008080; font-style: italic;">-class</span> Win32_OperatingSystem <span style="color: #008080; font-style: italic;">-computername</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Content</span> computers.txt<span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><span style="color: #008080; font-weight: bold;">New-TimeSpan</span> <span style="color: #008080; font-style: italic;">-start</span> <span style="color: #000080;">$_</span>.ConvertToDateTime<span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.lastbootuptime<span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-end</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Date</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: #008080; font-style: italic;">-MemberType</span> NoteProperty <span style="color: #008080; font-style: italic;">-Name</span> Computername <span style="color: #008080; font-style: italic;">-Value</span> <span style="color: #000080;">$_</span>.CSName <span style="color: #008080; font-style: italic;">-passthru</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Select</span> ComputerName<span style="color: pink;">,</span> <span style="color: pink;">@</span><span style="color: #000000;">&#123;</span>Name<span style="color: pink;">=</span><span style="color: #800000;">'Hours'</span>;Expression<span style="color: pink;">=</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#91;</span>system.math<span style="color: #000000;">&#93;</span>::truncate<span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.TotalHours<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span><span style="color: pink;">,</span> Minutes<span style="color: pink;">,</span> Seconds</pre></td></tr></table></div>

<p><span id="more-207"></span></p>
<p>Special thanks to mjolinor at <a href="http://PowerShell.org">PowerShell.org</a> for pointing out my Math error.  I went ahead and fixed the issue I identified with pulling the wrong values.  It said total hours, minutes, seconds.  Not Days&#8230;</p>
<p>Here&#8217;s the dirty details I went through to get there&#8230;</p>
<p><strong>Beginner Track Event 0: System Uptime</strong></p>
<p>So I need to calculate uptime from a specified list of hosts in a text file, so I already know I will be feeding in Get-Content textfile somewhere in here.  I also need to know where to get the information.  The WMI class Win32_OperatingSystem has a last boot time property that&#8217;s stored in WMI&#8217;s funky format, so I can probably do some math.  It also has CSName, which gives me the name even if I only had an IP to start with.  The trick will be getting name and separated properties into an object I can pass on the shell in a one-line command.</p>
<p>&#8212;PowerShell Pause</p>
<p>So I went into PowerShell and pulled the WMI class from my machine and started playing with the properties.  I knew there was a scriptmethod for converting that funky WMI date to something more readable, but I didn&#8217;t realize it put out a [System.DateTime] object (I guess I should have everything in PowerShell is an object!).  Turns out, this object already has the hours/minutes/seconds I want.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-WMIObject</span> <span style="color: pink;">-</span>computer <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Content</span> textfile.txt<span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-class</span> Win32_OperatingSystem<span style="color: #000000;">&#41;</span>.ConvertToDateTime<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$lastboouptime</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> 
<span style="color: #008000;">#gives me back a readable object oriented date.</span>
&nbsp;
<span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-WMIObject</span> <span style="color: pink;">-</span>computer <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Content</span> textfile.txt<span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-class</span> Win32_OperatingSystem<span style="color: #000000;">&#41;</span>.lastbootuptime 
<span style="color: #008000;">#gives me back a boot time in WMI Format</span></pre></td></tr></table></div>

<p>My problem at the moment is the ConvertToDateTime scriptproperty because it needs an argument, which is another member of the same object.  Unless I store the object, I do not know how to get it there besides calling it again&#8230;</p>
<p>(Get-WMIObject &#8230;).ConvertToDateTime((Get-WMIObject&#8230;).lastbootuptime)?</p>
<p>I&#8217;m also not sure how to pass in the computername without storing it.  I know I can add-member on the object that results from these commands, but I don&#8217;t know how to get the name fed in during my Get-Content other than the store it.  Maybe that&#8217;s the key&#8230;</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#40;</span><span style="color: #800080;">$computers</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> computers.txt <span style="color: pink;">|</span> `
<span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;">$tmp</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WMIObject</span> <span style="color: #008080; font-style: italic;">-class</span> Win32_OperatingSystem `
<span style="color: #008080; font-style: italic;">-computername</span> <span style="color: #000080;">$_</span>;$tmp.ConvertToDateTime<span style="color: #000000;">&#40;</span><span style="color: #800080;">$tmp</span>.lastbootuptime<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>This returned the readable date-time objects I was looking for!  And I retain the computername as a passable string in the pipe.  Now we&#8217;re getting somewhere.</p>
<p>I need to get uptime now, so I need to compare times.  Playing with Get-Command *Time* found me an cmdlet called New-TimeSpan, which compares dates and times.  So I can use that to get my uptime by simply comparing the date I got back to right now.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#40;</span><span style="color: #800080;">$computers</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> computers.txt <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;">$tmp</span> <span style="color: pink;">=</span> `
<span style="color: #008080; font-weight: bold;">Get-WMIObject</span> <span style="color: #008080; font-style: italic;">-class</span> Win32_OperatingSystem <span style="color: #008080; font-style: italic;">-computername</span> <span style="color: #000080;">$_</span>; `
<span style="color: #800080;">$tmp</span><span style="color: pink;">=</span><span style="color: #800080;">$tmp</span>.ConvertToDateTime<span style="color: #000000;">&#40;</span><span style="color: #800080;">$tmp</span>.lastbootuptime<span style="color: #000000;">&#41;</span>;<span style="color: #008080; font-weight: bold;">New-Timespan</span> `
<span style="color: #008080; font-style: italic;">-start</span> <span style="color: #800080;">$tmp</span> <span style="color: #008080; font-style: italic;">-end</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Date</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>This gets me back the uptime objects.  I think I can feed the computernames in here.  I need to pull it out of that WMI class&#8230;</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#40;</span><span style="color: #800080;">$computers</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> computers.txt <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;">$tmp</span> <span style="color: pink;">=</span> `
<span style="color: #008080; font-weight: bold;">Get-WMIObject</span> <span style="color: #008080; font-style: italic;">-class</span> Win32_OperatingSystem <span style="color: #008080; font-style: italic;">-computername</span> <span style="color: #000080;">$_</span>; `
<span style="color: #800080;">$computername</span> <span style="color: pink;">=</span> <span style="color: #800080;">$tmp</span>.CSName; `
<span style="color: #800080;">$tmp</span><span style="color: pink;">=</span><span style="color: #800080;">$tmp</span>.ConvertToDateTime<span style="color: #000000;">&#40;</span><span style="color: #800080;">$tmp</span>.lastbootuptime<span style="color: #000000;">&#41;</span>;<span style="color: #008080; font-weight: bold;">New-Timespan</span> `
<span style="color: #008080; font-style: italic;">-start</span> <span style="color: #800080;">$tmp</span> <span style="color: #008080; font-style: italic;">-end</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Date</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: #008080; font-style: italic;">-MemberType</span> NoteProperty `
<span style="color: #008080; font-style: italic;">-Name</span> ComputerName <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$computername</span> <span style="color: #008080; font-style: italic;">-passthru</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Initially I thought this didn&#8217;t work, because I only got back the properties of the time span object.  I piped the whole thing into Get-Member and found my new property, then I remembered the object has default properties.  Piping to Select gives me what I want and cleaned up the output&#8230;</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#40;</span><span style="color: #800080;">$computers</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> computers.txt <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;">$tmp</span> <span style="color: pink;">=</span> `
<span style="color: #008080; font-weight: bold;">Get-WMIObject</span> <span style="color: #008080; font-style: italic;">-class</span> Win32_OperatingSystem <span style="color: #008080; font-style: italic;">-computername</span> <span style="color: #000080;">$_</span>; `
<span style="color: #800080;">$computername</span> <span style="color: pink;">=</span> <span style="color: #800080;">$tmp</span>.CSName; `
<span style="color: #800080;">$tmp</span><span style="color: pink;">=</span><span style="color: #800080;">$tmp</span>.ConvertToDateTime<span style="color: #000000;">&#40;</span><span style="color: #800080;">$tmp</span>.lastbootuptime<span style="color: #000000;">&#41;</span>;<span style="color: #008080; font-weight: bold;">New-Timespan</span> `
<span style="color: #008080; font-style: italic;">-start</span> <span style="color: #800080;">$tmp</span> <span style="color: #008080; font-style: italic;">-end</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Date</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: #008080; font-style: italic;">-MemberType</span> NoteProperty `
<span style="color: #008080; font-style: italic;">-Name</span> ComputerName <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$computername</span> <span style="color: #008080; font-style: italic;">-passthru</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Select</span> `
ComputerName<span style="color: pink;">,</span> Days<span style="color: pink;">,</span> Hours<span style="color: pink;">,</span> Minutes<span style="color: pink;">,</span> Seconds</pre></td></tr></table></div>

<p>So there&#8217;s a one liner that gives me Days, Hours, Minutes, Seconds of uptime for each computer in a list.  I&#8217;m not sure if its considered cheating to call this a one-liner, since each semi-colon technically delimits a new scriptblock line, but there you have it.</p>
<p>&#8212;break for about 30 seconds</p>
<p>It always hits me late!  Here&#8217;s a version that is really a one-liner.  Pipe within a pipe.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-WMIObject</span> <span style="color: #008080; font-style: italic;">-class</span> Win32_OperatingSystem <span style="color: #008080; font-style: italic;">-computername</span> `
<span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Content</span> computers.txt<span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><span style="color: #008080; font-weight: bold;">New-TimeSpan</span> <span style="color: #008080; font-style: italic;">-start</span> `
<span style="color: #000080;">$_</span>.ConvertToDateTime<span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.lastbootuptime<span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-end</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Date</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: #008080; font-style: italic;">-MemberType</span> `
NoteProperty <span style="color: #008080; font-style: italic;">-Name</span> Computername <span style="color: #008080; font-style: italic;">-Value</span> <span style="color: #000080;">$_</span>.CSName <span style="color: #008080; font-style: italic;">-passthru</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> `
<span style="color: #008080; font-weight: bold;">Select</span> ComputerName<span style="color: pink;">,</span> Days<span style="color: pink;">,</span> Hours<span style="color: pink;">,</span> Minutes<span style="color: pink;">,</span> Seconds</pre></td></tr></table></div>

<p>I really enjoy playing with these as mind-teasers.  Some people play Sudoku, I write PowerShell code.  This one was fun because it forced me to change the way I think about the pipeline.</p>
<p>Tomorrow I&#8217;ll take a swing at the advanced track exercise.  Until then, stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryanandjeffshow.com/blog/2013/04/21/psg2013-beginner-event-0-system-uptime/feed/</wfw:commentRss>
		<slash:comments>