<?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</title>
	<atom:link href="http://www.ryanandjeffshow.com/blog/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: Beginner Event 1: Managing Old Log Files</title>
		<link>http://www.ryanandjeffshow.com/blog/2013/05/02/psg2013-beginner-event-1-managing-log-files/</link>
		<comments>http://www.ryanandjeffshow.com/blog/2013/05/02/psg2013-beginner-event-1-managing-log-files/#comments</comments>
		<pubDate>Thu, 02 May 2013 21:00:13 +0000</pubDate>
		<dc:creator><![CDATA[Jeff Liford]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.ryanandjeffshow.com/blog/?p=239</guid>
		<description><![CDATA[This is my submission for Event 1 in the beginner track for the PowerShell Scripting Games, 2013 Here&#8217;s the instructions. The premise of the event is log files have been allowed to accumulate and are now eating disk space on<span class="ellipsis">&#8230;</span><div class="read-more"><a href="http://www.ryanandjeffshow.com/blog/2013/05/02/psg2013-beginner-event-1-managing-log-files/">Read more &#8250;</a></div><!-- end of .read-more -->]]></description>
				<content:encoded><![CDATA[<p>This is my submission for Event 1 in the beginner track for the <a href="http://scriptinggames.org">PowerShell Scripting Games, 2013</a></p>
<p><a href="http://scriptinggames.org/265642e83094138277ac.pdf">Here&#8217;s the instructions.</a>  The premise of the event is log files have been allowed to accumulate and are now eating disk space on a server.  Files accumulate in C:\Application\Log\[Application Name].  We need to identify and move files that are older than 90 days to an archive located at \\NASServer\Archive.  When moving, the files should retrain the path with the application name in it.  This means C:\Application\Log\App1\OldLogFile.log should land as \\NASServer\Archive\App1\OldLogFile.log.  As concise as possible, One-Liners are appreciated.</p>
<p>Here&#8217;s my submission:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">#Broken Up</span>
<span style="color: #008080; font-weight: bold;">Get-ChildItem</span> C:\Application\Log\<span style="color: pink;">*</span>\<span style="color: pink;">*</span> <span style="color: pink;">|</span> 
<span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span> 
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</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: #000080;">$_</span>.lastaccesstime<span style="color: #000000;">&#41;</span>.TotalDays <span style="color: #FF0000;">-gt</span> <span style="color: #804000;">90</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> 
   <span style="color: #008080; font-weight: bold;">Move-Item</span> <span style="color: #000080;">$_</span> \\NASServer\Archive\$<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.directory<span style="color: #000000;">&#41;</span>.name<span style="color: #000000;">&#41;</span>\$<span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.name<span style="color: #000000;">&#41;</span> 
   <span style="color: #000000;">&#125;</span> 
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;">#Retained on one line</span>
<span style="color: #008080; font-weight: bold;">Get-ChildItem</span> C:\Application\Log\<span style="color: pink;">*</span>\<span style="color: pink;">*</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span> <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</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: #000080;">$_</span>.lastaccesstime<span style="color: #000000;">&#41;</span>.TotalDays <span style="color: #FF0000;">-gt</span> <span style="color: #804000;">90</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Move-Item</span> <span style="color: #000080;">$_</span> \\NASServer\Archive\$<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.directory<span style="color: #000000;">&#41;</span>.name<span style="color: #000000;">&#41;</span>\$<span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.name<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-239"></span></p>
<p><strong>Managing Old Log Files</strong></p>
<p>So first things first, we need to actually go out and get all the log files.  This turned out to be amazingly simple.</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-ChildItem</span> C:\Application\Log\<span style="color: pink;">*</span>\<span style="color: pink;">*</span></pre></td></tr></table></div>

<p>I didn&#8217;t realize Get-ChildItem (which is what the dir alias is in PowerShell) would let me double wildcard like this.  I found it just playing around with the cmdlet.  But once I have the return, that is and object for every file from every folder beneath Application\Log, I can start examining them directly to make a decision and do something.</p>
<p>From there I need to identify which files are older than 90 days and take an action.  The LastAccessTime Property of a filesystem object contains a standard date object, which I can simply subtract from the date right now (Get-Date).  This results in a timespan object that has a non-incremental TotalDays property.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span> <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</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: #000080;">$_</span>.lastaccesstime<span style="color: #000000;">&#41;</span>.TotalDays <span style="color: #FF0000;">-gt</span> <span style="color: #804000;">90</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008000;">#Do Something } }</span></pre></td></tr></table></div>

<p>From here, its just a matter of moving files.  To pick the object to move, I simply specify the object on the pipeline $_.  To retain the folder structure, I had to get a little more creative.  Each filesystem object has a directory property, which is a call for the directory that houses the file.  By calling this and looking at its name, I can specify it in the destination path for Move-Item.  Simply specify the name of the file after we&#8217;ve modified the path.  I need the extra dollar sign to signify to PowerShell that this string should be the result of enumerating that property.  $($_.name) is a variable stand-in for the name of the file i.e. Someoldlogfile.log</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Move-Item</span> <span style="color: #000080;">$_</span> \\NASServer\Archive\$<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.directory<span style="color: #000000;">&#41;</span>.name<span style="color: #000000;">&#41;</span>\$<span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.name<span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Simply string it all together.  No Output if it succeeds, errors are not suppressed.  Move-Item will complain if the destination path does not exist, but on an archive server it stands to reason the path probably already exists, or that I&#8217;d at least be willing to create it the first time I tried to do this task.</p>
<p>The advanced version calls for parameterizing the paths to examine as well as the time-frame to judge on.  That doesn&#8217;t sound terribly difficult, so I will look at it next.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryanandjeffshow.com/blog/2013/05/02/psg2013-beginner-event-1-managing-log-files/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>