troubleshooting Question

Powershell script check for files and log

Avatar of Skumar_CCSA
Skumar_CCSAFlag for India asked on
PowershellVisual Basic.NETVB ScriptWindows BatchScripting Languages
5 Comments1 Solution240 ViewsLast Modified:
Hi ,

i have working script..
i want to put additional checks in the script to check for files and proceed further.
it should check for files after it checks for source folder.
if all three files available then proceed further to run remaining lines of checking backupfolder.
if even one file is missing or all missing it should write log the missing files and exit

Files names to check-

$logdate=Get-Date -Format ddMMMyyyy_HHmmss
$Logfile = "E:\Logs\FileProcessing_$logdate.txt"

function LogWrite ([string]$logstring)
   Add-content $Logfile -value $logstring

#Zip Files

#check if source exists
if(!(Test-Path $sourcefolder)){
    $msj="The source folder ""$($sourcefolder)"" doesn't exists. Exiting..."
    LogWrite "$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) ERROR:`t$msj"
    LogWrite -ForegroundColor Red $msj
    exit -1

#check the target folder or create it
if(!(Test-Path $backupFolder)){
    $msj="The target folder ""$($backupFolder)"" was created"
    LogWrite "$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) EXECUTE:`t$msj"
    LogWrite -ForegroundColor Magenta $msj
    New-Item -ItemType Directory -Path $backupFolder | Out-Null
foreach($file in (Get-ChildItem -File $sourcefolder -Filter "*.xml" -Recurse)){
    $date = Get-date -Format "ddMMyyyy_HHmmss"
    $destinationFile = "$($FILE.Directory.FullName)\$($file.Name).zip"
    $MoveFile = "$backupFolder\$($file.BaseName)_$date.xml"
        $msj="Compressing File ""$($file.Name)"" into ""$destinationFile"" succeeded"
        Compress-Archive -Path $($file.FullName) -CompressionLevel Optimal -DestinationPath  $destinationFile -Force
        LogWrite "$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) COMPRESS:`t$msj"

        #successful compress
        $msj="Moving File ""$MoveFile"""
        Move-Item -Path $($file.FullName) -Destination $MoveFile -Force
        LogWrite "$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) ARCHIVE:`t$msj"
        $msj="There was an Error: $($_.exception.Message)"
        LogWrite "$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) ERROR:`t$msj"
        LogWrite -ForegroundColor Red $msj
        exit -1
Michael B. Smith
Managing Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros