[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
回复 15# idwma


    还是不行, 多了两个空行

TOP

回复 15# idwma


    搞定了, 把if移到上面去就行了
  1. $Hosts = "$env:windir\System32\drivers\etc\hosts"
  2. $a = gc $Hosts
  3. if ($a[-1] -ne '') { ac $hosts '' }
  4. $urls = @(
  5. "www.baidu.com"
  6. "www.bing.com"
  7. ) |
  8. foreach{
  9. if ((gc $Hosts | Select-String $_) -eq $null)
  10. {
  11. ac $hosts "127.0.0.1 $_"
  12. }
  13. }
复制代码

TOP

本帖最后由 5i365 于 2022-2-28 20:19 编辑

回复 15# idwma

分享老外写的, 用PS对话框的形式来改HOST, 里面有控件的位置, 尺寸之类的, 感觉应该可以去掉, 应该可以精简, 以前看到过有类似的, 才几行代码就行!
  1. Add-Type -AssemblyName System.Windows.Forms
  2. Add-Type -AssemblyName System.Drawing
  3. $hostsfilelocation = "$env:windir\System32\drivers\etc\hosts"
  4. $readhostsfile = Get-Content $hostsfilelocation
  5. $form = New-Object System.Windows.Forms.Form
  6. $form.Text = 'Update HOSTS File'
  7. $form.Size = New-Object System.Drawing.Size(300, 200)
  8. $form.StartPosition = 'CenterScreen'
  9. $AddHosts = New-Object System.Windows.Forms.Button
  10. $AddHosts.Location = New-Object System.Drawing.Point(55, 120)
  11. $AddHosts.Size = New-Object System.Drawing.Size(90, 25)
  12. $AddHosts.Text = 'Add Record'
  13. $AddHosts.DialogResult = [System.Windows.Forms.DialogResult]::OK
  14. $form.AcceptButton = $AddHosts
  15. $form.Controls.Add($AddHosts)
  16. $CancelButton = New-Object System.Windows.Forms.Button
  17. $CancelButton.Location = New-Object System.Drawing.Point(170, 120)
  18. $CancelButton.Size = New-Object System.Drawing.Size(75, 25)
  19. $CancelButton.Text = 'Cancel'
  20. $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
  21. $form.CancelButton = $CancelButton
  22. $form.Controls.Add($CancelButton)
  23. $Hostslabel = New-Object System.Windows.Forms.Label
  24. $Hostslabel.Location = New-Object System.Drawing.Point(10, 20)
  25. $Hostslabel.Size = New-Object System.Drawing.Size(280, 20)
  26. $Hostslabel.Text = 'Enter New HOSTNAME/FQDN:'
  27. $form.Controls.Add($Hostslabel)
  28. $HoststextBox = New-Object System.Windows.Forms.TextBox
  29. $HoststextBox.Location = New-Object System.Drawing.Point(10, 40)
  30. $HoststextBox.Size = New-Object System.Drawing.Size(260, 20)
  31. $form.Controls.Add($HoststextBox)
  32. $IPlabel = New-Object System.Windows.Forms.Label
  33. $IPlabel.Location = New-Object System.Drawing.Point(10, 60)
  34. $IPlabel.Size = New-Object System.Drawing.Size(280, 20)
  35. $IPlabel.Text = 'Enter IP:'
  36. $form.Controls.Add($IPlabel)
  37. $IPtextBox = New-Object System.Windows.Forms.TextBox
  38. $IPtextBox.Location = New-Object System.Drawing.Point(10, 80)
  39. $IPtextBox.Size = New-Object System.Drawing.Size(260, 20)
  40. $form.Controls.Add($IPtextBox)
  41. $form.Topmost = $true
  42. $form.Add_Shown({ ($HoststextBox, $IPtextbox).Select() })
  43. $result = $form.ShowDialog()
  44. if ($result -eq [System.Windows.Forms.DialogResult]::OK)
  45. {
  46. $inputhosts = $HoststextBox.Text
  47. $inputip = $IPtextBox.Text
  48. $newrecord = "$inputip $inputhosts"
  49. Add-Content -Path $hostsfilelocation -Value $newrecord
  50. }
复制代码

TOP

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

TOP

回复 19# idwma


  嗯, 偶尔改手动更方便些, 改多台电脑时, 用批处理改不用到电脑旁边, 不用远程就改, 非常方便

TOP

返回列表