SCCM PowerShell Output

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

AstinTeeb

Posted 2020-02-08T16:32:42.057

Reputation: 75

Do you want to export the result in a csv or xls file? – Wasif Hasan – 2020-02-08T17:16:58.387

Answers

0

If you want to export the result into a CSV file use this:

$result.GetEnumerator() | Export-CSV "filepath.csv" -NoTypeInformation

Or if you want to put it in a excel spreadsheet:

$excel = new-Object -comobject Excel.Application
$excel.visible = $true # set it to $false if you don't need monitoring the actions...
$workBook = $excel.Workbooks.Add()
$sheet =  $workBook.Sheets.Item(1)
$sheet.Name = "Result"
$sheet.Range("A1","A2").ColumnWidth = 40
$sheet.range('A:A').VerticalAlignment = -4160 #align is center (TOP -4108 Bottom -4107 Normal)

$sheet.Cells.Item(1,1) = " "
$sheet.cells.Item(1,2) = " "

$index = 2

$result.keys | ForEach {  

    $sheet.Cells.Item($index,1) = $_
    $sheet.Cells.Item($index,2) = $result.item($_)
    $index++
}

$workBook.SaveAs("C:\mylist.xls")
$excel.Quit()

Wasif Hasan

Posted 2020-02-08T16:32:42.057

Reputation: 827