Skumar_CCSA
asked on
Powershell script check for files and log
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-
cat.xml
rat.xml
bat.xml
$logdate=Get-Date -Format ddMMMyyyy_HHmmss
$Logfile = "E:\Logs\FileProcessing_$l ogdate.txt "
function LogWrite ([string]$logstring)
{
Add-content $Logfile -value $logstring
}
#Zip Files
$sourcefolder="E:\INT\PUT"
$backupFolder="E:\INT\Arch ive"
#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.FullNam e)\$($file .Name).zip "
$MoveFile = "$backupFolder\$($file.Bas eName)_$da te.xml"
try{
$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"
}
catch{
$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
}
}
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-
cat.xml
rat.xml
bat.xml
$logdate=Get-Date -Format ddMMMyyyy_HHmmss
$Logfile = "E:\Logs\FileProcessing_$l
function LogWrite ([string]$logstring)
{
Add-content $Logfile -value $logstring
}
#Zip Files
$sourcefolder="E:\INT\PUT"
$backupFolder="E:\INT\Arch
#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.FullNam
$MoveFile = "$backupFolder\$($file.Bas
try{
$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"
}
catch{
$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
}
}
ASKER
I am getting error and given below complete script, error msg and log.
Actually I want to check if 3 files exists..
If exists it should write the filename with path in the log file.
if not exists then should write missing file in the log and exit.
can help pls..
#Requires -version 3.0
$WarningPreference="Contin ue"
#variables
$ScriptPath = $(Split-Path -Parent $MyInvocation.MyCommand.De finition)
#Log Timestamp
$date=Get-Date -Format ddMMMyyyyHHmmss
$logdate=Get-Date -Format ddMMMyyyy_HHmmss
$Logfile = "E:\INT\LOG\FileProcessing _$logdate. txt"
function LogWrite ([string]$logstring)
{
Add-content $Logfile -value $logstring
}
LogWrite ("$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) START:`t"+"File Processing of XML`n")
#Zip Files and move to backup folder
$sourcefolder="E:\INT\PUT"
$backupFolder="E:\INT\Arch ive"
#check if source folder 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 if source file exists
if ((Test-Path $cat.xml) -and (Test-Path $rat.xml) -and (Test-Path $bat.xml)) {
$msj="The source files ""$($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.FullNam e)\$($file .Name).zip "
$MoveFile = "$backupFolder\$($file.Bas eName)_$da te.xml"
try{
$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"
}
catch{
$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
}
}
exit 0
************************** ********** ********** ********** ********** ********** ******
I get this error msg .
PS E:\INT> E:\INT\FileCheck_Revise.ps 1
Test-Path : Cannot bind argument to parameter 'Path' because it is null.
At E:\INT\FileCheck_Revise.ps 1:31 char:16
+ if ((Test-Path $cat.xml) -and (Test-Path $rat.xml) -and (Test-Path $b ...
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nErrorNull NotAllowed ,Microsoft .PowerShel l.Commands .TestPathC ommand
PS E:\INT>
************************** ********** ********** ********** ********** ********** ******
Log file output
2018-09-18 17:09:46 START: File Processing of XML
2018-09-18 17:09:49 COMPRESS: Compressing File "bat.xml" into "E:\INT\PUT\bat.xml.zip" succeeded
2018-09-18 17:09:49 ARCHIVE: Moving File "E:\INT\Archive\bat_180920 18_170946. xml"
2018-09-18 17:09:49 COMPRESS: Compressing File "cat.xml" into "E:\INT\PUT\cat.xml.zip" succeeded
2018-09-18 17:09:49 ARCHIVE: Moving File "E:\INT\Archive\cat_180920 18_170949. xml"
2018-09-18 17:09:49 COMPRESS: Compressing File "rat.xml" into "E:\INT\PUT\rat.xml.zip" succeeded
2018-09-18 17:09:49 ARCHIVE: Moving File "E:\INT\Archive\rat_180920 18_170949. xml"
Actually I want to check if 3 files exists..
If exists it should write the filename with path in the log file.
if not exists then should write missing file in the log and exit.
can help pls..
#Requires -version 3.0
$WarningPreference="Contin
#variables
$ScriptPath = $(Split-Path -Parent $MyInvocation.MyCommand.De
#Log Timestamp
$date=Get-Date -Format ddMMMyyyyHHmmss
$logdate=Get-Date -Format ddMMMyyyy_HHmmss
$Logfile = "E:\INT\LOG\FileProcessing
function LogWrite ([string]$logstring)
{
Add-content $Logfile -value $logstring
}
LogWrite ("$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) START:`t"+"File Processing of XML`n")
#Zip Files and move to backup folder
$sourcefolder="E:\INT\PUT"
$backupFolder="E:\INT\Arch
#check if source folder 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 if source file exists
if ((Test-Path $cat.xml) -and (Test-Path $rat.xml) -and (Test-Path $bat.xml)) {
$msj="The source files ""$($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.FullNam
$MoveFile = "$backupFolder\$($file.Bas
try{
$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"
}
catch{
$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
}
}
exit 0
**************************
I get this error msg .
PS E:\INT> E:\INT\FileCheck_Revise.ps
Test-Path : Cannot bind argument to parameter 'Path' because it is null.
At E:\INT\FileCheck_Revise.ps
+ if ((Test-Path $cat.xml) -and (Test-Path $rat.xml) -and (Test-Path $b ...
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
PS E:\INT>
**************************
Log file output
2018-09-18 17:09:46 START: File Processing of XML
2018-09-18 17:09:49 COMPRESS: Compressing File "bat.xml" into "E:\INT\PUT\bat.xml.zip" succeeded
2018-09-18 17:09:49 ARCHIVE: Moving File "E:\INT\Archive\bat_180920
2018-09-18 17:09:49 COMPRESS: Compressing File "cat.xml" into "E:\INT\PUT\cat.xml.zip" succeeded
2018-09-18 17:09:49 ARCHIVE: Moving File "E:\INT\Archive\cat_180920
2018-09-18 17:09:49 COMPRESS: Compressing File "rat.xml" into "E:\INT\PUT\rat.xml.zip" succeeded
2018-09-18 17:09:49 ARCHIVE: Moving File "E:\INT\Archive\rat_180920
I used variables, if you use filenames do not prepend the $
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Michael ,
thanks for help..
it works.
thanks for help..
it works.
if ((Test-Path $firstfile) -and (Test-Path $secondfile) -and (Test-Path $Thirdfile)) {
Yourcodehere
} else { break }