'Get variable value from log file line or telnet output (Powershell)

I have a service that giving me some data from telnet output. Output looks like string: some command 123 I need grab only value - 123, but I don't know how to split the line.

My powershell script:

$wshell = new-object -com wscript.shell
start-process telnet -argumentlist "127.0.0.1 7000 -f C:\7000_output.txt"; 
sleep 3; 
$wshell.SendKeys("some command{ENTER}")
sleep 1;
$wshell.SendKeys("quit{ENTER}")
Select-String "C:\7000_output.txt" -pattern "some command" | Select-Object -Last 1


Solution 1:[1]

You could change that last line to the following code, which uses regex to extract an string of digits prefixed by a space, converts them to an int, and saves it to a variable.

$MyVariable = 0
Select-String "C:\7000_output.txt" -pattern "some command" | Select-Object -Last 1 | ForEach-Object {
    if($_ -match '^.+\s(?<Number>\d+).*$') { $script:MyVariable = [int]($Matches.Number) }    
}
Write-Host "MyVariable: $MyVariable"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1