小编Oli*_* Z.的帖子

正则表达式 IF 条件

我正在寻找一种方法来过滤我的主机文件中的新 IP 地址。我创建了一个脚本,每次当我使用来自矩阵企业管理器的数据调用它时都会更新我的主机文件。它工作正常。但是我必须找到一个只允许更新 10.XX.XX.XX 或 172.XX.XX.XX 地址的解决方案。

Param(
  $newHost = $args[0],
  $newIP = $args[1]
)

$SourceFile = "hosts"
$Match = "$newHost"

(Get-Content $SourceFile) | % {if ($_ -notmatch $Match) {$_}} | Set-Content $SourceFile

Start-Sleep -Seconds 1

$tab = [char]9
$enter = $newIP + $tab + $newHost

if ($newIP XXXX)  #--> here should be a regex if condition... no clue how it works..

$enter | Add-Content -Path hosts
Run Code Online (Sandbox Code Playgroud)

regex powershell

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

powershell ×1

regex ×1