Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to replace IP with powershell script Windows hosts record

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to use powershell script Windows hosts records to replace IP, for this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Click (here) to collapse or open

# replacement of IP mapping in host file requires manual definition of host IP

$db_host1 = "192.168.1.1"

$db_host2 = "192.168.1.2"

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

$db_name = "db.test.com"

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

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

$v_db_ip = $v_db-split ":"

$v_db_ip_now = $v_db_ip [3]

If ($v_db_ip_now-like $db_host1) {

$sw_dbhost = $db_host2

}

Else {

$sw_dbhost = $db_host1

}

# switch IP function

Function f_switch_db {

Write-host "Database IP is $v_db_ip_now before switching"

(Get-Content $host_file)-replace $vested dbObjective dbhost | Set-Content $host_file

Write-host "Database IP is $sw_dbhost after switching"

}

$title = "switch database IP"

$message = "do you want to switch database IP?"

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

"ready to switch IP to $sw_dbhost"

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

"do not switch IP."

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

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

Switch ($result)

{

0 {f_switch_db}

1 {"You selected No."}

}

The following is the result of the run:

This is the answer to the question about how to use the powershell script Windows hosts record to replace IP. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report