Powershell etc/主机文件条目是否匹配如果不匹配条目则发送触发器

Powershell etc/主机文件条目是否匹配如果不匹配条目则发送触发器,powershell,Powershell,在这里,我不熟悉PowerShell脚本。我正在尝试执行以下操作:主机文件条目应与来自C:\Windows\System32\drivers\等的IP和DNS名称条目动态列表中的每个条目匹配。任何不匹配都将导致审核失败 参赛作品为 10.23.24.45 foo.com 10.24.45.34 domain.com 这是我的密码 "$Pattern = '^(?<IP>\d{1,3}(\.\d{1,3}){3})\s+(?<H

在这里,我不熟悉PowerShell脚本。我正在尝试执行以下操作:主机文件条目应与来自C:\Windows\System32\drivers\等的IP和DNS名称条目动态列表中的每个条目匹配。任何不匹配都将导致审核失败

参赛作品为

10.23.24.45           foo.com
10.24.45.34           domain.com
这是我的密码

"$Pattern = '^(?<IP>\d{1,3}(\.\d{1,3}){3})\s+(?<Host>.+)$'
$File    = "$env:SystemDrive\Windows\System32\Drivers\etc\hosts"
$Entries = @()

(Get-Content -Path $File)  | ForEach-Object {
  If ($_ -match $Pattern) {
    $Entries += "$($Matches.IP)  $($Matches.Host)"
    Write-Host " the values are $Entries"
    $FailureMessage = "IP and host entries are existing"
  }
  else {
    $FailureMessage = "IP and host entries are doesn't existing"
  }
}"
“$Pattern=”^(?\d{1,3}(\.\d{1,3}){3})\s+(?。+)$”
$File=“$env:SystemDrive\Windows\System32\Drivers\etc\hosts”
$Entries=@()
(获取内容-路径$File)| ForEach对象{
如果($\匹配$模式){
$Entries+=“$($Matches.IP)$($Matches.Host)”
写入主机“值为$Entries”
$FailureMessage=“IP和主机条目已存在”
}
否则{
$FailureMessage=“IP和主机条目不存在”
}
}"

但这对我不起作用。您能在这里提供帮助吗?

主机文件还可以在IP和主机名部分之后包含注释行或注释,前面是
字符。你现在的正则表达式不能解释这一点

我将创建一个包含所有必需项的查找哈希表,并使用该哈希表来查找hosts文件是否包含这些项,从而创建一个psobject数组,以实现良好的格式设置和轻松的过滤

比如:

$file    = "$env:SystemDrive\Windows\System32\Drivers\etc\hosts"
$pattern = '^\s*(?<IP>[0-9a-f.:]+)\s+(?<HostName>[^\s#]+)(?<Comment>.*)$'
# create an array of Hashtables with required entries
$required = @{Ip = '10.23.24.45'; HostName = 'foo.com'},
            @{Ip = '10.24.45.34'; HostName = 'domain.com'}

# read the current content of the hosts file, filter only lines that match the pattern
$result = Get-Content -Path $file | Where-Object { $_ -match $pattern } | ForEach-Object {
    $ip = $matches.Ip
    $hostname = $matches.HostName
    # test if the entry is one of the required ones
    $exists = [bool]($required | Where-Object { $_.Ip -eq $ip -and $_.HostName -eq $hostname })
    # output an object
    [PsCustomObject]@{
        IP = $ip
        HostName = $hostname
        Exists = $exists
    }
}

# show results on screen
$result | Format-Table -AutoSize
正则表达式详细信息:

^                   Assert position at the beginning of a line (at beginning of the string or after a line break character)
\s                  Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   *                Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?<IP>              Match the regular expression below and capture its match into backreference with name “IP”
   [0-9a-f.:]       Match a single character present in the list below
                    A character in the range between “0” and “9”
                    A character in the range between “a” and “f”
                    One of the characters “.:”
      +             Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)                  
