Using Powershell, how can I find a specific string in a multiline variable and then find the third word or value in the string

I use Windows PowerShell to clear values ​​from a third-party executable. My goal is to reinstall and simplify the output of the tool, to display only a subset of the content and allow me to collect data on many devices at the same time. I have most of the script work, but I'm stuck in a simple task that is so simple on Linux bash.

A third-party program displays the status of a computer device as standard. I can successfully set the standard content for a variable. For instance:

PS C:\> $status = mycmd.exe device1 --status
PS C:\> $status

The $ status variable will return a multi-line list of values ​​as follows:

Device Number:  1
PCIe slot:  3
Firmware Version:  5.1.4
Temperature:  45C
State:  Online

. Linux - ( ):

Firmware=$(mycmd device1 --status | grep "Firmware" | cut -c 19-24)

Powershell Select-String, :

$Firmware = $Status | select-string -Pattern "Firmware Version"

. , , Powershell, , , , . , .

, $Firmware " 5.1.4", .

+4
5
Firmware = ($Status | Select-String -Pattern '\d{1}\.\d{1,2}\.\d{1,2}' -AllMatches | % { $_.Matches } | % { $_.Value }
+1
$Firmware = ($Status | Select-String -Pattern '(?<=Firmware Version:\s+)[\d.]+').Matches.Value

1 \d , Firmware Version:.

, Select-String , .Matches.Value ( ).

+1

-replace :

$Var = 
@'
Device Number:  1
PCIe slot:  3
Firmware Version:  5.1.4
Temperature:  45C
State:  Online
'@

$Firmware = $var -replace '(?ms).+^Firmware Version:\s+([0-9.]+).+','$1'
$Firmware

5.1.4
+1

... [system.version] ( ).

$FirmwareLine = $Status | select-string -Pattern "Firmware Version"| select -expand line
[system.version]$firmware=$firmwareLine -split ":" | select -first 1 -skip 1

, "cast"

$firmware=$firmwareLine -split ":" | select -first 1 -skip 1 
0

Another option is to replace the colons with "=", convert the output to key = value pairs and then turn them into a hash table (and then into a PS object, if you want) using ConvertFrom-StringData:

$Var = 
@'
Device Number:  1
PCIe slot:  3
Firmware Version:  5.1.4
Temperature:  45C
State:  Online
'@

$DeviceStatus = New-object PSObject -Property $(ConvertFrom-StringData $var.Replace(':','='))

$DeviceStatus.'Firmware Version'

5.1.4
0
source

All Articles