Friday, November 21, 2014

Archive a file based on its creation date

I have recently run into a situation where a co-worker needed a script to move a file that is created from a nightly process to another directory for archival purposes.  He wanted the file to be renamed with the creation date. 
Years ago I had to create a vbs for a similar process, so I followed this guide from the scripting guy:  And I came up with this script.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='c:\Logs'} Where " _
        & "ResultClass = CIM_DataFile")
For Each objFile In FileList
    strDate = Left(objFile.CreationDate, 8)
    strNewName = objFile.Drive & "logs\" &  strDate & "." & "txt"
    strNameCheck = Replace(strNewName, "\", "\\")
    i = 1
    Do While True
        Set colFiles = objWMIService.ExecQuery _
            ("Select * from Cim_Datafile Where Name = '" & strNameCheck & "'")
        If colFiles.Count = 0 Then
            errResult = objFile.Rename(strNewName)
            Exit Do
        Else
            i = i + 1
            strNewName = objFile.Drive & objFile.Path & _
                strDate & "_" & i & "." & "txt"
            strNameCheck = Replace(strNewName, "\", "\\")
        End If
    Loop
Next
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True  
objRegEx.Pattern = "\d{0}"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Logs\")
Set colFiles = objFolder.Files
For Each objFile in colFiles
    strSearchString = objFile.Name
    Set colMatches = objRegEx.Execute(strSearchString) 
    For Each strMatch in colMatches
        strFolderName = strMatch.Value
        strFolderName = Replace(strFolderName, "_", "")
        strFolderName = "c:\Logs\backups" & strFolderName & strDate & "\"
        If Not objFSO.FolderExists(strFolderName) Then
            Set objNewFolder = objFSO.CreateFolder(strFolderName)
        End If
    Next
    objFSO.MoveFile objFile.Path, strFolderName

Next
What this script does is lists all files under c:\Logs and puts them in a Backup folder according to its creation date.  For instance a file that was copied to C:\Logs on 10/31/2014 will be archived to C:\Logs\Backup20141031.  It will not change the name of the file. I will simply put all files that were copied to C:\Logs into a directory called backup+ the date that is was copied to C:\logs.

Friday, August 1, 2014

VBScript to send mail using cdo

I was tasked with creating a script for a user to send logs for an app via email with attachments on a schedualed basis.  Of course you can not schedule Outlook to to send mail.  You could use mapisend.exe which is a command line utility that ships with MS BackOffice Resource Kit. You can use it to create a batch but you have to have mapi installed on the server or pc that you are sending the email from.  Blat is also used to send email through the command line.  I do not have any experience with Blat. Here is a link to send an email through vbs that I have been using for scripting and schedualing for years.

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "hdl test"
objMessage.From = "username@domain.org"
objMessage.To = "username@domain.org"
objMessage.To = "username@domain.org"
objMessage.To = "username@domain.org"  
objMessage.TextBody = ""
objMessage.Addattachment "%systemdrive%\temp\Test.txt"
'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp server ip address"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send

Here is a script that I have used to troubleshoot reporting issues from the reporting application to  the SMTP server.

Here is a good link to test troubleshoot sending mail uing telnet.  I have always experienced that many venders will want you to do send email from telnet to ensure that there are not issues with your smtp server. 
 

Monday, January 6, 2014

Enumerate Members of a Group by user name.

This is a script that queries an AD group and writes the members of the group to an Excel spread sheet. I actually googled most of the info but I do not remember what site I got it from. 

Dim objGroup, objExcel, iRow, strUser
Set objGroup = GetObject("LDAP://cn=MRKTING,ou=NEWYORK,DC=SY,DC=Domain,DC=Domain,DC=RIVERSIDE,DC=CA,DC=US")
Set objExcel = CreateObject("Excel.Application")
With objExcel
.SheetsInNewWorkbook = 1
.Workbooks.Add
.Visible = True
.Worksheets.Item(1).Name = mid(objGroup.Name, instr(1,objGroup.Name,"=") + 1 ) 'set Worksheet name to that of the DL
rem .Worksheets.Item(1).Name = dldump
.Cells(1,1) = "UserID"
irow=2
For Each strUser in objGroup.Member
Set objUser = GetObject("LDAP://" & strUser)
.Cells(iRow,1) = objUser.samaccountname
irow=irow + 1
Next
.Columns(1).entirecolumn.autofit
End With
Set objExcel = Nothing
Set objGroup = Nothing