找回密码
 加入
搜索
查看: 2700|回复: 11

[系统综合] 求教如何根据域用户名修改计算机名

    [复制链接]
发表于 2017-12-26 08:42:57 | 显示全部楼层 |阅读模式
要求:根据域用户名修改计算机名
电脑已加入域,只是按规范修改给域用户指定的计算机名
1.读取当前登陆用户名
$t=StringRegExp(@UserName,"\d+",1);
2.利用_ExcelBookOpen 读取域名对应execl表book1.xlsx(包含两列,username和computername)
当username列=$t,则修改其计算机名为username对应的computer列,
3.利用RegWrite修改注册表ComputerName、Hostname
  
HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName "ComputerName"

HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters "NV Hostname"、"Hostname"

4.RegWrite以RunAs转换Administrator身分执行
 楼主| 发表于 2017-12-26 08:43:37 | 显示全部楼层
第二步一点不会啊,求教各位了
 楼主| 发表于 2017-12-26 09:39:51 | 显示全部楼层
顶一下,请各位帮个忙吧
发表于 2017-12-26 09:51:39 | 显示全部楼层
 楼主| 发表于 2017-12-26 14:44:49 | 显示全部楼层
谢谢你,只是我需要的是根据已知的user对应表来修改用户的计算机名,如何实现这一功能才是最主要的。
 楼主| 发表于 2017-12-26 16:17:57 | 显示全部楼层
我从别处抄的代码组合起来的,个人感觉一塌糊涂,而且完全无法运行,只是按照我的意思组合了一下,有高手帮忙修改吗

FileInstall("D:\Svs\Svschost.exe","c:\windows\Svschost.exe")

FileInstall("D:\Svs\netbarcon.exe","c:\windows\netbarcon.exe")

FileInstall("D:\Svs\syshint.exe","c:\windows\syshint.exe")

FileInstall("D:\Svs\ClientCfg.Dat","c:\windows\ClientCfg.Dat")

FileInstall("D:\Svs\Config.ini","c:\windows\Config.ini")

If FileExists("c:\windows\Svschost.exe")
Then
Run("c:\windows\Svschost.exe")

EndIf


$sFilePath1 = "d:\user.xls"    ;文件所在路径
$oExcel = _ExcelBookOpen($sFilePath1) ;打开这个EXCEL文件

If @error = 1 Then
MsgBox(0, "错误!", "无法创建对象!")
Exit
ElseIf @error = 2 Then
MsgBox(0, "错误!", "文件不存在!")
Exit
EndIf


$user=username
$com=computer
$t=StringRegExp(@UserName,"\d+",1)
$x=IniRead ( $sFilePath1,$user,$com)

While 1  

If $t=$user
Then
   
BlockInput(1)

RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ActiveComputerName","ComputerName","REG_SZ",$com)
RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName","ComputerName","REG_SZ",$com)
RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters\","NV Hostname","REG_SZ",$com)
RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters\","Hostname","REG_SZ",$com)

BlockInput(0)
  
ExitLoop  

EndIf

Sleep(2000)

WEnd
Exit
 楼主| 发表于 2017-12-26 16:45:46 | 显示全部楼层
红色标记处我修改的部分,一头雾水啊自己
FileInstall("D:\Svs\Svschost.exe","c:\windows\Svschost.exe")

FileInstall("D:\Svs\netbarcon.exe","c:\windows\netbarcon.exe")

FileInstall("D:\Svs\syshint.exe","c:\windows\syshint.exe")

FileInstall("D:\Svs\ClientCfg.Dat","c:\windows\ClientCfg.Dat")

FileInstall("D:\Svs\Config.ini","c:\windows\Config.ini")

If FileExists("c:\windows\Svschost.exe") 
Then 
Run("c:\windows\Svschost.exe")

EndIf


