Saturday, January 28, 2006

Change Computer Name with VBScript

Here is the first script. As I wrote this script before creating the blog I have not included any of the fun stuff we go through while writing the script. This will change from here on out.
My one note with regard to this script is that the reboot function of WMI requires security settings to change within dcom. This would complicate things since the whole point of this project is to automate everything that I can automate. Therefore I found that simply calling shutdown.exe -r -t 05 from the shell accomplished everything I needed without extra settings.
Option Explicit
Function GetCompName()
Dim objNetComp
Dim strComputer
''Get current name of comp
Set objNetComp = CreateObject("WScript.NetWork")
strComputer = cstr(objNetComp.ComputerName)
GetCompName = strComputer
End Function


Function CreateName()
Dim intIp
Dim cTempIPAddress
Dim cIPAddress
Dim cIPAddressKey
Dim CliSeed
Dim oSh
Dim cInterfacesKey
Dim cNICSearch
Dim cNicServiceName
Dim IPbase
''CliSeed will seed the name with something that will be thier everytime the script is run.
''This will need to be standardized
CliSeed = "MicahNew"


''Generate New Name by grabbing the last octet of the ip address
''This should absolutely insure uniqueness
Set oSh = CreateObject("WScript.Shell")
cInterfacesKey="HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\"
cNICSearch="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\2\ServiceName"
'''''First check which network card to use
cNicServiceName=oSh.RegRead(cNICSearch)

'''''Now read the IP Address from that card
cIPAddressKey=cInterfaceskey + cNicServiceName+"\IPAddress"
cTempIPAddress=oSh.RegRead (cIPAddresskey)

''''Split the items in the var tempIPAddress to the octets in array IPAddress
cIPAddress=Split (cTempIPAddress(0),".",4)
IpBase = CStr(cIPAddress(3))
''''Create the name
strNewName = CliSeed & "_" & IpBase
'MsgBox(strNewName)
CreateName = strNewName
End Function
Function ChangeCompName(strNewName, strOldName)
Dim key
Dim strKeyPath
Dim objReg
Dim intRc
''Setup registry for edit
Const HKLM = &H80000002
strKeyPath = "System\CurrentControlSet\Control\ComputerName\ComputerName"

''Edit the Registry
''set 1
Set objReg = GetObject("winmgmts:\\" & strOldName & "\root\default:StdRegProv")
intRc = objReg.SetStringValue(HKLM, strKeyPath, "ComputerName", strNewName)
''set 2
strKeyPath = "System\CurrentControlSet\Services\Tcpip\parameters"
intRc = objReg.SetStringValue(HKLM, strKeyPath, "NV Hostname", strNewName)
End Function

Function RebootComp(strComputer)
On Error Resume Next
Dim objWmi
Dim objOs
Dim wshShell

''Reboot the computer
Set wshShell = WScript.CreateObject("WScript.Shell")
Set objWmi = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
For Each objOs In objWmi.InstancesOf("Win32_OperatingSystem")
objOs.reboot()
Next
wshShell.Run("shutdown -r -t 05")
End Function

Dim strOldName
Dim strNewName

strOldName = GetCompName()
strNewName = CreateName()
RebootComp(strOldName)

1 Comments:

Anonymous Anonymous said...

If anything they rewarded me for my hard work and service through steak cookouts yes you read correctly steak, paid days off for making your quota and rewards for safety. [url=http://www.mulberryhandbagssale.co.uk]Mulberry uk[/url] It will fly formation for a while, then wing over into a tight climbing turn. [url=http://www.goosecoatsale.ca]canada goose women parka[/url] Htasjdeab
[url=http://www.pandorajewelryvip.co.uk]pandora outlet[/url] Lzpsfjobl [url=http://www.officialcanadagooseparkae.com]canada goose toronto factory[/url] mtycsmnxf

8:29 AM, December 24, 2012  

Post a Comment

<< Home