Kaseya Community

nice challenge: Whats the best way to realize this? vbscript? Powershell? or agent procedure?

This question is answered

Hi, 

in #vAgentConfiguration.Agent TempDir# I have 4 text files.

The content is a drive-letter.

Example 1:

filename: content:
PublicFolderDatabaseEdbVolume.txt C:
PublicFolderDatabaseLogVolume.txt D:
PublicFolderDatabaseEdbTemp.txt C:
MailboxDatabaseLogVolume.txt E:

 Example 2 (another Server):

filename: content:
PublicFolderDatabaseEdbVolume.txt C:
PublicFolderDatabaseLogVolume.txt C:
PublicFolderDatabaseEdbTemp.txt D:
MailboxDatabaseLogVolume.txt D:

Example 3 (another Server):

filename: content:
PublicFolderDatabaseEdbVolume.txt C:
PublicFolderDatabaseLogVolume.txt C:
PublicFolderDatabaseEdbTemp.txt C:
MailboxDatabaseLogVolume.txt C:

Now I need a possibility to get this informations back to a new text file in this format:

for example 1:

filename: content:
ExchangeVolumes.txt C:,D:,E:

for example 2:

filename: content:
ExchangeVolumes.txt C:,D:

for example 3:

filename: content:
ExchangeVolumes.txt C:


Whats the best way to realize this?

Thanks for helping!

Richard

Verified Answer
  • This one gets rid of the duplicate drive letters

    ****************************************

    Dim fso, folder, files, NewsFile,sFolder

    Const ForReading = 1

    Const ForAppending = 8

    Drives = ""

    sfolder = "C:\kworking\exchange"  ' Change this to what every directory you use

    Set fso = CreateObject("Scripting.FileSystemObject")

    Set folder = fso.GetFolder(sFolder)

    Set files = folder.Files

    row = 0

    For each folderIdx In files

    sName=lcase(folderIdx.Name)

    if right(sName,3)="txt" then

    row = row + 1

    Set WshShell = WScript.CreateObject("WScript.Shell")

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objfile = objFSO.opentextfile(sfolder + "\" + folderIdx.Name,forreading)

    do until objfile.AtEndOfStream

    filecontent = objfile.readline

    Set objRegEx = CreateObject("VBScript.RegExp")

    objRegEx.Pattern = filecontent

    Set colMatches = objRegEx.Execute(drives)  

    If colMatches.Count = 1 Then

    else

    on error resume next  

    if err.number = 0 then  

    if row = 1 then

    Drives = filecontent

    else

    Drives = Drives & "," & filecontent

    End if

    end if

    end if

    loop

    objFile.close

    'folderidx.delete    'Uncomment this line if you want the files deleted after they have been processed

    end if

    next

    Set objFile = objFSO.createTextFile(sfolder + "\ExchangeVolumes.ini")

    objFile.WriteLine(drives)

    objfile.close

All Replies
  • Well im not sure what's the "BEST" way of doing it is and im sure that will start a pointless debate.

    However if it was me i'd use VB

    Doing a read line and grouping them then out putting the results in the desired format.

    I guess the real question should be why do they need to be in C:,D:,E: format  

  • Hi Michael,

    I need this format for a exchange Backup Procedure and my last step is to get this information back in this format!

    wbadmin start backup -backupTarget:F: -include:C:,D:  -vssfull -quiet

    Thanks for helping

    Richard

  • Try this

    It's not perfect as it includes the same drive letter if it occurs more than once.

    Also have changed the output file name to give it an extension of .ini.

    This was simply because while testing all the text files where in the same folder so the script double processes the ExchangeVolumes.txt file as it has the same extension as the other files .. So have a play ...

    Cheers Paul

    -------------------------

    Dim fso, folder, files, NewsFile,sFolder

    Const ForReading = 1

    Const ForAppending = 8

    Drives = ""

    sfolder = "C:\kworking\exchange"  ' Change this to what every directory you use

    Set fso = CreateObject("Scripting.FileSystemObject")

    Set folder = fso.GetFolder(sFolder)

    Set files = folder.Files

    row = 0

    For each folderIdx In files

    sName=lcase(folderIdx.Name)

    if right(sName,3)="txt" then

    row = row + 1

    Set WshShell = WScript.CreateObject("WScript.Shell")

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objfile = objFSO.opentextfile(sfolder + "\" + folderIdx.Name,forreading)

    do until objfile.AtEndOfStream

    filecontent = objfile.readline

      on error resume next  

    if err.number = 0 then  

    if row = 1 then

    Drives = filecontent

    else

    Drives = Drives & "," & filecontent

    End if

    end if

    loop

    objFile.close

    'folderidx.delete    'Uncomment this line if you want the files deleted after they have been processed

    end if

    next

    Set objFile = objFSO.createTextFile(sfolder + "\ExchangeVolumes.ini")

    objFile.WriteLine(drives)

    objfile.close

  • This one gets rid of the duplicate drive letters

    ****************************************

    Dim fso, folder, files, NewsFile,sFolder

    Const ForReading = 1

    Const ForAppending = 8

    Drives = ""

    sfolder = "C:\kworking\exchange"  ' Change this to what every directory you use

    Set fso = CreateObject("Scripting.FileSystemObject")

    Set folder = fso.GetFolder(sFolder)

    Set files = folder.Files

    row = 0

    For each folderIdx In files

    sName=lcase(folderIdx.Name)

    if right(sName,3)="txt" then

    row = row + 1

    Set WshShell = WScript.CreateObject("WScript.Shell")

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objfile = objFSO.opentextfile(sfolder + "\" + folderIdx.Name,forreading)

    do until objfile.AtEndOfStream

    filecontent = objfile.readline

    Set objRegEx = CreateObject("VBScript.RegExp")

    objRegEx.Pattern = filecontent

    Set colMatches = objRegEx.Execute(drives)  

    If colMatches.Count = 1 Then

    else

    on error resume next  

    if err.number = 0 then  

    if row = 1 then

    Drives = filecontent

    else

    Drives = Drives & "," & filecontent

    End if

    end if

    end if

    loop

    objFile.close

    'folderidx.delete    'Uncomment this line if you want the files deleted after they have been processed

    end if

    next

    Set objFile = objFSO.createTextFile(sfolder + "\ExchangeVolumes.ini")

    objFile.WriteLine(drives)

    objfile.close

  • Hi Paul,

    what should I say? GREAT WORK!

    Thanks much

    Richard