近期换了一家公司,负责IT所有的服务器、网络、资产管理。为了便于桌面用户的资产管理,我决定使用计算机的主机编号来命名,然后从域中就可以很清晰的看到每一个资产的状态。我的命名规则是SZ-主机编号。
脚本代码如下:
- 'get current pcname
- strComputer = "."
- Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
- Set colComputers = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
- For Each objComputer in colComputers
- strPCname = UCase(objComputer.Name)
- Next
-
- 'get sn number
- Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
- Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS")
- For Each objItem in colItems
- strsn = objItem.SerialNumber
- Next
-
- StrRname = "CNSZ-" & strsn
-
- If strPCname = StrRname then
- wscript.quit
- End if
-
- Set objWMIService = GetObject("winmgmts:" & "{ impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
- Set colComputers = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
- For Each objComputer in colComputers
- err = objComputer.Rename(StrRname)
- Wscript.Echo "The new PC name is " & StrRname & "." & VbCrLf & "Need reboot to take effect!"
- Next
-
- 'Reboot or not
- strReboot = MsgBox("Do you want to reboot computer?", vbYesNo, "Warning!")
- If strReboot = vbNo Then
- Wscript.Quit
- End If
-
- '==Reboot Computer==
- strComputer = "."
- Set objWMIService = GetObject("winmgmts:" _
- & "{ impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
- Set colOperatingSystems = objWMIService.ExecQuery _
- ("Select * from Win32_OperatingSystem")
- For Each objOperatingSystem in colOperatingSystems
- ObjOperatingSystem.Reboot()
- Next
本文转自yangye1985 51CTO博客,原文链接:http://blog.51cto.com/yangye/562546,如需转载请自行联系原作者