I was asked today to write this script that provides last boot time of computers based on SCCM collection, the computers can come from many sources like Active Directory group or text file not just SCCM collection, hope you’ll find it useful too.

$CollectionName = 'CollectionName' $file = "Csv report file path" $SiteServer = 'SCCMSERVER' $SiteCode = 'YourSCCMSiteCode' #Get the collection using WMI $Collection = get-wmiobject -ComputerName $siteServer -NameSpace "ROOT\SMS\site_$SiteCode" -Class SMS_Collection | where {$_.Name -eq "$CollectionName"} #Get the collection members $CollectionMmebers = Get-WmiObject -ComputerName $SiteServer -Namespace "ROOT\SMS\site_$SiteCode" -Query "SELECT * FROM SMS_FullCollectionMembership WHERE CollectionID='$($Collection.CollectionID)' order by name" | select Name #The $CollectionMembers can come from other places like a file, group in Active Directory etc. $data = @() ForEach($Computer in $CollectionMmebers) { try { $operatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $($Computer.Name) $lastBoot = [Management.ManagementDateTimeConverter]::ToDateTime($operatingSystem.LastBootUpTime) } catch { $lastBoot = "ERROR Connecting" } $data += New-Object PSObject -Property @{ Name = $Computer.Name "Last Boot" = $lastBoot } } $data | Export-Csv $file

Share this: Twitter

Facebook

Like this: Like Loading... Related

Tagged: Get-WmiObject