简体   繁体   English

从字符串 PowerShell 中删除文本

[英]Remove text from string PowerShell

I´m vacation hangover and cannot wrap my head around an Powershell dilemma!我是假期宿醉,无法解决 Powershell 的困境!

I´ve a script where I get the following output below which is saved to a variable $ScanOutput .我有一个脚本,我在其中得到以下 output ,下面将其保存到变量$ScanOutput Now I want to filter out and only keep the code after ExitCode= (Can be 500 or 0).现在我想过滤掉并只保留ExitCode=之后的代码(可以是 500 或 0)。 How can I filter out everything before and after 500 (which can be other codes, not static).如何过滤掉500之前和之后的所有内容(可以是其他代码,不是静态的)。

@{StdOut=Checking for updates...
Determining available updates...
Check for updates completed
Number of applicable updates for the current system configuration: 0
No updates available.
Execution completed.
Program exited with return code: 500
; ExitCode=500; StdErr=}

Thanks in advance, Kind regards, JN在此先感谢,亲切的问候,JN

You can simply use Select-String :您可以简单地使用Select-String

($ScanOutput | Select-String -Pattern 'ExitCode=(\d+)').Matches.Groups[1].Value
500

Many ways to skin a cat, assuming the delimiter ;假设分隔符有很多方法可以给猫剥皮; is consistently placed.是一致放置的。 Might be nice to test which method is fastest:测试哪种方法最快可能会很好:

($scanoutput -split ';')[1].Trim()
ExitCode=500

Since JNilsson came back and said that I actually was correct in my statement that it was hash table I will add back my answer to help others with similar problem.由于 JNilsson 回来并说我在声明中实际上是正确的,即它是 hash 表,我将添加我的答案以帮助其他有类似问题的人。

The object object

@{StdOut=Checking for updates... Determining available updates... Check for updates completed Number of applicable updates for the current system configuration: 0 No updates available. Execution completed. Program exited with return code: 500 ; ExitCode=500; StdErr=}

To get the ExitCode from the object simply make sure to assign it to a variable, in my case $hash then you can get the value by doing this:要从 object 获取 ExitCode,只需确保将其分配给一个变量,在我的情况下为 $hash 然后您可以通过执行以下操作获取值:

$hash.ExitCode

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

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