Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to replace IP with powershell script Windows hosts record

Shulou Source: shulou.com Published: 2022-06-01 03:41:24 10月04日 Update

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.

Tags: Switch data database question script more help answer easy easy host content function boy buddy manual file method knowledge article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Docker MySQL Shulou Information macOS