Monday, April 4, 2011

VBScript to Set DNS Suffix, Check File, Check Registry

The below script...
  • Reads computer names from c:\temp\compters.txt
  • Sets the DNS suffix to append under Advanced TCP/IP properties 
  • Verifies a file exists C:\Temp\efs_found.txt and the file size is 0
  • Checks a registry entry

'Check Computers in c:\temp\computers.txt
'Set DNS suffix
'Check a file exists and the file size
'Check Registry setting

Option Explicit
Dim objFSO, objTextFile, strNextLine, strCantConnect, arrServiceList
Dim objFolder, strKeyPath, strValueName, strValue, oReg, i, objFile

' Read a Text File into an Array
Const HKEY_LOCAL_MACHINE = &H80000002
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("c:\temp\computers.txt", ForReading)
WScript.Echo "Setting DNS Suffix, Checking Encypted Files, Checking ODBC on C:\Temp\Computers.txt"
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    'Wscript.Echo "Server name: " & arrServiceList(0)
    strCantConnect = "0"
  ' Verify connection to the Desktop
 Set objFSO = CreateObject("Scripting.FileSystemObject")
  If objFSO.FolderExists("\\" & arrServiceList(0) & "\Admin$") Then
      Set objFolder = objFSO.GetFolder("\\" & arrServiceList(0) & "\Admin$")
  Else
      WScript.Echo "Can't Connect to " & arrServiceList(0) & chr(10) & chr(13) & "Please verify it is contactable via DNS and you have Admin rights"
      strCantConnect = "1"
  End If
 
 ' Create Registry DNS Search String Value
 If  strCantConnect = "0" Then
 Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    arrServiceList(0) & "\root\default:StdRegProv")
  strKeyPath = "SYSTEM\CurrentControlSet\services\Tcpip\Parameters"
 strValueName = "SearchList"
 strValue = "contoso,contoso.local"
 oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
 'WScript.Echo "DNS Search Domains Added to "& arrServiceList(0) & " Registry"
 Else
 End If

 ' Verify that the EFS checking script
has been run
 Set objFSO = CreateObject("Scripting.FileSystemObject")
 If objFSO.FileExists("\\" &  arrServiceList(0) & "\C$\Temp\efs_found.txt") Then
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFSO.GetFile("\\"  & arrServiceList(0) & "\C$\Temp\efs_found.txt")
   If objFile.Size = "0" Then
   'WScript.Echo "No encypted Files Exist on "  & arrServiceList(0)
   Else
   WScript.Echo "Encypted Files Exist on "  & arrServiceList(0) & " Unencrypt before Migrating!"
   End If
 Else
 If  strCantConnect = "0" Then
    Wscript.Echo "Script for checking for Encryption Files hasn't been run."& chr(10) & chr(13) & "Login to "  & arrServiceList(0) & "and run
FindEFS.bat"& chr(10) & chr(13) & "Before continuing!"
 End If
  End If
 ' Check ODBC settings for Dataworks
 If  strCantConnect = "0" Then
  Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
     arrServiceList(0) & "\root\default:StdRegProv")
   strKeyPath = "SOFTWARE\ODBC\ODBC.INI\DataWorks"
  strValueName = "Trusted_Connection"
  oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
 If strValue = "Yes" Then
  WScript.Echo "Dataworks ODBC Settings NOT SET CORRECTLY on "  & arrServiceList(0) &"!"
  Else
     'Wscript.Echo "Dataworks ODBC Settings look OK on "  & arrServiceList(0)
     End If 
    End if
        For i = 1 to Ubound(arrServiceList)
        Wscript.Echo "Service: " & arrServiceList(i)

    Next
Loop
Wscript.Echo "Finished Checking and setting DNS!"

No comments:

Post a Comment