Option Explicit
'----------------------------------- Script Description ----------------------
'This script takes an input file and writes out in another folder
'a file vnc conenction file for each of the machines in the text file
'-----------------------------------------------------------------------------
Dim InputFile, OutputFolder, InputDescriptionFile
Dim oFSO, oInputFile, MachineName, sNextLine
Dim OutputFile, oOutputFile, x, TempSplit
Dim oRootDSE, strDNC, objDomain, objMember
Dim CN, ADSPath, UB, OU
'--------------------- Constants ----------------------
Const FailIfNotExist = 0
Const OverwriteIfExist = -1
Const OpenAsDefault = -2
Const CreateIfNotExist = -1
Const OpenAsASCII = 0
Const OpenAsUnicode = -1
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
'--------------------- END Constants --------------------
'--------------------- Variables -------------------------
InputFile = "E:\Scripts and Utilities\Generate VNC files\vnchosts\hosts.txt" ' This is the file containing the machine names, one on each line..
OutputFolder = "E:\Scripts and Utilities\Generate VNC files\vncfiles\" 'This the folder to output the vnc connection files to. Make sure you trial with a slash - "\"
'--------------------- END Variables -------------------------
'-------------------- MAIN PROCEDURE--------------------------
Set oRootDSE = GetObject("LDAP://RootDSE")
strDNC = oRootDSE.Get("defaultNamingContext")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oInputFile = oFSO.OpenTextFile(InputFile, ForWriting, CreateIfNotExist, OpenAsASCII) ' Open the Input File
GetListAD(strDNC)
CreateTheFiles()
'-------------------- MAIN PROCEDURE END --------------------------
Function CreateTheFiles()
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oInputFile = oFSO.OpenTextFile(InputFile, ForReading) ' Open the Input File
x = 0
Do Until oInputFile.AtEndOfStream 'Read each line in
sNextLine = oInputFile.Readline
TempSplit = Split(sNextLine,",")
MachineName = TempSplit(0) ' Each line is equal to a machine name
OutputFile = TempSplit(1) ' Each line is equal to a machine name
If Not oFSO.FolderExists(OutputFolder & OutputFile) Then
oFSO.CreateFolder(OutputFolder & OutputFile)
End If
OutputFile = OutputFolder & OutputFile & "\" & MachineName & ".vnc" ' Create the Output File, from the Output folder and the machine name
Set oOutputFile = oFSO.OpenTextFile(OutputFile, ForWriting, CreateIfNotExist)
oOutputFile.WriteLine("[connection]")
oOutputFile.WriteLine("host=" & MachineName)
oOutputFile.WriteLine("port=" & "5900")
oOutputFile.Close ' Clean up
MachineName = Null ' Clean up
x = x + 1
Loop ' Repeat until end of stream...
End Function
Function GetListAD(strDNC)
Set objDomain = GetObject("LDAP://" & strDNC) ' Bind to the top of the Domain using LDAP using RootDSE
' WScript.Echo "Connected to: " & strDNC
x = 0
Call enummembers(objDomain)
oInputFile.Close
End Function
Sub enumMembers(objDomain)
For Each objMember In objDomain
If ObjMember.Class = "computer" Then
x = x + 1
Cn = ObjMember.CN
ADSPath = ObjMember.ADSPath
ADSPath = Split(ADSPath,",")
UB = (UBound(ADSPath) - 2)
OU = Split(ADSPath(UB),"=")
'WScript.Echo Cn & "," & OU(1)
oInputFile.WriteLine Cn & "," & OU(1)
End If
' If the AD enumeration runs into an OU object, call the Sub again to itinerate
If objMember.Class = "organizationalUnit" Or OBjMember.Class = "container" Then
enumMembers (objMember)
End If
Next
End Sub
|