使用Powershell实现计算机名称及IP地址修改
我的第一篇博客分享,写这个代码的用途是在公司Ghost完系统之后去修改本地计算机名称及IP 地址,用Powershell实现。
1. 代码第一部分,检查Powershell是否已管理员权限执行,如果不是的话,强制以管理员权限开启一个powershell窗口.
#region Key code: force to run with administrator rights
$currentWi = [Security.Principal.WindowsIdentity]::GetCurrent()
$currentWp = [Security.Principal.WindowsPrincipal]$currentWi if( -not $currentWp.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
$boundPara = ($MyInvocation.BoundParameters.Keys | foreach{
'-{0} {1}' -f $_ ,$MyInvocation.BoundParameters[$_]} ) -join ' '
$currentFile=(Resolve-Path $myInvocation.MyCommand.Source).Path $fullPara = $boundPara + ' ' + $args -join ' '
Start-Process "$psHome\powershell.exe" -ArgumentList "$currentFile $fullPara" -verb runas
return
}
#endregion
2. 第二部分,调用.NET Framework创建一个窗口输入新的computer name,之后重命名
#access .NET Framework
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
# config Computer Name
$computer=[Microsoft.VisualBasic.Interaction]::InputBox("Please enter a computer name. Standard names can contain letters (a-z, a-z), Numbers (0-9), and hyphen (-).", "Computer Name", "$env:computername")
Rename-Computer -NewName $computer
# end of configure Computer Name
3. 第三部分, 因为ghost后的系统网络名称不是原来的Local Area Connection,而会变成Local Area Connection #2类似这样后边跟数字的名称,所以我们需要调用gwmi组件并且正则表达式去匹配并选定网络名称包含Local Area Connection 的adapter,然后再去set ip地址.
# Configure the local network IP address
$IP=[Microsoft.VisualBasic.Interaction]::InputBox("Please enter the IP address, such as 192.168.1.1", "IP Address",'192.168.1.2')
$parttern="^Local Area Connection"
$NICs=gwmi win32_networkadapter `
| Where {$_.NetConnectionID -match $parttern}
foreach($NIC in $NICs) {
$N=$NIC.NetConnectionID
netsh interface ip set address name="$N" source=static addr=$IP
}
if($Error.Count -eq 0) {
echo "Set OK!!!!"
}
Write-Host 'Press any key to exit ...'
Read-Host
# end of configure the local network IP address
4. 最后附上代码实现截图:

使用Powershell实现计算机名称及IP地址修改的更多相关文章
- 计算机名称和IP地址
获取本地IP地址 得到远程机IP地址与描述 若仅仅是查看IP地址
- 获取本地计算机名称和Ip地址
using System.Net; Dns.GetHostName();//获取本地计算机主机名 IPAddress[] IP = Dns.GetHostAddresses(Dns.GetHostNa ...
- 获取硬件信息的delphi源码CPUID、操作系统、Mac物理地址、计算机名称、IP地址、用户名
{-----------------------------------------------------------------------------作者:sushengmiyan 2013.0 ...
- IP地址、计算机名称、MAC地址如何获取
以下的操作都在“命令提示窗口”中操作. 已知IP,如何获得计算机名称 方法(1): 使用ping -i ip地址 例如已知地址为192.168.1.168. 那么使用ping -i 192.168.1 ...
- 通过jquery 获取用户当前所在的城市名称和IP地址
下面这段JS代码是通过jquery 结合新浪IP地址库和QQip地址库接口获取用户当前所在的城市(省份)名称. 用户当前IP地址等数据.其中当前IP是通过 QQip地址库接口获取,其他数据都是通过 新 ...
- 使用Python获取计算机名,ip地址,mac地址等等
获取计算机名 # 获取计算机名,常用的方法有三种 import os import socket # method one name = socket.gethostname() print(name ...
- 获取网卡名称及其IP地址的方法
代码 # -*- coding: utf-8 -*- import psutil #获取网卡名称和其ip地址,不包括回环 def get_netcard(): netcard_info = [] in ...
- 【Azure Developer】在Azure Resource Graph Explorer中查看当前订阅下的所有资源信息列表并导出(如VM的名称,IP地址内网/公网,OS,区域等)
问题描述 通过Azure的Resource Graph Explorer(https://portal.azure.cn/#blade/HubsExtension/ArgQueryBlade),可以查 ...
- ubuntu设置IP地址&修改vi模式键盘上下键错位
解决ubuntu上面使用vi 出现方向键错乱的情况 编辑/etc/vim/vimrc.tiny 使用root权限操作:将“set compatible”改成“set nocompatible” 新增一 ...
随机推荐
- 自动化测试-13.selenium执行JS处理滚动条
前言 selenium并不是万能的,有时候页面上操作无法实现的,这时候就需要借助JS来完成了. 常见场景: 当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的. 这时候 ...
- QT | QT MSVC 2015 + VS 2015开发环境配置及GIT设置
1.下载: 所有Qt版本的下载地址: http://download.qt.io/archive/qt/ 实际使用了http://download.qt.io/archive/qt/5.7/5.7.1 ...
- linu下未编译的mysql安装包
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21.tar.gz
- day01计算机基础
今日内容 1.计算机初步认识 1.计算机认识 1. 计算机基础 1.1硬件:cpu/内存/硬盘/主板/网卡 1.2操作系统 linux:免费开源 windows mac 1.3解释器/编译器 补充:编 ...
- list基本代码
#include<iostream> #include<list> //STL之list的基本用法 using namespace std; void outputList(l ...
- 批量将某一目录下的.py文件改为.txt格式文件
#!/usr/env/python#-*- coding:utf-8 -*-#批量将某一目录下的.py文件改为.txt格式文件import os,os.pathfile_list = os.listd ...
- Python开发 基礎知識 3.類別&方法 (bool & str) (未完待續)
類別 可使用type()查看 內建 [ 布爾:bool (Boolen) 字串:str (String) 數字:int (Integer) 小數:float 列表:list 元祖:tuple 字典:d ...
- linux服务器架设--学习笔记
PS: Centos是属于红帽子的操作系统
- PythonStudy——字典 Dictionary
# 容器(集合):存放多个值的变量# 单列容器(系统中的单列容器很多):list | tuple# 双列容器(map):只有dict,存放数据 成对出现,dict存放数据采用 key-value键值对 ...
- kvm创建新虚拟机
安装图形化管理界面yum install virt-manager -y 安装好之后 新建虚拟机,我使用的方法是使用ISO镜像文件安装 选择镜像 设置内存 如此,一步一步走下去即可,不再截图 创建好之 ...