Powershell 使用键值对拆分字符串

Powershell 使用键值对拆分字符串,powershell,Powershell,我从文本文件中读取此字符串。如何获取两个键的值 statusId=0 statusError=Info: Using ID '8d07afe6-4fc3-47a3-9583-571f79ca15ae' for connections to the remote server. 由于内容原因,我无法轻松拆分空格或分号。如果字符串始终是格式的 statusId=<numbers> statusError=<whatever> 模式“^(statusId=\d+\s+(.

我从文本文件中读取此字符串。如何获取两个键的值

statusId=0  statusError=Info: Using ID '8d07afe6-4fc3-47a3-9583-571f79ca15ae' for connections to the remote server.

由于内容原因,我无法轻松拆分空格或分号。

如果字符串始终是格式的

statusId=<numbers> statusError=<whatever>

模式
“^(statusId=\d+\s+(.+)”
查找以
statusId=
开头的字符串,至少有一个数字后跟至少一个空格,然后任何内容都会出现。通过
$Matches
变量访问结果。然后,使用
-split'='
将匹配分为两部分,并使用
[1]
索引器,到达“Info:”部分。

最简单的方法是在键上拆分:

$statusId,$statusError = $string -split 'statusId=|statusError='

您可能应该发布您希望输出的样子…我需要两个键的值。我不喜欢output.cool代码,今天学到了一些关于powershell的新知识。刚刚测试过它的工作原理!谢谢我不知道为什么或者实际上我改变了什么没有太多的代码,但现在它不再工作了:/I get:statuscode:---------------------------statusmessage:0信息:使用ID,零数字应该在“statuscode:”之后,但它不是吗?该死的,它以前工作过。。。这就是我的代码:$statusCode,$statusMessage=$statusContent-split'statusCode=| statusMessage='看起来您的输入中可能有一些意外的东西。
$statusId,$statusError = $string -split 'statusId=|statusError='