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/

在Init_file文件中一共有5个文件,双击Init_exec.bat文件即可选择加IP或者加域名等;

执行步骤

1、Init_exec.bat,此脚本是调用Called.ps1脚本,然后有Called.ps1脚本调用AddIp.ps1和AddDomain.ps1脚本,而AddIp.ps1调用的是ChangeName.vbs脚本

2、添加IP和主机名

输入IP

输入主机名

输入后可以设定一定时间重启服务器

   

 

2、重启后继续执行Init_exec.bat脚本,添加域、OPS组和关闭防火墙

       

 

以上就配置好了

 

 

Init_exec.bat脚本

setlocal enabledelayedexpansion
@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit


powershell -File "Called.ps1"

 

Called.ps1脚本

echo "请输入选择的数字:"
echo " "
echo "1: 添加IP和主机名;"
echo "2:添加域、OPS组、关闭防火墙;"
echo " "
echo " "


<#
    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 "请输入数字:" -ButtonText "Submit"
#echo $resultText


if( $resultText -eq 1 )
{
    powershell -File "AddIp.ps1"
}
elseif( $resultText -eq 2)
{
   powershell -File "AddDomain.ps1"
}
else
{
   echo "请输入数字!!!"
   echo "请输入数字!!!"
   echo "请输入数字!!!"
   sleep 3 
}
  

 

AddIp.ps1脚本

<#
    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 the IP" -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")

$dns = "DNS1", "DNS2"

$wmi.SetDNSSERVERSearchOrder($dns)


ipconfig /flushdns

#$CurrentyDir = Split-Path -Parent $MyInvocation.MyCommand.Definition

C:\opstools\Init_file\ChangeName.vbs

sleep 7

netsh interface ip show config

sleep 3

ping www.baidu.com

netsh interface ip show config

sleep 25

ping www.baidu.com

 

ChangeName.vbs脚本

Dim reval
Set objnet = CreateObject ("WScript.Network")
Set R = CreateObject("WScript.Shell")
reval = InputBox ("Your ComputerName is:" & objnet.ComputerName,"Input Your new ComputerName")
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")

For Each objComputer in colComputers
    errReturn = ObjComputer.Rename (reval)
    If reval <> "" Then 
                return=MsgBox ("Reboot Computer?",vbokcancel+vbexclamation,"tips!")
                If return=vbok Then
                        R.run("Shutdown.exe -r -t 10")
                End if
    End If 
Next

 

AddDomain.ps1脚本

echo "++++++添加域++++++"
$domain = "域名" 
$credential = New-Object System.Management.Automation.PSCredential($username,$password) 
Add-Computer -DomainName $domain -Credential $credential
echo "结果为: $?"
echo " "
echo " "
sleep 2


echo "++++++添加ops组++++++"
net localgroup Administrators 组名 /add
echo "结果为: $?"
echo " "
echo " "
sleep 5


#只关闭域防火墙
#netsh firewall set opmode mode=disable
echo "++++++关闭所有防火墙++++++"
netsh advfirewall set allprofiles state off
echo "结果为: $?"  
echo " "
echo " "
sleep 5


echo "++++++查看主机名++++++"
hostname
echo " "
echo " "
sleep 3


echo "++++++查看主机IP++++++"
netsh interface ip show config | select-string "10."
echo " "
echo " "
sleep 3


echo "++++++查看域名++++++"
systeminfo | select-string "Domain"
echo " "
echo " "
sleep 3


echo "++++++查看组++++++"
net localgroup Administrators | select-string "CORP|PROD"
echo " "
echo " "
sleep 5


echo "++++++查看防火墙++++++"
netsh advfirewall show allprofiles | select-string "Profile|State"
echo "40秒钟后将重启!!!"
echo " "
echo " "
echo " "
sleep 40
echo " "
echo " "


shutdown.exe -r -t 40

 

posted @ 2019-08-26 23:45  Study~Column  阅读(720)  评论(0编辑  收藏  举报