简体   繁体   English

从日志文件行或 te.net 中获取变量值 output (Powershell)

[英]Get variable value from log file line or telnet output (Powershell)

I have a service that giving me some data from te.net output. Output looks like string: some command 123 I need grab only value - 123, but I don't know how to split the line.我有一项服务,从 te.net output 给我一些数据。Output 看起来像字符串:某些命令 123 我只需要获取值 - 123,但我不知道如何拆分该行。

My powershell script:我的 powershell 脚本:

$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

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.您可以将最后一行更改为以下代码,该代码使用正则表达式提取以空格为前缀的数字字符串,将它们转换为 int,并将其保存到变量中。

$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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM