I am trying to search for a string called "Hostname" from a variable "$a" which stores the output of "systeminfo" command but ran into issues. I am using "system-info" to gather a bigger set of data.
Code:
$a= (get-systeminfo -computername localhost | select-object hostname, OSNAME, osversion, model, type)
write-output $a
select-string $a -pattern "hostname"Error:
Select-String : Cannot find path 'C:\Users\john001c\@{Hostname=PACCPL-FTN1TM1; OSName=Microsoft Windows 7 Enterprise ; OSVersion=6.1.7601 Service Pack 1 Build 7601; Model=Latitude E
4310; Type=x64-based PC}' because it does not exist.
At line:5 char:14
+ select-string <<<< $a -pattern "hostname" + CategoryInfo : ObjectNotFound: (C:\Users\john...e=x64-based PC}:String) [Select-String], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SelectStringCommandWhat is the correct way to search for a string inside a variable?
31 Answer
Select-string has multiple parameter sets. To search within an object (instead of a file) your parameters would look like this. Note the use of the -InputObject parameter is required for usage against something that isn't a file, and it isn't usable as a positional paramater, the -InputObject parameter must be explicitly named.
Parameter Set: Object Select-String [-Pattern] -InputObject
So try this intead.
select-string -pattern "hostname" -InputObject $a 1