您好,登錄后才能下訂單哦!
如何利用powershell腳本W(wǎng)indows hosts記錄替換IP,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
點(diǎn)擊(此處)折疊或打開
#host 文件更換IP映射,需要手工定義主機(jī)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
}
#切換IP函數(shù)
function f_switch_db{
write-host "切換前數(shù)據(jù)庫(kù)IP為$v_db_ip_now"
(Get-Content $host_file) -replace $v_db_ip_now,$sw_dbhost | Set-Content $host_file
write-host "切換后數(shù)據(jù)庫(kù)IP為$sw_dbhost"
}
$title = "切換數(shù)據(jù)庫(kù)IP"
$message = "你希望切換數(shù)據(jù)庫(kù)IP嗎?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"準(zhǔn)備切換IP為$sw_dbhost"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"不切換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."}
}
以下為運(yùn)行結(jié)果:
關(guān)于如何利用powershell腳本W(wǎng)indows hosts記錄替換IP問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(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)容。