PowerShell选择值 -notlike 具有多行值

huangapple go评论73阅读模式
英文:

PowerShell select value -notlike that has multiple line values

问题

我只想返回 {74.74.74.1, 74.74.74.2} 这个数值。
但是当我尝试筛选出 {10.88.2.44, 148.175.100.45} 的IP值时,它不起作用,因为ServerAddresses数据字段包含了2个独立的值,而不仅仅是一个。
这是我正在使用的查询:

Get-DnsClientServerAddress | where {($_.serveraddresses -notlike "*10.*" -and 
$_.serveraddresses -notlike "*148.*" -and $_.InterfaceAlias -eq "Ethernet")}

谢谢

英文:

I am querying local desktops to find out their DNS settings, but want to filter out company DNS ip addresses and just report back the internet provider DNS addresses, but the addresses values are being returned on multiple lines (2 separate values) because there are 2 IP addresses associated with each column value.
Like this..

InterfaceAlias               Interface Address ServerAddresses                      PSComputerName
                             Index     Family
--------------               --------- ------- ---------------                      --------------
Ethernet                            10 IPv4    {10.88.2.44, 148.175.100.45}        Computer1
Ethernet                            10 IPv4    {74.74.74.1, 74.74.74.2}        	   Computer1

i just want to return back the {74.74.74.1, 74.74.74.2} value.
But when i try to filter out the {10.88.2.44, 148.175.100.45} IP values, its not working because the ServerAddresses data field is 2 separate values, not just one.
This is the query i am using.

Get-DnsClientServerAddress | where {($_.serveraddresses -notlike "*10.*" -and 
$_.serveraddresses -notlike "*148.*" -and $_.InterfaceAlias -eq "Ethernet")}

thank you

答案1

得分: 1

以下是翻译好的代码部分:

<!-- language-all: sh -->

以下返回所有接口别名为 `Ethernet` 且服务器IP地址数组仅包含不以 `10.``148.` 开头的地址的对象:

```shell
Get-DnsClientServxerAddress |
  Where-Object {
    $_.InterfaceAlias -eq 'Ethernet' -and 
      -not ($_.ServerAddresses -match '^(10|148)\.')
  }
  • 此处使用基于正则表达式-match 操作符,而不是基于通配符-like 操作符,因为它允许您在单个操作中查找 多个 模式,使用交替 (|)。 (正则表达式通常比通配符表达式更强大。)

  • 所有 PowerShell 的比较操作符都可以在其左操作数(LHS)为_数组_时运作,此时它们充当_过滤器_,返回匹配值的_子数组_,而不仅仅是 $true$false

  • -match '^(10|148)\.' 因此返回以 (^) 10148 开头,后跟文字 . (\.,因为 . 在正则表达式中具有特殊含义)

  • -not 应用于可能为空的匹配值子数组会导致它首先强制转换为布尔值: 子数组变为 $false(由 -not 转换为 $true),而_非空_ 子数组(表示存在一个或多个 10.148. 地址)变为 $true(由 -not 转换为 $false)。

    • 其净效应是,如果存在 10.148. 地址 - 或两者都存在 - 则整体表达式为 $false
  • 请注意,尽管 -match 有一个否定的对应物,即 -notmatch,但后者不能在此处使用:

    • $_.ServerAddresses -notmatch '^(10|148)\.' 将返回10.148. 开头的元素的子数组,这意味着_还或者独占地_包含一个或多个_不同_地址的数组将产生_非空_ 子数组,由于被强制转换为布尔值,将产生 $true

<details>
<summary>英文:</summary>

&lt;!-- language-all: sh --&gt;

The following returns all objects whose interface alias is `Ethernet` and whose array of server IP addresses only contains addresses that start _neither_ with `10.` nor `148.`:

Get-DnsClientServxerAddress |
Where-Object {
$.InterfaceAlias -eq 'Ethernet' -and
-not ($
.ServerAddresses -match '^(10|148).')
}


* The [regex](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Regular_Expressions)-based [`-match`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Comparison_Operators#-match-and--notmatch) operator is used in lieu of the [wildcard](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_wildcards)-based [`-like`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Comparison_Operators#-like) operator, because it allows you to look for _multiple_ patterns in a single operation, using alternation (`|`). (Regexes are generally more powerful than wildcard expressions.)

* All of PowerShell&#39;s comparison operators can operate on _arrays_ as their LHS, in which case they act as _filters_, returning a _subarray of matching values_ rather than just `$true` or `$false`.

* `-match &#39;^(10|148)\.&#39;` therefore returns those elements that start with (`^`) either (`|`) `10` or `148` followed by a literal `.` (`\.`, which is escaped, because `.` has special meaning in regexes)

