<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/post//</link>
<title><![CDATA[Powershell修改hosts文件并删除存在hosts记录行]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[运维管理]]></category>
<pubDate>Sun, 04 Dec 2022 11:00:07 +0000</pubDate> 
<guid>https://www.heckjj.com/post//</guid> 
<description>
<![CDATA[ 
	#使用前请修改域名地址和ip地址<br/><br/>#域名地址<br/>$url=&quot;heckjj.com&quot;<br/>#ip地址<br/>$ip=&quot;127.0.0.1&quot;<br/>$hosts=&quot;$env:windir&#92;System32&#92;drivers&#92;etc&#92;hosts&quot;<br/>$content=Get-Content $hosts <br/>if( ($content &#124;?&#123;$_ -imatch &quot;&#92;s$url&quot;&#125;) -eq $null)&#123;<br/> Write-Host &quot;Adding `&#039;$ip`&nbsp;&nbsp;to `&#039;$url`&#039;&quot;<br/>&quot;`n$ip $url&quot; &#124; Out-File -FilePath $hosts -Append -encoding ascii<br/> Write-Host &quot;Added success!&quot;<br/>&#125;else&#123;<br/>$content=Get-Content $hosts &#124; Where&#123;$_.Indexof(&quot;$url&quot;) -eq -1&#125;<br/> Write-Host &quot;update `&#039;$ip`&nbsp;&nbsp;to `&#039;$url`&#039;&quot;<br/> $content+=&quot;$ip $url&quot; <br/> $content &#124; Set-Content&nbsp;&nbsp;$hosts -Force<br/> Write-Host &quot;updated success!&quot;<br/>&#125;<br/><br/>最终效果，第一次是增加，后面一直是替换原来的host记录，实际上是生写了，最开始的思路是找到域名所在的行删除该行，但发现行不通，根本没有获取域名所在行的方法，最后想着是通过获取文件内容时先过滤掉当前域名的记录，也就是只读取不包含该域名host记录的行，最后再追加就可以了。
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post//#blogcomment</link>
<title><![CDATA[[评论] Powershell修改hosts文件并删除存在hosts记录行]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>