[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
  1. $IP_URL = "127.0.0.1 www.bing.com"
  2. if ((Get-Content $env:windir\System32\drivers\etc\hosts | ?{ $_ -match "$($IP_URL -replace '\s+','\s+')" }) -eq $null)
  3. {
  4. "$IP_URL" | ac "$env:windir\System32\drivers\etc\hosts"
  5. echo "已添加!"
  6. }
  7. else
  8. {
  9. echo "已存在"
  10. }
复制代码

TOP

回复 3# 5i365


    你看这两个是一样的吗?
  1. $IP_URL = "127.0.0.1 www.bing.com"
  2. $IP_URL11 = "127.0.0.1   www.bing.com"
  3. $IP_URL -match $IP_URL11
  4. $IP_URL -match "$($IP_URL11 -replace '\s+','\s+')"
复制代码

TOP

回复 5# 5i365

试试第一行多加几个空格前后增加中间穿插若干个空格
是不是感觉这个替换多于了,不加也可以的

TOP

  1. $IP_URL = @(
  2.         "127.0.0.1 www.bing.com"
  3.         "127.0.0.1 www.sina.com"
  4. )
  5. $IP_URL -notmatch "$((Get-Content $env:windir\System32\drivers\etc\hosts | ?{ $_ -match "$($IP_URL -join '|')"}) -join '|')"|ac "$env:windir\System32\drivers\etc\hosts"
复制代码

TOP

  1. $IP_URL = @(
  2.         "127.0.0.1 www.bing.com"
  3.         "127.0.0.1 www.sina.com"
  4. )
  5. $a=(Get-Content "$env:windir\System32\drivers\etc\hosts" | ?{ $_ -match "$($IP_URL -join '|')"}) -join '|'
  6. if ($a -eq '')
  7. {
  8. $IP_URL | ac "$env:windir\System32\drivers\etc\hosts"
  9. echo "已添加!"
  10. }
  11. else
  12. {
  13. $IP_URL -notmatch "$a" | ac "$env:windir\System32\drivers\etc\hosts"
  14. echo "已存在"
  15. }
复制代码

TOP

回复 14# 5i365
  1. $Hosts = "$env:windir\System32\drivers\etc\hosts"
  2. $a=gc $Hosts
  3. $urls = @(
  4. "www.baidu.com"
  5. "www.bing.com"
  6. ) |
  7. foreach{
  8. if($a[-1] -ne ''){ac $hosts ''}elseif ((gc $Hosts | Select-String $_) -eq $null)
  9. {
  10. ac $hosts "127.0.0.1 $_"
  11. }
  12. }
复制代码

TOP

这么绕的吗,直接用记事本打开不好吗

TOP

返回列表