Our January 2016 puzzle comes from MVP Adam Bertram. We're actively interested in receiving Scripting Games puzzles from members of the community - submit yours, along with an official solution, to us at admin@ via email!
Instructions
The Scripting Games are a monthly puzzle. We publish puzzles the first Saturday of each month, along with solutions and commentary for the previous month's puzzle. You can find them all at https://powershell.org/category/announcements/scripting-games/. Many puzzles will include optional challenges, that you can use to really push your skills.
To participate, add your solution to a public Gist (http://gist.github.com; you'll need a free GitHub account, which all PowerShellers should have anyway). After creating your public Gist, just copy the Gist URL from your browser window and paste it, by itself, as a comment of this post. Only post one entry per person. However, remember that you can always go back and edit your Gist. We'll always pull the most recent one when we display it, so there's no need to post multiple entries if you want to make an edit. Just edit the original Gist and we'll see your changes shortly.
Don't forget the main rules and purpose of these monthly puzzles, including the fact that you won't receive individual scoring or commentary on your entry.
User groups are encouraged to work together on the monthly puzzles. User group leaders should submit their group's best entry to Ed Wilson, the Scripting Guy, via e-mail, prior to the third Saturday of the month. On the last Saturday of the month, Ed will post his favorite, along with commentary and excerpts from noteworthy entries. The user group with the most "favorite" entries of the year will win a grand prize from PowerShell.org.
Our Puzzle
Server uptime is the lifeblood of system administrators. We strive on it, get addicted to it..we need…more server uptime! Don't you think something as addictive and important as server uptime be measured? How do we know we're getting our uptime fix? As that famous quote goes, "Reality does not exist until it's measured.". Let's measure it not only for our own sake but also to give a pretty report to our manager with all those whizbang, doohickey Excel juju that they love to see!
For this month's challenge, I want you to create a PowerShell function that you can remotely point to a Windows server to see how long it has been up for. Here's an example of what it should output.
Requirements:
1. Support pipeline input so that you can pipe computer names directly to it.
2. Process multiple computer names at once time and output each computer's stats with each one being a single object.
3. It should not try to query computers that are offline. If an offline computer is found, it should write a warning to the console yet still output an object but with Status of OFFLINE.
4. If the function is not able to find the uptime it should show ERROR in the Status field.
5. If the function is able to get the uptime, it should show 'OK' in the Status field.
6. It should include the time the server started up and the uptime in days (rounded to 1/10 of a day)
7. If no ComputerName is passed, it should default to the local computer.
Bonus:
1. The function should show a MightNeedPatched property of $true ONLY if it has been up for more than 30 days (rounded to 1/10 of a month). If it has been up for less than 30 days, MightNeedPatched should be $false.
https://gist.github.com/bundyfx/f84c6716e743c0a488c0
https://gist.github.com/brianbunke/2c4fb81ed24cfb9610c2
Lets try that again…
https://gist.github.com/mramplin/ca897d6e3e6793f427d2
[…] Powershell.org Scripting Games – January 2016 […]
https://gist.github.com/mmarchese/ca100c94b5159f1d9b75
Number two was the trickiest since you aren’t really processing multiple computer names at one time if you use a for-each loop inside your process block. Instead we offload our scriptblock to individual runspaces so everything can be processed at once.
https://gist.github.com/anonymous/e5169516a70ea877d6df#file-2016jan_scriptinggames-ps1
https://gist.github.com/KirillPashkov/a979a2f24261be898ba7
https://gist.github.com/XPlantefeve/36e544cd40f39b4d4d55
7 Requirements and Brucey Bonus.
https://gist.github.com/githubbery/ba48237f59d37f9e90d2
https://gist.github.com/jeffbuenting/5f39afcc19f62ed5fb91
Cheers
FYI – No idea how GitHub works. I’ll have to work on that!
https://gist.github.com/liampkemp/9295f760330f3168c159.js
function Get-Uptime {
[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact=’Low’)]
PARAM (
[Parameter(
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)]
[Alias(‘hostname’)]
[string[]]$computerName= $env:COMPUTERNAME
)
BEGIN {}
PROCESS {
Write-Verbose ‘Checking uptime for $computerName’
foreach ($computer in $ComputerName) {
if($PSCmdlet.ShouldProcess($com)) {
if(Test-Connection -count 1 -ComputerName $computer) {
$os = Get-CimInstance -Class Win32_OperatingSystem -ComputerName $computer |
Select-Object LastBootupTime,LocalDateTime
$uptime = ((New-TimeSpan -Start $os.LastBootupTime -End $os.LocalDateTime).TotalDays -as [int])
$properties = @{‘ComputerName’=$computer;
‘StartTime’=$os.lastbootuptime;
‘Uptime’=$uptime;
‘Status’=if($uptime -ge 0){
Write-Output ‘OK’
} else{
Write-Output ‘ERROR’
}
‘MightNeedPatched’=($properties.Uptime -gt 30)
}
} else {
Write-Warning -Message ‘Could not connect to $computer’
$properties = @{‘ComputerName’=$computer;
‘StartTime’=”;
‘Uptime’=”;
‘Status’=’OFFLINE’}
}
$obj = New-Object -TypeName PSObject -Property $properties
Write-Output $obj | Select-Object ComputerName,StartTime,@{LABEL=’Uptime (Days)’;EXPRESSION={$_.Uptime}},Status,MightNeedPatched | Format-Table
}
}
}
END {}
}
https://gist.github.com/kvprasoon/8ba60788ccc84bc535c8
My solution for 2016-January Scripting Games Puzzle
https://donrsh.wordpress.com/2016/01/11/january-2016-scripting-games-puzzle-get-uptime/
https://github.com/donrsh001/GitHub/blob/master/Get-Uptime.ps1
[…] Here’s the link to the puzzle for this month. I would recommend that you look it over, and begin thinking of how you might approach it. However, let’s let everyone have a chance to answer the puzzle and work through it as a team […]
My solution for the January puzzle! Thanks.
function Get-Uptime{
[CmdletBinding()]
param
(
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[String[]]$ComputerName = $env:COMPUTERNAME
)
PROCESS{
foreach($computer in $ComputerName){
$isAlive = Test-Connection -ComputerName $computer -Count 1 -Quiet
if($isAlive -eq $true){
$compSystem = Get-WmiObject -ComputerName $computer -ClassName Win32_ComputerSystem
$osSystem = Get-WmiObject -ComputerName $computer -ClassName Win32_OperatingSystem
$bool = ((Get-Date)-$osSystem.ConvertToDateTime($osSystem.LastBootUpTime)).Days -gt 30
$hash = [ordered]@{
ComputerName = $compSystem.Name
StartTime = $osSystem.ConvertToDateTime($osSystem.LastBootUpTime)
“Uptime (Days)” = ((Get-Date)-$osSystem.ConvertToDateTime($osSystem.LastBootUpTime)).Days
Status = “OK”
MightNeedPatched = $bool
}
$obj = New-Object -TypeName psobject -Property $hash
$obj
}
else{
$hash = [ordered]@{
ComputerName = $computer
StartTime = “”
“Uptime (Days)” = “”
Status = “ERROR”
MightNeedPatched = “”
}
Write-Warning -Message “$computer is Offline”
$obj = New-Object -TypeName psobject -Property $hash
$obj
}
}
}
}
My solution:
Cross posted on my site:
http://www.poshcodebear.com/blog/2016/1/13/scripting-games-january-2016
Or maybe I need to do it this way…
https://gist.github.com/poshcodebear/19244b0751c4a24bbe78
thanks, I’ve taken your [Math]::Round
Gah — well that failed miserably!
Maybe this is how it works…
Maybe this?
https://gist.github.com/TheRickOlson/68dd74b5e5699fc44da0
Sorry for all the spam everyone! First time participating, first time using GitHub, first time using Gists….yikes.
My solution –
https://gist.github.com/arabha123/f36f7f496fd96cbb3bc2#file-answer-scriptinggames2016-ps1
https://gist.github.com/DezCorps/33b97c66972b9b2fcacc
https://gist.github.com/BennettBenson/5ea19b7d72d12bef5786.js
https://gist.github.com/michaellee7/4b86b391df8ed44a5f8e.js
https://gist.github.com/mwu17/d1a6765ddfc1d53de694
https://gist.github.com/P0oki/5bea17e213312d8cf97c#file-get-uptime-ps1
https://gist.github.com/sellynx/9bb93961167e6956579b
https://gist.github.com/dean1609/1c9223dc8930742eb51d
function get-uptime
{
[CmdletBinding()]
Param(
[parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
[string[]]$computername = “localhost”)
foreach ($comp in $computername)
{
$staus = “ok”
$rtr = test-connection -ComputerName $comp -Count 2 -ErrorAction SilentlyContinue
if($rtr -match 0)
{
$time = Get-WmiObject win32_operatingsystem -ComputerName $comp
$bootime = $time.converttodatetime($time.lastbootuptime)
$today = get-date
$diff = New-TimeSpan -Start $bootime -End $today
$time | select -Property @{name=’computername’;expression={$_.pscomputername}},@{name=”startTime”;expression={$_.converttodatetime($_.lastbootuptime)}},@{name= “uptime(days)”;expression = {$diff.days}},@{name=”status”;expression={“ok”}}
}
else
{Write-Warning “computer Status could be OFFLINE”}
}
}