\s                  Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   +                Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(?<HostName>        Match the regular expression below and capture its match into backreference with name “HostName”
   [^\s#]           Match a single character NOT present in the list below
                    A whitespace character (spaces, tabs, line breaks, etc.)
                    The character “#”
      +             Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)                  
(?<Comment>         Match the regular expression below and capture its match into backreference with name “Comment”
   .                Match any single character that is not a line break character
      *             Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)                  
$                   Assert position at the end of a line (at the end of the string or before a line break character)
^在行首(字符串开头或换行符后)断言位置
\s匹配单个“空白字符”(空格、制表符、换行符等)
*在零次和无限次之间,尽可能多次,根据需要回馈(贪婪)
(?匹配下面的正则表达式,并将其匹配捕获到名为“IP”的反向引用中)
[0-9a-f.:]匹配下表中的单个字符
介于“0”和“9”之间的字符
介于“A”和“f”之间的字符
其中一个字符“:”
+在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
)                  
\s匹配单个“空白字符”(空格、制表符、换行符等)
+在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
(?匹配下面的正则表达式,并将其匹配捕获到名为“HostName”的反向引用中)
[^\s#]匹配下表中不存在的单个字符
空白字符(空格、制表符、换行符等)
字符“#”
+在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
)                  
(?匹配下面的正则表达式,并将其匹配捕获到名为“Comment”的backreference中
.匹配不是换行符的任何单个字符
*在零次和无限次之间,尽可能多次,根据需要回馈(贪婪)
)                  
$Assert在行尾的位置(在字符串末尾或换行符之前)

主机文件还可以在IP和主机名部分后面包含注释行或注释,前面是
#
字符。你现在的正则表达式不能解释这一点

我将创建一个包含所有必需项的查找哈希表,并使用该哈希表来查找hosts文件是否包含这些项,从而创建一个psobject数组,以实现良好的格式设置和轻松的过滤

比如:

$file    = "$env:SystemDrive\Windows\System32\Drivers\etc\hosts"
$pattern = '^\s*(?<IP>[0-9a-f.:]+)\s+(?<HostName>[^\s#]+)(?<Comment>.*)$'
# create an array of Hashtables with required entries
$required = @{Ip = '10.23.24.45'; HostName = 'foo.com'},
            @{Ip = '10.24.45.34'; HostName = 'domain.com'}

# read the current content of the hosts file, filter only lines that match the pattern
$result = Get-Content -Path $file | Where-Object { $_ -match $pattern } | ForEach-Object {
    $ip = $matches.Ip
    $hostname = $matches.HostName
    # test if the entry is one of the required ones
    $exists = [bool]($required | Where-Object { $_.Ip -eq $ip -and $_.HostName -eq $hostname })
    # output an object
    [PsCustomObject]@{
        IP = $ip
        HostName = $hostname
        Exists = $exists
    }
}

# show results on screen
$result | Format-Table -AutoSize
正则表达式详细信息:

^                   Assert position at the beginning of a line (at beginning of the string or after a line break character)
\s                  Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   *                Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?<IP>              Match the regular expression below and capture its match into backreference with name “IP”
   [0-9a-f.:]       Match a single character present in the list below
                    A character in the range between “0” and “9”
                    A character in the range between “a” and “f”
                    One of the characters “.:”
      +             Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)                  
\s                  Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   +                Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(?<HostName>        Match the regular expression below and capture its match into backreference with name “HostName”
   [^\s#]           Match a single character NOT present in the list below
                    A whitespace character (spaces, tabs, line breaks, etc.)
                    The character “#”
      +             Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)                  
(?<Comment>         Match the regular expression below and capture its match into backreference with name “Comment”
   .                Match any single character that is not a line break character
      *             Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)                  
$                   Assert position at the end of a line (at the end of the string or before a line break character)
^在行首(字符串开头或换行符后)断言位置
\s匹配单个“空白字符”(空格、制表符、换行符等)
*在零次和无限次之间,尽可能多次,根据需要回馈(贪婪)
(?匹配下面的正则表达式,并将其匹配捕获到名为“IP”的反向引用中)
[0-9a-f.:]匹配下表中的单个字符
介于“0”和“9”之间的字符
介于“A”和“f”之间的字符
其中一个字符“:”
+在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
)                  
\s匹配单个“空白字符”(空格、制表符、换行符等)
+在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
(?匹配下面的正则表达式,并将其匹配捕获到名为“HostName”的反向引用中)
[^\s#]匹配下表中不存在的单个字符
空白字符(空格、制表符、换行符等)
字符“#”
+在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
)                  
(?匹配下面的正则表达式,并将其匹配捕获到名为“Comment”的backreference中
.匹配不是换行符的任何单个字符
*在零次和无限次之间,尽可能多次,根据需要回馈(贪婪)
)                  
$Assert在行尾的位置(在字符串末尾或换行符之前)

我已经修复了代码格式。代码块周围有额外的引号。“。这些引号也在代码中吗?如果只是复制粘贴错误,请回答问题并删除这些引号。在回答问题时,请向问题添加详细信息,并解释代码如何不起作用