使用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” 新增一 ...
随机推荐
- hadoop day 5
1.Zookeeper Zookeeper的安装和配置(集群模式) 1)在conf目录下创建一个配置文件zoo.cfg, tickTime=2000——心跳检测的时间间隔(ms) dataDir=/U ...
- latex之注释快捷键
注释快捷键 ctrl+T:注释掉选中区域 ctrl_U:解除选中区域的注释
- CPU瓶颈分析工具
性能指标: 一.CPU利用率. 1.用户CPU使用率:用户态CPU使用率(user)和低优先级用户态CPU使用率(nice). 2.系统CPU使用率:说明内核比较忙. 3.等待I/O的CPU使用率(i ...
- hdu5007 Post Robot AC自动机
DT is a big fan of digital products. He writes posts about technological products almost everyday in ...
- 全志A33开发板Linux内核定时器编程
开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 Linux 内核定时器是内核 ...
- Yii2中多表关联查询(hasOne、hasMany、join、joinwith)
表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer (id customer_name) 订单表Order (id order_name customer_id ...
- Fabric的settings用法
http://fabric-chs.readthedocs.io/zh_CN/chs/api/core/context_managers.html?highlight=with%20settings# ...
- MySQL通过分组计算百分比
公司在做柯米克的分析报告,需要我这边把汽车之家柯米克论坛的评论数据和评论用户所在地的数据获取,通过爬虫的方式很快的解决了数据的问题,但是需要我提取下各省评论人数的比例,所以在数据库里面直接计算了相关的 ...
- edit this cookie chrome插件 (HttpAnalyzerStdV3 获取Cookie 后,再用edit this cookie添加cookie)
插件下载地址及安装方法:http://www.bkill.com/download/154494.html 博客提供下载地址:https://files.cnblogs.com/files/Fooo/ ...
- 第一个Eureka程序,Eureka Client的自启动原理和简要过程
https://blog.csdn.net/u011531425/article/details/81675289 在之前的Spring Cloud Config的基础上,搭建简单的Eureka Se ...