* Applying `-not` to the - possibly empty - subarray of values returned causes it to be coerced to a Boolean first: an _empty_ subarray becomes `$false` (which `-not` turns to `$true`), and any _non-empty_ subarray (indicating the presence of one or more `10.` or `148.` addresses) becomes `$true` (which `-not` turns to `$false`).

   * The net effect is that if a `10.` or `148.` address - or both - are present, the overall expression is `$false`.
 
* Note that even though `-match` has a negated counterpart, `-notmatch`, the latter can _not_ be used here: 
  * `$_.ServerAddresses -notmatch &#39;^(10|148)\.&#39;` would return the subarray of elements *not* starting with `10.` or `148`, which means that an array that _also or exclusively_ contains one or more _different_ addresses would yield a _non-empty_ subarray, which - due to being coerced to a Boolean - would yield `$true`



</details>



# 答案2
**得分**: 0

- 在左侧有数组时,-notlike 的工作方式会有些不同。一种方法是将两个地址连接成一个由空格分隔的字符串。

```PowerShell
'10.88.2.44', '148.175.100.45' -notlike '*10.*'

148.175.100.45

$a = [pscustomobject]@{serveraddresses = '10.88.2.44', '148.175.100.45' },
     [pscustomobject]@{serveraddresses = '74.74.74.1', '74.74.74.2';}
$a | where {$_.serveraddresses -join ' ' -notlike '*10.*' -and 
            $_.serveraddresses -join ' ' -notlike '*148.*'}

serveraddresses
---------------
{74.74.74.1, 74.74.74.2}

你可以尝试这个表达式,但它会通过任何其他 IP 地址:

'10.88.2.44', '148.175.100.45' -notlike '*10.*' -notlike '*148.*'
 
# 没有输出

'10.88.2.44', '148.175.100.45', '74.74.74.1' -notlike '*10.*' -notlike '*148.*'

74.74.74.1
英文:

That is a little tricky. Where there's an array on the left, -notlike works differently. One way is to join the two addresses into a string seperated by spaces.

&#39;10.88.2.44&#39;, &#39;148.175.100.45&#39; -notlike &#39;*10.*&#39;

148.175.100.45


$a = [pscustomobject]@{serveraddresses = &#39;10.88.2.44&#39;, &#39;148.175.100.45&#39; },
     [pscustomobject]@{serveraddresses = &#39;74.74.74.1&#39;, &#39;74.74.74.2&#39;}
$a | where {$_.serveraddresses -join &#39; &#39; -notlike &#39;*10.*&#39; -and 
            $_.serveraddresses -join &#39; &#39; -notlike &#39;*148.*&#39;}

serveraddresses
---------------
{74.74.74.1, 74.74.74.2}

You can try this expression, but it will let through any other ip's:

&#39;10.88.2.44&#39;, &#39;148.175.100.45&#39; -notlike &#39;*10.*&#39; -notlike &#39;*148.*&#39;
 
# no output


&#39;10.88.2.44&#39;, &#39;148.175.100.45&#39;, &#39;74.74.74.1&#39; -notlike &#39;*10.*&#39; -notlike &#39;*148.*&#39;

74.74.74.1

答案3

得分: 0

根据我的评论。
现在请注意,我在这里使用了 match,但使用其他比较运算符应该产生与您所期望的相同结果。

清除主机

创建一个样本数据集以便处理,避免显示个人系统详细信息

设置数组

$DNSServerDataRecords = New-Object System.Collections.ArrayList($null)

使用多维数组进行记录构造,并循环以填充表格

(('Ethernet'), ('10.88.2.44', '148.175.100.45', '127.0.0.0')),
(('vEthernet'), ('10.88.2.22', '148.175.100.50', '127.0.0.0')),
(('vEthernet'), ('74.74.74.1', '74.74.74.2', '8.8.8.8')) |
ForEach-Object {
$DNSServerData = [PSCustomObject]@{
InterfaceAlias = $PSitem[0]
ServerAddresses = $PSitem[1]
}
[void]$DNSServerDataRecords.Add($DNSServerData)
}

显示表格构建结果

$DNSServerDataRecords

结果

按目标标准解析记录

清除主机
$DNSServerDataRecords |
Where-Object {$PSitem.ServerAddresses -match '10';}

结果

清除主机
$DNSServerDataRecords |
Where-Object {$PSitem.ServerAddresses -match '148';}

结果

清除主机
$DNSServerDataRecords |
Where-Object {$PSitem.ServerAddresses -match '10|148';}

结果

清除主机
$DNSServerDataRecords |
Where-Object {$PSitem.ServerAddresses -match '10' -and $PSitem.InterfaceAlias -eq 'Ethernet';}

结果

清除主机
$DNSServerDataRecords |
Where-Object {$PSitem.ServerAddresses -match '10' -and $PSitem.InterfaceAlias -eq 'vEthernet';}

结果

英文:

As per my comment.
Now, note I am using match here, but using the other comparison operators, should yield the same as what you are after.

Clear-Host
# Create a sample data set to work with and avoid displaying personal system details

# Set array
$DNSServerDataRecords = New-Object System.Collections.ArrayList($null)

# Use multi-dimentional array for record construction and loop to populat a table
((&#39;Ethernet&#39;), (&#39;10.88.2.44&#39;, &#39;148.175.100.45&#39;, &#39;127.0.0.0&#39;)),
((&#39;vEthernet&#39;), (&#39;10.88.2.22&#39;, &#39;148.175.100.50&#39;, &#39;127.0.0.0&#39;)),  
((&#39;vEthernet&#39;), (&#39;74.74.74.1&#39;, &#39;74.74.74.2&#39;, &#39;8.8.8.8&#39;)) | 
ForEach-Object {
        $DNSServerData = [PSCustomObject]@{
            InterfaceAlias  = $PSitem[0]
            ServerAddresses = $PSitem[1]
        }
        [void]$DNSServerDataRecords.Add($DNSServerData)
}

# Show table build results
$DNSServerDataRecords
# Results
&lt;#
InterfaceAlias ServerAddresses                        
-------------- ---------------                        
Ethernet       {10.88.2.44, 148.175.100.45, 127.0.0.0}
vEthernet      {10.88.2.22, 148.175.100.50, 127.0.0.0}
vEthernet      {74.74.74.1, 74.74.74.2, 8.8.8.8}  
#&gt;

# Parse the records by target criteria
Clear-Host
$DNSServerDataRecords | 
Where-Object {$PSitem.ServerAddresses -match &#39;10&#39;}
# Results
&lt;#
InterfaceAlias ServerAddresses                        
-------------- ---------------                        
Ethernet       {10.88.2.44, 148.175.100.45, 127.0.0.0}
vEthernet      {10.88.2.22, 148.175.100.50, 127.0.0.0}
#&gt;

Clear-Host
$DNSServerDataRecords | 
Where-Object {$PSitem.ServerAddresses -match &#39;148&#39;}
# Results
&lt;#
InterfaceAlias ServerAddresses                        
-------------- ---------------                        
Ethernet       {10.88.2.44, 148.175.100.45, 127.0.0.0}
vEthernet      {10.88.2.22, 148.175.100.50, 127.0.0.0}
#&gt;

Clear-Host
$DNSServerDataRecords | 
Where-Object {$PSitem.ServerAddresses -match &#39;10|148&#39;}
# Results
&lt;#
InterfaceAlias ServerAddresses                        
-------------- ---------------                        
Ethernet       {10.88.2.44, 148.175.100.45, 127.0.0.0}
vEthernet      {10.88.2.22, 148.175.100.50, 127.0.0.0}
#&gt;

Clear-Host
$DNSServerDataRecords | 
Where-Object {$PSitem.ServerAddresses -match &#39;10&#39; -and $PSitem.InterfaceAlias -eq &#39;Ethernet&#39;}
# Results
&lt;#
InterfaceAlias ServerAddresses                        
-------------- ---------------                        
Ethernet       {10.88.2.44, 148.175.100.45, 127.0.0.0}
#&gt;

Clear-Host
$DNSServerDataRecords | 
Where-Object {$PSitem.ServerAddresses -match &#39;10&#39; -and $PSitem.InterfaceAlias -eq &#39;vEthernet&#39;}
# Results
&lt;#
InterfaceAlias ServerAddresses                        
-------------- ---------------                        
vEthernet      {10.88.2.22, 148.175.100.50, 127.0.0.0}
#&gt;

huangapple
  • 本文由 发表于 2023年3月7日 01:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定