码迷,mamicode.com
首页 > 系统相关 > 详细

powershell自动添加静态IP

时间:2019-08-27 00:54:28      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:nss   lte   dial   void   nbsp   pre   arc   The   you   

 参考:

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 programs 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

标签:nss   lte   dial   void   nbsp   pre   arc   The   you   

原文地址:https://www.cnblogs.com/hanshanxiaoheshang/p/11415785.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!