Welcome › Forums › General PowerShell Q&A › How do I show everything in the custom table?
- This topic has 4 replies, 3 voices, and was last updated 3 weeks ago by
Participant.
-
AuthorPosts
-
-
December 28, 2020 at 9:11 pm #282847
Hi
How can I display the server name and uptime all in the same custom table?
How can I insert the error message in custom table?Below is the example of the output I am attempting to achieve
PowerShell12345Server Name Uptime----------- ------ServerA 12/12/2020ServerB 24/11/2020ServerC ErrorThis is the code
PowerShell12345678Foreach ($Server in $Serverlist){reboot_time = Get-WmiObject win32_operatingsystem -ComputerName $Server -ErrorAction Stop | %{ $_.ConvertToDateTime($_.LastBootUpTime) }$result = New-Object PsObjectAdd-Member -InputObject $result NoteProperty 'Server Name' $ServerAdd-Member -InputObject $result NoteProperty 'Uptime' $reboot_time$result | Select-Object "Server Name", "Uptime" | Format-Table -AutoSize}-
This topic was modified 3 weeks, 1 day ago by
mrdon03.
-
This topic was modified 3 weeks, 1 day ago by
-
December 29, 2020 at 5:27 am #282925
Hmm, my first Code is gone, so I try it again.
Sorry, if there is a doublePost…PowerShell1234567Foreach ($Server in $Serverlist) {$reboot_time = $(Get-WmiObject win32_operatingsystem -ComputerName $Server -Errorvariable reboot_time | ForEach-Object { $PSItem.ConvertToDateTime($PSItem.LastBootUpTime) }) 2>&1$result = New-Object PsObjectAdd-Member -InputObject $result NoteProperty 'Server Name' $ServerAdd-Member -InputObject $result NoteProperty 'Uptime' $reboot_time$result}-
December 29, 2020 at 6:30 am #282937
Hi it is working.
can you explain “2>&1” part ?
-
-
December 29, 2020 at 6:45 am #282943
There are different Output-Pipes in PS
“2>&1”
1 means the standard output pipe
2 means the error output pipeI redirect the output from ErrorOutputPipe to the StandardOutputPipe
-
December 29, 2020 at 11:09 am #283000
Use calculated expressions to rename the columns or perform conversions:
PowerShell1234567891011$serverList = 'Computer1'$results = Foreach ($Server in $Serverlist) {Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction Stop |Select-Object @{Name='ServerName';Expression={$Server}},@{Name='Uptime';Expression={$_.LastBootUpTime}},@{Name='UptimeInDays';Expression={[math]::Round((New-TimeSpan -Start $_.LastBootUpTime -End (Get-Date)).TotalDays)}},LastBootupTime}$resultsNote that ConvertToDateTime is not required and Powershell does the date conversion and use Get-CimInstance as Get-WmiObject is deprecated.
-
This reply was modified 3 weeks ago by
Rob Simmers.
-
This reply was modified 3 weeks ago by
-
-
AuthorPosts
- You must be logged in to reply to this topic.