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
% mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",)(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 ,
$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 )
{
powershell -File "AddIp.ps1"
}
elseif( $resultText -eq )
{
powershell -File "AddDomain.ps1"
}
else
{
echo "请输入数字!!!"
echo "请输入数字!!!"
echo "请输入数字!!!"
sleep
}
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 ,
$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(".",)[]
$b = $resultText.split(".",)[]
$c = $resultText.split(".",)[]
$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 netsh interface ip show config sleep ping www.baidu.com netsh interface ip show config sleep 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 echo "++++++添加ops组++++++"
net localgroup Administrators 组名 /add
echo "结果为: $?"
echo " "
echo " "
sleep #只关闭域防火墙
#netsh firewall set opmode mode=disable
echo "++++++关闭所有防火墙++++++"
netsh advfirewall set allprofiles state off
echo "结果为: $?"
echo " "
echo " "
sleep echo "++++++查看主机名++++++"
hostname
echo " "
echo " "
sleep echo "++++++查看主机IP++++++"
netsh interface ip show config | select-string "10."
echo " "
echo " "
sleep echo "++++++查看域名++++++"
systeminfo | select-string "Domain"
echo " "
echo " "
sleep echo "++++++查看组++++++"
net localgroup Administrators | select-string "CORP|PROD"
echo " "
echo " "
sleep echo "++++++查看防火墙++++++"
netsh advfirewall show allprofiles | select-string "Profile|State"
echo "40秒钟后将重启!!!"
echo " "
echo " "
echo " "
sleep
echo " "
echo " " shutdown.exe -r -t
powershell自动添加静态IP的更多相关文章
- Linux:自动获取静态IP地址,清空iptable,修改selinux脚本
自动获取静态IP地址,清空iptable,修改selinux脚本 环境:VMware 平台:centos6.8全新 功能: 1)应用ifconfig -a,route -n,cat /etc/reso ...
- 从零开始学安全(四)●Vmware CentOS 7 添加静态ip联网
一.虚拟网络编辑器配置 1.VMnet8设置(不需要改动) 2.NAT设置(不需要改动) 3.DHCP设置(CentOS IP地址段设置,不需要改动) 二.虚拟机设置(网络适配器选择NAT模式) 三. ...
- 家中路由添加静态IP映射(二)
家中的路由器普遍通过DHCP向连接到此路由器的客户主机提供IP配置方式,但是一般都是动态IP: 假设需要静态IP配置到个人电脑,就要在路由器上进行配置IP和MAC地址映射关系.如上篇截图.路由器端配置 ...
- 家中路由添加静态IP映射(一)
登录路由器配置页面,选择更多功能来添加IP和MAC地址映射关系
- bat文件设置ip自动和静态ip切换
下载地址:https://i.cnblogs.com/Files.aspx win10系统: @echo off cd /d %~dp0 %1 start "" mshta vbs ...
- linux 添加静态ip dns
/etc/network 下:interfaces auto loiface lo inet loopbackauto eth0iface eth0 inet staticaddress 192.16 ...
- 在VMware中使用Nat方式设置静态IP, 宿主机可以 ssh
坑很多: 麻痹, 主要还是要先 防火墙关掉,永久关掉. seliux 也永久关掉. 临时关闭防火墙:systemctl stop firewalld 开机不启动: systemctl di ...
- 自动拒绝恶意IP远程登录Linux服务器脚本
当我们已经配置了iptables防火墙,我们允许22端口对外网所有人访问,当然这也是为了方便,我们在任何地方都连接上,没有做VPN,也没有做ssh密钥验证,但是我们的密码设置得非常复杂,大小写.特殊符 ...
- deepin 2014 静态IP无法保存,临时方法
打开终端: #临时添加静态IP ifconfig eth0 [ip] netmask [掩码] #添加默认网关 route add default gw [网关] #添加DNS vim /etc/re ...
随机推荐
- Django框架起步
一.环境安装 二.创建项目 三.项目目录 四.创建项目应用 五.应用目录 六.第一个响应 七.第一个模板页面 八.第一个重定向 九.url应用移植 十.多应用相同模板页面冲突 十一.静态资源的配置 十 ...
- web开发:javascript之dom与bom
一.节点认知 二.文档结构 三.文档节点操作 四.事件target 五.BOM操作 一.节点认知 - dom与dom属性 <!DOCTYPE html> <html> < ...
- 01_Redis简述
一:关系型数据库和非关系型数据库的区别: 1:关系型数据库(SQL):数据和数据之间,表和字段之间,表和表之间是存在关系的: 优点:数据之间有关系,进行数据的增删改查时非常方便的:关系型数据库有事务操 ...
- Oralce问题之ORA-12560:TNS协议适配器错误
在Windows系统中,通过CMD命令打开命令窗口,通过命令:sqlplus / as sysdba回车后提示 协议适配器错误 可能原因: (1).Oralce数据库监听服务没启动起来 通过开始-&g ...
- H2数据库启动提示8082端口被占用
The Web Console server could not be started. Possible cause: another server is already running at ht ...
- 少有人知的 GitHub 使用技巧
GitHub 大家常上吧?可是使用 GitHub 的各种小窍门你就不一定知道了.本文将各种使用 GitHub 的小窍门分享给大家. diff时忽略空格 有些修改只是增减了空格,在URL中添加?w=1就 ...
- CentOS7 内核优化 修改参数
一:内核简介 内核是操作系统最基本的部分.它是为众多应用程序提供对计算机硬件的安全访问的一部分软件,这种访问是有限的,并且内核决定一个程序在什么时候对某部分硬件操作多长时间. 内核的分类可分为单内核和 ...
- Spring入门篇——第1章 概述
第1章 概述 本章对课程的情况进行介绍,并介绍框架和Spring概况. 1-1 Spring入门课程简介 1-2 Spring概况 1-3 Spring框架
- BZOJ 2244: [SDOI2011]拦截导弹 (CDQ分治 三维偏序 DP)
题意 略- 分析 就是求最长不上升子序列,坐标取一下反就是求最长不下降子序列,比较大小是二维(h,v)(h,v)(h,v)的比较.我们不看概率,先看第一问怎么求最长不降子序列.设f[i]f[i]f[i ...
- BZOJ 3772: 精神污染 (dfs序+树状数组)
跟 BZOJ 4009: [HNOI2015]接水果一样- CODE #include <set> #include <queue> #include <cctype&g ...