PSG2013: Beginner Event 1: Managing Old Log Files

This is my submission for Event 1 in the beginner track for the PowerShell Scripting Games, 2013

Here’s the instructions. 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.

Here’s my submission:

#Broken Up
Get-ChildItem C:\Application\Log\*\* | 
ForEach { 
if (((Get-Date)-$_.lastaccesstime).TotalDays -gt 90) { 
   Move-Item $_ \\NASServer\Archive\$(($_.directory).name)\$($_.name) 
   } 
}
 
#Retained on one line
Get-ChildItem C:\Application\Log\*\* | ForEach { if (((Get-Date)-$_.lastaccesstime).TotalDays -gt 90) { Move-Item $_ \\NASServer\Archive\$(($_.directory).name)\$($_.name) } }

Read more ›

Posted in Uncategorized

PSG2013 Advanced Event 0: System Uptime

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 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.

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’t grab it. It’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).

Read more ›

Posted in Desktop, PowerShell, Server

PSG2013 Beginner Event 0: System Uptime.