Export-csv powershell get-uptime of computers

by powershellds at 2013-04-27 00:14:03

Hello experts

I am trying to use the below function and export csv list of computers with information like LoginID, Displayname, Computername, Last Boot time and TIme since last reboot.
I found this function on the internet it works fine and I get the output as shown below, if I run it on individual computer, however I am not able to export-csv in proper format.
Please help me for the same.

PS D:\ad> Get-Uptime abc-ab1852
LoginId : abcdef
UserName : Lastname, Firstname
Server : abc-ab1852
Last Reboot : 4/23/2013 11:48:12 AM
Time Since Reboot : 4 Days 0 Hours 25 Minutes 49 Seconds

I tried the below script to export output in a csv format, this is how I get the export in a single coloum of the csv file.

Get-ADComputer -Filter {(Name -like "abc-ab185*")} -property dnshostname, name | foreach-object {if (Test-connection -computername $
.dnshostname -count 1 -quiet)
$uptime = Get-Uptime $
write-host -foregroundcolor green "System $
.dnshostname is Down"
new-object -TypeName PSObject -Property @{
AllData = $uptime
} | select-object AllData
} | export-csv -path C:\export\test1237.csv -notypeinformation

CSV Output

@{LoginId=abcdef; UserName=Lastname, Firstname; Server=abc-ab1852.ab.abcd.com; Last Reboot=4/23/2013 11:48:12 AM; Time Since 4 Days 0 Hours 25 Minutes 49 Seconds}

I would like to have an output in different coloum like shown below
PS D:\ad> Get-Uptime abc-ab1852 | Select-Object * | ft -Wrap
LoginId UserName Server Last Reboot Time Since Reboot
------- -------- ------ ----------- -----------------
abcdef Lastname, Firstname abc-ab1852 4/23/2013 11:48:12 AM 4 Days 0 Hours 40 Minut
es 12 Seconds[/list]

Please help me how can I achieve it ?

#Function Get-Uptime

import-module activedirectory
Function Get-Uptime {
param( [Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]

[string[]]$ComputerName = $env:COMPUTERNAME
foreach ($computer in $computername) {
$LastBoot=[System.Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject win32_operatingsystem -ComputerName $($computer)).lastbootuptime)
$user = (gwmi -class win32_process -computername $($computer) -Filter "Name = ‘explorer.exe’").Getowner().user
$username = get-aduser -identity $user -property Displayname | select-object -ExpandProperty Displayname
$Result=@{ “Server”=$($Computer);
“Last Reboot”=$LastBoot;
“Time Since Reboot”=”{0} Days {1} Hours {2} Minutes {3} Seconds” -f ($Now – $LastBoot).days,
($Now – $LastBoot).hours,($Now – $LastBoot).minutes,($Now – $LastBoot).seconds}
Write-Output (New-Object psobject -Property $Result|select LoginId, UserName, Server, “Last Reboot”, “Time Since Reboot”)

by Martin9700 at 2013-04-27 03:50:50
Well, you’re loading all your data into a hashtable, then trying to output that as an object. Why not just output the object directly and bypass the middleman? First I’d move Import-Module inside your function, and define the object in a Begin block:

Import-Module ActiveDirectory
$Result = @()

Then add your data into the $Result variable:

$Result += New-Object PSObject -Property @{
“Server” = $Computer
“Last Reboot” = $LastBoot
"LoginId" = $user
"UserName" = $username
“Time Since Reboot” = ”{0} Days {1} Hours {2} Minutes {3} Seconds” -f ($Now – $LastBoot).days,($Now – $LastBoot).hours,($Now – $LastBoot).minutes,($Now – $LastBoot).seconds}

Then, when all done, you can use select to output the fields in the order you want them to come out. Use the END block so it’ll output after it’s gathered everything.

$Result | Select LoginID,UserName,Server,'Last Reboot','Time Since Reboot'

If you’re on Powershell 3.0 you can also used an ordered object and save yourself saving $Results and just output the object directly in the PROCESS block:

New-Object PSObject -Property [ordered]@{
…properties and values here…
by RichardSiddaway at 2013-04-29 05:08:47
If you can use PowerShell v3 try using
Get-CimInstance -ClassName Win32_OperatingSystem

the date is returned already translated in to human format