keepit
December 26, 2019, 8:52pm
1
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:
function 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"
$display2
Write-output $BootMinutes
Write-Output $time.TotalMinutes
Write-Output $reftime
}
get-uptime
If ($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
Olaf
December 27, 2019, 5:57am
2
May I suggest a different approach?
function Get-Uptime {
$BootTime = (Get-CimInstance -ClassName CIM_OperatingSystem).LastBootUpTime
$Now = Get-Date
$Uptime = New-TimeSpan -Start $BootTime -End $Now
[PSCustomObject]@{
BootTime = $BootTime
UpTime = $Uptime
Message = "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"
}
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:
#Wait 5 min...
Start-Sleep -Seconds (5 * 60)
'Starting tricks...'
#Wait 2 min...
Start-Sleep -Seconds (2 * 60)
'Starting more tickets...'
keepit
December 27, 2019, 4:28pm
4
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.
keepit
December 27, 2019, 4:36pm
5
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).
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
It is formating the strings using placeholders, and please refer to this link below…
https://ss64.com/ps/syntax-f-operator.html