简体   繁体   English

修剪 Powershell OutPut

[英]Trim Powershell OutPut

Requirement is to trim the Output.要求是修整 Output。 Retain only the output quoted within double quotes from Name and remove/avoid the earlier lines/characters仅保留名称中双引号内引用的 output 并删除/避免较早的行/字符

From:从:

$R.Output = \\GBVServer1\root\cimv2:Win32_Group.Domain="Contoso",Name="Domain Users"

$R.Output = \\GBVServer1\root\cimv2:Win32_SystemAccount.Domain="GBVServer1",Name="INTERACTIVE"

To:至:

$R.Output = Domain Users

$R.Output = INTERACTIVE

Could somebody assist with the powershell switch to be used?有人可以协助使用 powershell 开关吗?

You can do this with regex to capture only the Name part between the quotes for these strings:您可以使用正则表达式执行此操作,以仅捕获这些字符串的引号之间的名称部分:

$regex = [regex]'(?i)Name="([^,]+)"'

$string = '\\GBVServer1\root\cimv2:Win32_Group.Domain="Contoso",Name="Domain Users"'
$R.Output = $regex.Match($string).Groups[1].Value  # --> Domain Users

$string = '\\GBVServer1\root\cimv2:Win32_SystemAccount.Domain="GBVServer1",Name="INTERACTIVE"'
$R.Output = $regex.Match($string).Groups[1].Value  # --> INTERACTIVE

Regex details:正则表达式详细信息:

Name="      Match the characters “Name="” literally
(           Match the regular expression below and capture its match into backreference number 1
   [^,]     Match any character that is NOT a “,”
      +     Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
"           Match the character “"” literally

The (?i) makes the match case-insensitive (?i)使匹配不区分大小写

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

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