1
I've written a PS script to find the last time a machine was booted (actually powered on rather than just started up as we have fast boot enabled in Win10). Script seems to do it's job, it brings back boot date/time, last user, and hostname. However, I'm running this as a script in SCCM and not sure of the best way to deal with the Output at scale.
At the moment the script outputs to a hashtable, just so I could assign a key to each value for whoever ends up reading the results. But I'm thinking if you're running this against 50 machines your going to want to be able to drop the results into a spreadsheet or something to sort/filter. But copying hastable results over doesn't format well for multiple devices.
Does anyone have any advice as how to best achieve this? It's worth noting that I'm far from a PowerShell expert and there's every chance a lot of the below is inefficient or redundant. Also ignore the split on "(" for full name. It's to do with the formatting of our user accounts.
Thanks in advance. Current form of script below.
$LastBoot = Get-CimInstance -Class win32_operatingsystem | select lastbootuptime
$LastBoot = $LastBoot | Select -ExpandProperty "lastbootuptime"
$LastBoot = $LastBoot.ToString()
$LastBootDate = $LastBoot.split(" ")[0]
$LastBootTime = $LastBoot.split(" ")[1]
$LocalMachine = Get-CimInstance -ClassName win32_operatingsystem | select csname
$LocalMachine = $LocalMachine | Select -ExpandProperty "csname"
$Username = Get-CimInstance -Class win32_networkloginprofile | Sort-Object -Property LastLogon -Descending | Select -ExpandProperty "name" -First 1
$Username = $Username.split("\")[1]
$Domain = $env:userdomain
$Friendly = Get-CimInstance -Class win32_networkloginprofile | Sort-Object -Property LastLogon -Descending | Select -ExpandProperty "FullName" -First 1
$Friendly = $Friendly.split("(")[0]
$Results = @{
Hostname = $LocalMachine;
BootDate = $LastBootDate;
BootTime = $LastBootTime;
Username = $Username;
User = $Friendly
}
Write-Output $Results | Format-Table
Do you want to export the result in a csv or xls file? – Wasif Hasan – 2020-02-08T17:16:58.387