Sunday, January 29, 2006

Setting My Documents Location

I am aware that this setting can be set via group policy. However myself and a coworker responded to a server down at a large sized client once. The errors on the server required an immediate reload. Upon restoring the AD to the temporary server which we placed in the stead of the downed server we found that these settings were somehow locked and could not be changed from the client. While this may be a one-in-a-million situation, group policy still has a huge number of 'moving parts'. A script to set the my docs should help alleviate any future problems. This should be a quick script as I believe it is simply one registry key. I have created a file on my C drive named 'slappy' as this is unlikely to appear anywhere else in the registry. I am going to set the my docs location to this file and then search the registry for the word 'slappy'.

Enabling RDP

Rdp is one of those settings that is a simple click. However, it comes back to a simple click on 30 computers adds time. Therefore since I use rdp on almost all of my XP pro clients, I choose to come up with a way to add this functionality with a script. I scoured the internet and several books and found no tutorials on doing this. This does not mean they aren't there, simply that I did not find them. I choose a different route this time. First I downloaded windiff. This program will allow me to specify two different versions of a file and it will give me the precise changes over the versions. I turned off RDP on my home computer and exported the registry. I then turned rdp back on and reexported the registry. Upon running windiff on these to files I was shocked to find only two keys changed. This is either a stroke of good luck or a problem caused by the fact that this was not the first time I had enabled RDP. The below keys were all that had changed: [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Terminal Server] "fDenyTSConnections"=dword:00000001 ''Remote Desktop Disabled "fDenyTSConnections"=dword:00000000 ''Remote desktop Enabled [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server] "fDenyTSConnections"=dword:00000001 ''Remote Desktop Disabled "fDenyTSConnections"=dword:00000000 ''Remote Desktop Enabled As a test I created two .reg files to apply the changes. It appears to have worked flawlessly. So it is now time to convert this into a script. After wasting a half an hour because of a typo I was able to produce a short and sweet script to enable RDP.
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 EditRegistry(strCompName)
Dim key Dim strKeyPath
Dim objReg
Dim intRc
Dim regVal
regVal = 0
''Setup registry for edit
Const HKLM = &H80000002
strKeyPath = "SYSTEM\ControlSet001\Control\Terminal Server"
''Edit the Registry
''set 1
Set objReg = GetObject("winmgmts:\\" & strCompName & \root\default:StdRegProv")
intRc = objReg.SetDWORDValue(HKLM, strKeyPath, "fDenyTSConnections", regVal)
''set 2
strKeyPath = "SYSTEM\CurrentControlSet\Control\Terminal Server"
intRc = objReg.SetDWORDValue(HKLM, strKeyPath, "fDenyTSConnections", regVal)
End Function
Dim compName
compName = GetCompNameEditRegistry(compName)

Saturday, January 28, 2006

Script to Set DNS

When I spoke with coworkers about this script they asked that since I control DHCP in the network, why should I manually assign these addresses? Every admin out there has seen a computer take 1 minute getting an address from dhcp. Plus, as the primary address is their local domain dns and the second address is a widely used web dns server (207.217.120.83 sprints I believe) their should be no situation where they are required to change name servers.
The first problem I ran into with this script is that there appears to be no easy way to assign addressing via vbscript without directly accessing the registry. However a quick batch file should be able to drop to netsh and assign this manually. While attempting to use the Wscript.Shell.Run to run each command via the shell I ran into a problem which I believe is related to the speed with which the commands are being processed. Therefore I am going to attempt to create a batch file which calls netsh and process the commands from there.
I searched the web for some hints and came to this reference on programmaticly displaying the settings of network adapters. Based on this I was able to create a batch file containing the below content:
netsh int ip set dns name="Local Area Connection" source=static addr=192.168.1.10 register=PRIMARY
netsh int ip add dns name="Local Area Connection" addr=207.217.120.83 index=2
The second command in this script threw me for a loop. It seems counter intuitive to require separate objects to set a primary and a secondary name server. To finalize this script I include a Wscript.Shell.Run call in a vbscript to the batch file.
Set wshShell = WScript.CreateObject("WScript.Shell")
nReturnCode = wshShell.Run("setdns.bat")

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)

List of Scripts I have identified for the project.

These are the "units of work" I have currently identified as necessary for the Domain Scripting Project. Once again, please comment.
  • Activate Remote Desktop
  • Create local computer admin account
  • Domain Users group = Local Admin
  • Install Telnet
  • Join Domain
  • Registry Key to Set My Documents Location
  • Set DNS servers
  • Set Printers
  • Activate Telnet
  • Disjoin Domain

Introduction to the "Domain Scripting Project"


For lack of a better title I have chosen "The Domain Scripting Project". I am a Network Administrator. Any of you who share this position are aware of the large amount of redundant work we do while creating commercial networks. From joining everyone to the domain to guaranteeing service availability we waste hours of time.
Consider this: a standard commercial setup with 30 computers, 2 servers, a point-to-point VPN solution, a client to VPN solution and 3 commercial grade copier/printers. Normally we manually do the work on each client. That means spending 7 minutes per computer joining them to the domain. A menial task which entails 8 screens, 2 reboots and several client side settings. Most of us can do this in our sleep, however at 210 total minutes this sucks up more than a quarter of a standard workday.
I believe that a majority of this can be scripted or assigned via Group Policy. I have compiled a list of twelve scripts which I believe once created and integrated will gain administrators like myself significant time. So, without further ado its time to get into the scripting.
Please comment on anything you see or think. All scripts and GP's will be available, as I hope that anyone reading this will post any ideas, comments or criticisms.