Welcome › Forums › General PowerShell Q&A › problem of comparating 2 boot times.
- This topic has 7 replies, 5 voices, and was last updated 1 year ago by
Participant.
-
AuthorPosts
-
-
December 27, 2019 at 2:22 am #195287
Hello,
I’ve a problem with my code, i’m not very specialist of PS, and i think that my problem is from a variable problem.
I try to compile a PS script that do some tricks if a PC was booted since less than X minutes, and other things if > 7 minutes
That’s my code:
PowerShell1234567891011121314151617181920212223function get-uptime {[int]$reftime=7$os = Get-WmiObject win32_operatingsystem$time = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime))$display = "time since last boot: " + $time.Days + " days, " + $time.Hours + " hours, " + $time.Minutes + " minutes and " + $time.Seconds + " seconds"Write-Output $display[int]$BootMinutes= $time.TotalMinutes$display2 = "Computer boot since " + $BootMinutes + " Minutes"$display2Write-output $BootMinutesWrite-Output $time.TotalMinutesWrite-Output $reftime}get-uptimeIf ($BootMinutes -lt $reftime) {Write-Host "less than 7 min since last boot"}If ($BootMinutes -ge $reftime) {Write-host "more than 7 min since last boot"}I try with 13 minutes and it’s ok:
“Computer boot since 13 Minutes
13
13,4964267466667
7
more than 7 min since last boot”I try now with 5 minutes, and i don’t knows why, but it doesn’t work:
“Computer boot since 5 Minutes
5
4,81610453666667
7
more than 7 min since last boot”
Anybody have an idea what i’m wrong?
Thank you for your help
-
December 27, 2019 at 11:27 am #195329
May I suggest a different approach?
PowerShell12345678910111213141516171819function Get-Uptime {$BootTime = (Get-CimInstance -ClassName CIM_OperatingSystem).LastBootUpTime$Now = Get-Date$Uptime = New-TimeSpan -Start $BootTime -End $Now[PSCustomObject]@{BootTime = $BootTimeUpTime = $UptimeMessage = "This computer is up for {0} days, {1} hours, {2} minutes and {3} seconds." -f $Uptime.Days, $Uptime.Hours, $Uptime.Minutes, $Uptime.Seconds}}$SevenMinutesAgo = (Get-Date).AddMinutes(-7)If ((Get-Uptime).BootTime -gt $SevenMinutesAgo) {Write-Host "less than 7 min since last boot"}If ((Get-Uptime).BootTime -lt $SevenMinutesAgo) {Write-host "more than 7 min since last boot"}-
December 27, 2019 at 9:58 pm #195392
Hi olaf,
Ok, it’s sure, it works with your script! and i will use it to continue my script.
But, if i want to progress, i want to undestand why my script is bad!
Thank you.
-
-
December 27, 2019 at 4:35 pm #195350
How are you planning on executing this script? Understand that you would basically have to run that script on boot and loop continually to see where the to hit the 7 min mark. If you are launching it at boot, why not just do basic sleeps:
PowerShell123456789#Wait 5 min...Start-Sleep -Seconds (5 * 60)'Starting tricks...'#Wait 2 min...Start-Sleep -Seconds (2 * 60)'Starting more tickets...'-
December 27, 2019 at 10:06 pm #195395
Hello rob,
No it’s the beginning of a script, and i think that it is not the good way, but i’m not good in PS, so i must start with something.
The idea is that : We will open all computer of our company on a date/hour (for example, sunday) with a bios auto boot config.
The idea is to lunch, somes minutes after the boot (5 minutes i think), a script that will analyse if the computer is open since 7 minutes.
The idea is:
- If computer is booted more than 7 minutes : there is a chance that this is a pc that is using by a users : so DO NOTHING
- If computer is booted less thant 7 minutes : the pc just booted up, so we do some tricks (update, antivirus, cleaning script, and finally, shutdown).
-
-
December 29, 2019 at 4:38 am #195512
I have a dump question.
Message = “This computer is up for {0} days, {1} hours, {2} minutes and {3} seconds.” -f $Uptime.Days, $Uptime.Hours, $Uptime.Minutes, $Uptime.Seconds
What is “-f” for ? Any reference that I can look at
-
December 29, 2019 at 6:08 am #195530
It is formating the strings using placeholders, and please refer to this link below…
-
December 30, 2019 at 3:21 pm #195692
Thanks a lot.
-
-
AuthorPosts
- The topic ‘problem of comparating 2 boot times.’ is closed to new replies.