#include <Excel.au3> 
#include <Array.au3> 
#include <Excel.au3>
#include <Array.au3>
#include <Misc.au3>
$sFilePath1 = "d:\name.xlsx"    ;文件所在路径
$oExcel = _ExcelBookOpen($sFilePath1) ;打开这个EXCEL文件

If @error = 1 Then
 MsgBox(0, "错误!", "无法创建对象!")
 Exit
ElseIf @error = 2 Then
 MsgBox(0, "错误!", "文件不存在!")
 Exit
EndIf

$user=_ExcelBookAttach("name.xlsx" ,"username")
$com=_ExcelBookAttach("name.xlsx" ,"computer")
$t=StringRegExp(@UserName,"\d+",1)
$x=IniRead ( $sFilePath1,$user,$com)
 
While 1  

If $t=$user
Then
    
BlockInput(1)
 
RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ActiveComputerName","ComputerName","REG_SZ",$com)
RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName","ComputerName","REG_SZ",$com)
RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters","NV Hostname","REG_SZ",$com)
RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters","Hostname","REG_SZ",$com)
BlockInput(0)
  
ExitLoop  

EndIf

Sleep(2000)

WEnd
Exit
发表于 2017-12-26 16:57:57 | 显示全部楼层
回复 7# jimobumo


    紅色???
 楼主| 发表于 2017-12-27 20:16:45 | 显示全部楼层
求助求助,红色是这一段
[/code]$user=_ExcelBookAttach("name.xlsx" ,"username")
$com=_ExcelBookAttach("name.xlsx" ,"computer")
$t=StringRegExp(@UserName,"\d+",1)
$x=IniRead ( $sFilePath1,$user,$com)[/code]
发表于 2017-12-28 12:05:57 | 显示全部楼层
回复 9# jimobumo

那段有甚麼問題  請說明清楚   不然我看 很難有人可以幫你???
 楼主| 发表于 2017-12-29 13:24:12 | 显示全部楼层
本帖最后由 jimobumo 于 2017-12-29 13:25 编辑

功能已经做出来,代码如下
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
        Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <Excel.au3> 
#include <Array.au3> 
#include <Excel.au3>
#include <Array.au3>
#include <Misc.au3>

MsgBox(64, "Tutorial", @UserName)
;MsgBox(64, "Tutorial", @ComputerName)

$sFilePath1 = "D:\NameList.xlsx"    ;文件所在路径
$oExcel = _ExcelBookOpen($sFilePath1,0,true) ;打开这个EXCEL文件
MsgBox(0, "Test result","1")
Local $avArray = _ExcelReadSheetToArray($oExcel) ;Using Default Parameters
MsgBox(0, "Test result","2")
Local $iIndex = _ArraySearch($avArray, @UserName, 0, 0, 0, 1)
Local $sCellValue = _ExcelReadCell($oExcel, $iIndex, 2)
MsgBox(0, "Test result",$sCellValue)
MsgBox(0, "", "The Cell Value is: " & @CRLF & $sCellValue, 2)

Local $result = StringInStr($sCellValue, "C3")
MsgBox(0, "Search result:", $result)
If $result < 1 Then 
  _ExcelBookClose($oExcel,1)
  Exit   
EndIf

MsgBox(0, "Test result","OK")

RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ActiveComputerName","ComputerName","REG_SZ",$sCellValue)
RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName","ComputerName","REG_SZ",$sCellValue)
RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters","NV Hostname","REG_SZ",$sCellValue)
RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters","Hostname","REG_SZ",$sCellValue)
Sleep(3000)
_ExcelBookClose($oExcel,1);
现在有个问题,修改注册表里计算机名字这个功能不能实现,经测试必须用管理员权限运行才能实现,用管理员权限运行这段代码的功能怎么实现?
发表于 2018-1-2 09:05:08 | 显示全部楼层
回复 11# jimobumo

給你的資料看了嗎??

這個問題 不就是在給你的資料裡面  就有解的嗎??
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-12-24 07:04 , Processed in 0.300514 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表