Trim Powershell 输出

Trim Powershell OutPut

要求是 trim 输出。仅保留 Name 和 remove/avoid 中双引号内引用的输出 lines/characters

发件人:

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

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

收件人:

$R.Output = Domain Users

$R.Output = INTERACTIVE

有人可以协助使用 powershell 开关吗?

您可以使用正则表达式执行此操作以仅捕获这些字符串引号之间的名称部分:

$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

正则表达式详细信息:

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

(?i) 使匹配不区分大小写