溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何利用powershell腳本W(wǎng)indows hosts記錄替換IP

發(fā)布時(shí)間:2021-11-03 17:51:36 來(lái)源:億速云 閱讀:292 作者:柒染 欄目:建站服務(wù)器

如何利用powershell腳本W(wǎng)indows hosts記錄替換IP,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

點(diǎn)擊(此處)折疊或打開

  1. #host 文件更換IP映射,需要手工定義主機(jī)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函數(shù)

  17. function f_switch_db{

  18. write-host "切換前數(shù)據(jù)庫(kù)IP為$v_db_ip_now"

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

  20. write-host "切換后數(shù)據(jù)庫(kù)IP為$sw_dbhost"

  21. }




  22. $title = "切換數(shù)據(jù)庫(kù)IP"

  23. $message = "你希望切換數(shù)據(jù)庫(kù)IP嗎?"

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

  25. "準(zhǔn)備切換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. 以下為運(yùn)行結(jié)果:

如何利用powershell腳本W(wǎng)indows hosts記錄替換IP

關(guān)于如何利用powershell腳本W(wǎng)indows hosts記錄替換IP問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI