温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何利用powershell脚本Windows hosts记录替换IP

发布时间:2021-11-03 17:51:36 来源:亿速云 阅读:273 作者:柒染 栏目:建站服务器

如何利用powershell脚本Windows hosts记录替换IP,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

点击(此处)折叠或打开

  1. #host 文件更换IP映射,需要手工定义主机IP

  2. $db_host1 = "192.168.1.1"

  3. $db_host2 = "192.168.1.2"

  4. $host_file = "C:\Windows\System32\drivers\etc\hosts"

  5. $db_name = "db.test.com"




  6. $v_db_name = Select-String -Path $host_file -Pattern $db_name

  7. $v_db = $v_db_name -split "\s+"

  8. $v_db_ip = $v_db -split ":"

  9. $v_db_ip_now = $v_db_ip[3]



  10. if ( $v_db_ip_now -like $db_host1){

  11. $sw_dbhost = $db_host2

  12. }

  13. else {

  14. $sw_dbhost = $db_host1

  15. }


  16. #切换IP函数

  17. function f_switch_db{

  18. write-host "切换前数据库IP为$v_db_ip_now"

  19. (Get-Content $host_file) -replace $v_db_ip_now,$sw_dbhost  | Set-Content $host_file

  20. write-host "切换后数据库IP为$sw_dbhost"

  21. }




  22. $title = "切换数据库IP"

  23. $message = "你希望切换数据库IP吗?"

  24. $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `

  25. "准备切换IP为$sw_dbhost"

  26. $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `

  27. "不切换IP."

  28. $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

  29. $result = $host.ui.PromptForChoice($title, $message, $options, 0)

  30. switch ($result)

  31. {

  32. 0 {f_switch_db}

  33. 1 {"You selected No."}

  34. }


  35. 以下为运行结果:

如何利用powershell脚本Windows hosts记录替换IP

关于如何利用powershell脚本Windows hosts记录替换IP问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI