powershell自动添加静态IP
Posted hanshanxiaoheshang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell自动添加静态IP相关的知识,希望对你有一定的参考价值。
参考:
https://ss64.com/nt/netsh.html
https://www.520mwx.com/view/11790
http://www.voidcn.com/article/p-rnrwftqs-bro.html
https://thinkpowershell.com/change-dns-servers-for-computers-with-static-ip-addresses/
<# 交互参考其他大佬 Intro: This function will display a form to communicate with the user. Input: -FormText -ButtonText Example: MakeForm -FormText "ForInput" -ButtonText "Submit" Use: To make the PowerShell program‘s interactivity better. #> function MakeForm param($FormText,$ButtonText) $null = [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $form = New-Object Windows.Forms.Form $form.size = New-Object Drawing.Size -Arg 400,80 $form.StartPosition = "CenterScreen" $form.Text = $FormText.toString() $textBox = New-Object Windows.Forms.TextBox $textBox.Dock = "fill" $form.Controls.Add($textBox) $button = New-Object Windows.Forms.Button $button.Text = $ButtonText $button.Dock = "Bottom" $button.add_Click( $global:resultText = $textBox.Text;$form.Close()) $form.Controls.Add($button) [Void]$form.ShowDialog() MakeForm -FormText "What‘s your name" -ButtonText "Submit" echo $resultText $wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = ‘true‘" $wmi.EnableStatic("$resultText", "255.255.255.0") $a = $resultText.split(".",4)[0] $b = $resultText.split(".",4)[1] $c = $resultText.split(".",4)[2] $gateway = "$a.$b.$c.1" $wmi.SetGateways("$gateway") #$Adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object $_.DHCPEnabled -ne ‘True‘ -and $_.DNSServerSearchOrder -ne $null $Adapters = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = ‘true‘" $Adapters | ForEach-Object $_.DNSServerSearchOrder $NewDnsServerSearchOrder = "DNS1","DNS2" $Adapters = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = ‘true‘" $Adapters | ForEach-Object $_.SetDNSServerSearchOrder($NewDnsServerSearchOrder) | Out-Null $Adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object $_.DHCPEnabled -ne ‘True‘ -and $_.DNSServerSearchOrder -ne $null $Adapters | ForEach-Object $_.DNSServerSearchOrder #Get-DnsClientServerAddress -AddressFamily IPv4 | #Out-GridView -PassThru | #foreach # Set-DnsClientServerAddress -InterfaceIndex $_.InterfaceIndex -Addresses ‘DNS1‘,‘DNS2‘ # #netsh interface ipv4 add dns "Local Area Connection *" DNS1 #netsh interface ipv4 add dns "Local Area Connection *" DNS2 index=2 ipconfig /flushdns netsh interface ip show config sleep 3 ping www.baidu.com
以上是关于powershell自动添加静态IP的主要内容,如果未能解决你的问题,请参考以下文章
Powershell 开机自动加载Snapin 和module