IF statement to PowerShell Script

I have a Powershell script that gets uptime from servers that I pieced together from bits and pieces from written code on the net. What I have works great, however I've been asked to modify it for some output based on how long the servers have been up.

Here is what I've been asked to add to the script:

An "if" statement to check if the $UptimeSeconds variable is less than 600 to Write-output the value "rebooted" and and if $UptimeSeconds is greater than 2592000 (30 days) to Write-output "rebootneeded"

I appreciate any help I can get. I'm just leaning Powershell and am digging in where I can.

David
Uptime.txt
David SechlerOwnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
Try..
####################################################################### 
# This PS script provides Uptime and Pingstatus for list of computers.
#######################################################################
$names = Get-Content "C:\Users\david.sechler\Documents\PowerShell\Get Uptime\servers.txt"
@(
	foreach ($name in $names)
	{
    if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue ) 
	{
    $wmi = gwmi -class Win32_OperatingSystem -computer $name
	$LBTime = $wmi.ConvertToDateTime($wmi.Lastbootuptime)
	[TimeSpan]$uptime = New-TimeSpan $LBTime $(get-date)
	If ($uptime.TotalDays -gt 30){
	Write-output "$name Uptime is  $($uptime.days) Days $($uptime.hours) Hours $($uptime.minutes) Minutes $($uptime.seconds) Seconds - rebootneeded"
	}ElseIf ($uptime.TotalSeconds -le 600) {
	Write-output "$name Uptime is  $($uptime.days) Days $($uptime.hours) Hours $($uptime.minutes) Minutes $($uptime.seconds) Seconds - rebooted"
	}Else{
	Write-output "$name Uptime is  $($uptime.days) Days $($uptime.hours) Hours $($uptime.minutes) Minutes $($uptime.seconds) Seconds"
	}
	}
    else {
    Write-output "$name is not pinging"
	}
	}
)| Out-file -FilePath "C:\Users\david.sechler\Documents\PowerShell\Get Uptime\results.txt"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David SechlerOwnerAuthor Commented:
I ran the script and got the same output as I did in the original script. I've added the results.txt as an attachmentresults.txt.
0
SubsunCommented:
It's because none of the servers have uptime below 600 seconds and above 30 days..

If you want to test change the threshold to one day.. Change line 13 to..
	If ($uptime.TotalDays -gt 1){

Open in new window

0
David SechlerOwnerAuthor Commented:
I just bounced one of my workstations in the list and your right! Good to go! Thanks so much for the help Subsun!
0
David SechlerOwnerAuthor Commented:
Thanks again for the help. This was my first question here and I'm quite pleased!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.