Friday, November 27, 2009

VBScript for changing a computer's name

I am working on some scripts that will help us check and make sure our computer names in Windows are consistent with our computer names in DNS. Included here is a script that just changes the name of a computer to one that you specify in an input box, after also telling you the current computername in Windows. Then once you put in the new name, it asks you if you want to restart so that the change takes affect, and then restarts your computer. This script was written for 32-bit Windows XP. I will admit that I got a lot of the ideas from books and the web, but pretty much wrote this myself. I hope this will be useful to some folks.  I also have to apologize for some of the formatting; some of the lines were longer for my code than the blog editor here can take. 



' This program is written to find the existing computername of a Windows Computer, display it to the user, and then ask the user for a new computername.
' Then the script changes the computername to the one entered by the user and reboots Windows.


'we define our variables here
option explicit
dim strComputerName
dim wshShell
dim vInput
dim vResult
dim sReg1
dim sReg2
dim vInputU
dim vMsg


'These are the registry keys used to change the computername later on
sReg1 = "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\"
sReg2 = "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\ParameterS\"


'These lines get the computername from Windows
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )


'And here we get the computername registered in DNS


'Here we ask the user for the new computername
vInput =  InputBox("The current computer name in Windows is  " & strComputerName & ".  Please enter a new name:","Computer Name")


'If the user enters "Cancel"
If vInput = -1 Or vInput = "" Then
    vMsg = MsgBox ("You have canceled operation.", 16, "Good Bye")    'this message appears


Else ' IF the user did not cancel
    vResult = MsgBox("You have entered " & vInput & ". Is this correct?", 292, "Computer Name") 'We verify what the user typed


    If vResult = vbNo Then        'if the typed computername is not verified by the user
        vInput =  InputBox("The current computer name is " & strComputerName & ". Please enter a new name:","Computer Name")    'we ask for the user's input again
            If vInput = -1 Or vInput = "" Then    ' and if the user clicks "cancel" this time
                vMsg = MsgBox ("You have canceled operation.", 16, "Good Bye")    'this message appears
            Else    ' The user clicks "OK"
                vResult = MsgBox("You have entered " & vInput & ". Is this correct?", 292, "Computer Name") 'we ask for the user to verify again
                If vResult = vbNo Then        'if the user does not verify again
                    vMsg = MsgBox ("Sorry.  Try running the program again.", 48, "Good Bye")    'then the program exits with this message
                End If
               
            End If
'at this point vResult should now be 6 if verified above and the following else clause should kick in


    Else     'if the typed computername is then verified by the user
        If vResult <> "" Then    'and if we don't have a null computername
            vInputU = UCase(vInput)    'we put the new computername into upper case
            wshShell.RegWrite sReg1 & "ComputerName", vInputU ' and modify the appropriate keys in the registry
            wshShell.RegWrite sReg2 & "NV Hostname", vInputU
            vMsg = MsgBox ("The computer name has changed.  Would you like to restart?", 36, "Success!") 'this tells you the naming was successful and asks if you want to restart
                If vMsg = vbYes Then            ' if the user wants to restart and clicks "Yes"
                    wshShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 0"        'then the computer restarts
                End If


        Else ' if we have a blank username entered
            MsgBox "Invalid input.  Run program again."        'this message appears
        End If


    End If       
End If









Tuesday, November 17, 2009

VB Script Update

Well, this won't have a lot of detail, but I felt like posting something.

I found that it was indeed feasible to add an extra WINS server to DHCP, so that scripting idea was scrapped. However, I am now working on a project to allow you to check what a computer's name is, and then change it through a text box. We will see how it will work out...

Tuesday, November 10, 2009

Why my head sometimes wants to explode

I found out the following the hard way today.

Right now we are in the middle of an Active Directory migration. I have Windows computers in our classrooms that authenticate to the old SunONE LDAP using an open-source product called pGina. The computers are joined to the domain, but most users don't have domain accounts yet.

A faculty member could not log into the classroom computer. A pGina error spit out "An unknown error has prevented your account from being created.\n\rThis may be due to policy or security settings as well as other machine configuration.\n\rPlease consult your administrator." Remember that no user object exists in AD for this user, so there are no password policies set in this case. He is also authenticating to SunONE. When I tested his username and password on a computer running pGina but not joined to the domain, I had no problems.

What we found is that the logins didn't like the dollar sign at the end of his password. We temporarily changed his password and found it worked, and then changed it back where it had problems again. I suggested to him that he change his password permanently.

It's weirdness like this that makes technology interesting. Or difficult. Take your pick.

Monday, November 9, 2009

VB Scripting N00b

Back when I was in college, a few years back, I studied a lot of computer programming (C, C++, Java, and yes, COBOL!) but I decided to work in tech support and later systems administration. With the kind of work I've had to do, I will have to admit that my coding skills are rather rusty. Due to some changes in my job, I have come to the realization that I really have to work on my scripting skills.

In the Windows world, I understand that it is preferable to learn Powershell and not bother with VBScript if you aren't really familiar with a scripting language. However, I have a task that I want to accomplish and all the client computers are running Windows XP. It's impractical to install Powershell on hundreds of XP clients just to run a script! Therefore, I will be working with VBScript.

I have decided to use Notepad++ as my editor, and have been reading VBScript, WMI, and ADSI Unleashed: Using VBScript, WMI, and ADSI to Automate Windows® Administration, Second Edition by Don Jones.

What is my main task? I would like to add three IP addresses to the list of WINS servers for the active ethernet adapter on a computer. Once the WINS servers are set, I have another .vbs script I would like to run, calling on a third-party program. I have already tried doing this using a batch file with a netsh command, but my concern is that the active network adapter won't always be "Local Area Connection" and I want the end user to be exposed to as few errors as possible. The Win32_NetworkAdapterConfiguration WMI class looks like it has some promise, but right now I'm not totally sure how to make it work for me. I guess I have some more reading to do!

Sunday, November 8, 2009

Hello!

Hi, I am trying again this blogging thing. It seems like most folks who are serious about technology have a blog. I would like to be considered serious about technology, so therefore here is my blog.

In my work I take care of such a wide variety of technologies, it's staggering! I am definitely a jack of all trades, and master of none. This means that I am one of you, those people who are trying to figure things out to keep the technology running, instead of an "expert" (although my users sometime think otherwise, lol!)

Thanks a lot for reading this! I hope to really get something going this time!