Reputation: 91
I am learning powershell as part of windows administration and I have a bit of a problem. I need to run sysinfo command for various systems and pick up the OS Name
, OS Version
, Total Physical Memory
and other fields from the output. My problem is the output is not a powershell object, so I can't use property based processing. What can I do to pick up only these fields ? I tried using findstr, but for fields like Hotfix, which have multiple values, it only picks up the first line.
Command
systeminfo /S <IP Address> /U Administrator /P <Password>
Output:
Host Name: TEST
OS Name: Microsoft Windows Server 2008 R2 Standard
OS Version: 6.1.7601 Service Pack 1 Build 7601
OS Manufacturer: Microsoft Corporation
OS Configuration: Standalone Server
OS Build Type: Multiprocessor Free
Upvotes: 1
Views: 4998
Reputation: 13422
Running systeminfo -?
indicates the command supports a format option. Using the CSV format, you can let PowerShell import the output directly:
systeminfo -fo CSV | ConvertFrom-Csv
This will return a custom PSObject containing properties like OS Name
, Host Name
, etc, which you can access with quotes (as Matt mentioned in his answer): $csv.'OS Name'
For reference, here is what systeminfo -?
shows on my machine:
SYSTEMINFO [/S system [/U username [/P [password]]]] [/FO format] [/NH]
Description:
This tool displays operating system configuration information for
a local or remote machine, including service pack levels.
Parameter List:
/S system Specifies the remote system to connect to.
/U [domain\]user Specifies the user context under which
the command should execute.
/P [password] Specifies the password for the given
user context. Prompts for input if omitted.
/FO format Specifies the format in which the output
is to be displayed.
Valid values: "TABLE", "LIST", "CSV".
/NH Specifies that the "Column Header" should
not be displayed in the output.
Valid only for "TABLE" and "CSV" formats.
/? Displays this help message.
Examples:
SYSTEMINFO
SYSTEMINFO /?
SYSTEMINFO /S system
SYSTEMINFO /S system /U user
SYSTEMINFO /S system /U domain\user /P password /FO TABLE
SYSTEMINFO /S system /FO LIST
SYSTEMINFO /S system /FO CSV /NH
Upvotes: 1
Reputation: 46700
I am making another answer in a attempt to not loose the simplicity of my first answer. To continue:
$sysInfo= systeminfo
$output = $sysInfo |
Where-Object{($_ -like "OS*") -or ($_ -like "Host Name:*")} |
ForEach-Object{ $_ -replace ":\s+"," = "}
ConvertFrom-StringData($output | Out-String)
Again, the systeminfo
is captured into a variable $sysInfo
. Capture the desired output in $output
.
This time I run each line with a replace using the ForEach-Object -replace ":\s+"," = "
. The reason is I am going to turn the text into a hash table which is a series of name = value
. The regex replace changes the colon and any following white space into =
.
The $output is of type System.Object[]
. However we need it to be a string in my example to it can be put into ConvertFrom-StringData which is why we pipe $output into Out-String.
That being said you get the following:
Name Value
---- -----
OS Manufacturer Microsoft Corporation
OS Version 6.1.7601 Service Pack 1 Build 7601
OS Configuration Member Workstation
OS Name Microsoft Windows 7 Professional
Host Name C4093
OS Build Type Multiprocessor Free
Which you can now use powershell cmd-lets against
$osInfo = ConvertFrom-StringData($output | Out-String)
$osInfo."Os Name"
"Os Name"
needs to be in quotes as it contains a space.
Upvotes: 0
Reputation: 46700
I know i can make the answer better but at least a start would be this:
$file = systeminfo
$file | Where-Object{($_ -like "OS*") -or ($_ -like "Host Name:*")}
Capture the systeminfo into a variable (array) that can be piped into Where-Object to parse out the data you are looking for
output:
Host Name: TE_ST
OS Name: Microsoft Windows 7 Enterprise
OS Version: 6.1.7601 Service Pack 1 Build 7601
OS Manufacturer: Microsoft Corporation
OS Configuration: Standalone Workstation
OS Build Type: Multiprocessor Free
I am trying to turn it into a proper object to improve my answer
Upvotes: 0