I am attempting to write a simple “baseline” script that will retrieve numerous properties from a few servers (OS, RAM, RDP status, PSVersion, disk info, etc). Obtaining this information is done, mostly using WMI, and each one is simply formatted like:
Write-Output "GENERAL INFORMATION"
"Operating System: $OperatingSystem"
"Remote Desktop Enabled: $(Get-RemoteDesktopState)"
I need to put these items into a table (indented from heading) for better legibility. I’m still sharpening my PowerShell skills; I’ve tried arrays, lists, etc. but unwanted things like RunspaceId and PSComputerName are added, or there’s a bunch of padding around the set. A set of information would exist under each heading.
In the meantime, I’ve been going through Don Jones’ “Learn PowerShell In A Month of Lunches, 3rd Edition” but can’t really wait to complete it and the Toolmaking edition before I figure out how to do these types of things.
Any suggestions?
Thank you.
Do you need this info in a file? IE: csv or txt
You need to create a Custom Object.
$OperatingSystem = "Windows 10"
$RemoteDesktop = "Enabled"
$result = New-Object PSObject
Add-Member -input $result NoteProperty 'Operating System' $OperatingSystem
Add-Member -input $result NoteProperty 'Remote Desktop' $RemoteDesktop
$result | Select "Operating System", "Remote Desktop"`
Thank you for the responses.
My apologies for not being clear. I meant to indicate I needed this information in a ‘table-like’ structure in that the data isn’t just spat out in a default form. Perhaps similar to how a Format-List works; assets listed on the left (OS, RAM, etc.) and their values listed on the right, rather than an actual table. When using a list, it includes RunspaceId and PSComputerName which I don’t want, and using -ExcludeProperty does not remove them, nor can I limit the padding around the list to reduce wasted screen real estate.
I came across one article that required each asset to go through 2 or 3 objects and then throw the final one into an array. I think that’s overkill and an inefficient way to script, unless there is nothing better out there.
Using Solomon’s suggestion works when piping to fl but it adds 4 blank lines beneath it which is what I am trying to avoid (2 lines above the list is ok for my purposes).
Solomon, when using your example in ISE, RunSpaceId and PSComputerName do not appear but they do in the PowerShell console. Any ideas how to exclude these in the console?
Thanks again.
Solomon’s response will get you what you want. Once you have the object you can exclude the properties you do not want that were added by the remote session.
At the point that you just want to output and trim the extra blank lines you can use format-list, output it as a string and then trim the string
$OperatingSystem = "Windows 10"
$RemoteDesktop = "Enabled"
$result = New-Object PSObject
Add-Member -input $result NoteProperty 'Operating System' $OperatingSystem
Add-Member -input $result NoteProperty 'Remote Desktop' $RemoteDesktop
$result | Select "Operating System", "Remote Desktop" -ExcludeProperty PSComputerName, RunspaceId
($result | Select "Operating System", "Remote Desktop" -ExcludeProperty PSComputerName, RunspaceId|format-list|out-string).Trim()
Thank you Jonathan! I was so close and as always, something very simple hinders the desired outcome.
Here is what I originally tried:
$array | Select "Operating System","Remote Desktop" -ExcludeProperty PSComputerName,RunspaceId
But this didn’t remove the items in -ExcludeProperty. Maybe to do with piping to Format-List?
Trim() also didn’t work originally but likely due to the preceding code not being in (…) like your example.
Greatly appreciate it kind sir!
(And thanks to Solomon for initial direction.)