File

PowerShell Remove Old Files From a Directory

Script for removal of old backup or logfiles from a specific directory. The example below removes all files from a directory that are older than 90 days. A log of all files that are removed is written to the windows eventlog.

# Global config settings...
[string]$sDirName = "D:\LogFiles"
[string]$sFileNameMask = "*.log"
[string]$sGCIMask = $sDirName + "\" + $sFileNameMask
[string]$sEventLogMessage = "### Cleanup Script Started... ###`r`n"

# Functions and Procedures...
function fGetFileModTime([string]$sFileName) {
  $vFileInfo = get-childitem $sFileName
  return $vFileInfo.lastwritetime 
}

# Start Main script...
if (Test-Path $sDirName) {
get-childitem $sGCIMask | 
    % { 
        # Get File Last Write Time...
        [datetime]$dFileModTime = fGetFileModTime($_.fullname)
        if ($dFileModTime -lt (Get-date).AddDays(-90)) {
        remove-item $_.fullname
        $sEventLogMessage = $sEventLogMessage + "File " + $_.fullname + " (" + $dFileModTime.DateTime + ") removed...`r`n"
        }
    }
}

# Finish and Cleanup...
[string]$sEventLogMessage = $sEventLogMessage + "### Cleanup Script Finished... ###"
write-eventLog -LogName "Windows PowerShell" -Source "PowerShell" -EventID 30001 -Message $sEventLogMessage -EntryType Information

PowerShell Check File Modified Time

Script to check is a specific file exists and its modified time is less than 1 day in the past. This script uses some custum functions from a imported module.

# Import DBA modules...
Import-Module 'D:\Path\To\DefaultFunctionsModule.psm1'

# Global config settings...
[string]$sSubject1 = "SBO:CheckLogFiles Errors Found..."
[string]$sSMTPServer1 = "smtp.example.com"
[string]$sToAddress1 = "mail@example.com"
[string]$sEventLogSource = "{eventlogsource}"

# Functions en Procedures...
function fnGetFileModTime([string]$sFileName) {
  $vFileInfo = get-childitem $sFileName
  return $vFileInfo.lastwritetime 
}

# Start Main script...
if ((Test-Path $args[0]) -and ($args[1])) {
  # Request file modified time...
  [datetime]$dFileModTime = fnGetFileModTime("$($args[0])")
  # Alert if file modified time is older than 1 day...
  if ((Get-date).AddDays(-1) -gt $dFileModTime) {
  $sMessage1 = "ERROR: File modified time is older than 1 day...`r`n`r`nFilename: " + $args[0] + "`r`nService: " + $args[1]
  fnWriteEventLog -sNode (fnGetFQDN) -sMessage $sMessage1
  fnSendMail -sSMTPSrvrName $sSMTPServer1 -sFrom (fnGetFQDN -swIsEmail) -sTo $sToAddress1 -sSubject $sSubject1 -sBody $sMessage1
  }
} else {
  if (($args[1])) {
    # Alert if file does not exist...
    $sMessage1 = "ERROR: File does not exist...`r`n`r`nFilename: " + $args[0] + "`r`nService: " + $args[1]
    fnWriteEventLog -sNode (fnGetFQDN) -sMessage $sMessage1
    fnSendMail -sSMTPSrvrName $sSMTPServer1 -sFrom (fnGetFQDN -swIsEmail) -sTo $sToAddress1 -sSubject $sSubject1 -sBody $sMessage1
  } else {
      $sMessage1 = "ERROR: Missing argument... [CheckModTime.ps1 `"c:\path\to\file`" `"service`"]`r`n`r`nFilename: " + $args[0]  + "`r`nService: " + $args[1]    
      echo $sMessage1
  }
}

GIT Version Control Basics

Clone a Repository, add files, commit and push to master…
$>git clone ssh://git@gitlab.example.com:22/Project.git
$>git add [filename|-A]
$>git commit -a -m “Initial upload…”
$>git push -u origin master

Create a branch for the new release and check-out to the new branch…
$>git branch release-4.1.11
$>git checkout release-4.1.11

Make the changes to the files and commit the changes
$>git commit -a -m “Change description…”

Mark as executable…
$>git update-index –chmod=+x build.sh

Push to server…
$>git push -u origin release-4.1.11

Merge changes with master branch…
A) Merge with master takes place on server. Pull new master branch from server…
$>git checkout master
$>git pull -a

B) Merge all changes to the master locally and push to server…
$>git checkout master
$>git merge release-4.1.11
$>git push -u origin master

Delete old branch…
$>git branch -d release-4.1.11
* Option -D deletes a not merged branch…