PowerShell 5 - SFTP System - Trying to create zip file, compress folders into that file with a date stamp, move that zip file to the archive, and then remove files from source

This question is for a PowerShell guru!

I am trying to create an archival script to archive files on an SFTP solution.  This is a Windows 2016 system with PS 5

PS C:\Windows\system32> $PSVersionTable

Name                           Value                                                                                                          
----                           -----                                                                                                          
PSVersion                      5.1.14393.2515                                                                                                 
PSEdition                      Desktop                                                                                                        
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                        
BuildVersion                   10.0.14393.2515                                                                                                
CLRVersion                     4.0.30319.42000                                                                                                
WSManStackVersion              3.0                                                                                                            
PSRemotingProtocolVersion      2.3                                                                                                            
SerializationVersion           1.1.0.1  

Open in new window


I am trying to run this script:

$EITTrain = "C:\test"
$ArchFld = "C:\test\archive"  
$fileAge = 1 
$archiveAge = 1 
  
$logFiles = Get-ChildItem $EITTrain -Filter *.csv | Where LastWriteTime -lt  (Get-Date).AddDays(-1 * $fileAge)  
$destinationPath = $ArchFld   EITTraining_(Get-Date -format "yyyyMMddHHmmss")   ".zip"  
  
$logFilePaths = @()  
  
foreach($logFile in $logFiles){  
    $logFilePaths  = $logFile.FullName  
    }  
  
Compress-Archive -Path $logFilePaths -DestinationPath $destinationPath -CompressionLevel Optimal  
Remove-Item –path $logFilePaths  
  
$archiveFiles = Get-ChildItem $ArchFld -Filter *.zip | Where LastWriteTime -lt  (Get-Date).AddDays(-1 * $archiveAge)  
  
foreach($archiveFile in $archiveFiles){  
    Remove-Item –path $archiveFile.FullName  
}  

Open in new window


and keep receiving this error:

At line:7 char:31
+ $destinationPath = $ArchFld   (Get-Date -format "yyyyMMddHHmmss")   " ...
+                               ~
Unexpected token '(' in expression or statement.
At line:7 char:69
+ ... inationPath = $ArchFld   (Get-Date -format "yyyyMMddHHmmss")   ".zip"
+                                                                    ~~~~~~
Unexpected token '".zip"' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken
 

Open in new window

Can the PowerShell gurus help me out with my script?

Thank you!
Mark RoddySystem AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
Please test this:
$destinationPath = $ArchFld   "EITTraining_"+"(Get-Date -format 'yyyyMMddHHmmss')"+".zip")

Open in new window


If that doesn't work I expect we'll need to add a $ into the mix or construct the partial file name prior to this statement.
Mark RoddySystem AdministratorAuthor Commented:
Here is my latest code:
$EITTrain = "C:\test"
$ArchFld = "C:\test\archive"  
$fileAge = 7  
$archiveAge = 30  
  
$logFiles = Get-ChildItem $EITTrain -Filter *.csv | Where LastWriteTime -lt  (Get-Date).AddDays(-1 * $fileAge)  
#$destinationPath = $ArchFld   (Get-Date -format "yyyyMMddHHmmss")   ".zip"
$destinationPath = $ArchFld   "EITTraining_"+"(Get-Date -format 'yyyyMMddHHmmss')"+".zip"  
  
$logFilePaths = @()  
  
foreach($logFile in $logFiles){  
    $logFilePaths  = $logFile.FullName  
    }  
  
Compress-Archive -Path $logFilePaths -DestinationPath $destinationPath -CompressionLevel Optimal  
Remove-Item –path $logFilePaths  
  
$archiveFiles = Get-ChildItem $ArchFld -Filter *.zip | Where LastWriteTime -lt  (Get-Date).AddDays(-1 * $archiveAge)  
  
foreach($archiveFile in $archiveFiles){  
    Remove-Item –path $archiveFile.FullName  
}  

Open in new window


It errors out here now...getting closer, I think!

At line:8 char:31
+ $destinationPath = $ArchFld   "EITTraining_"+"(Get-Date -format 'yyyy ...
+                               ~~~~~~~~~~~~~~
Unexpected token '"EITTraining_"' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

Open in new window

oBdACommented:
Try the -f operator; I think that'll give the clearest code in this case:
$destinationPath = '{0}\{1}_{2}.{3}' -f $ArchFld, 'EITTraining', (Get-Date -format "yyyyMMddHHmmss"), "zip"

Open in new window

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

aikimarkCommented:
or plain old concatenation:
$ArchFld + "EITTraining_"

Open in new window

oBdACommented:
That is too much like VBScript to be funny.
Using subexpressions would work, too, but in this case, I'd prefer the -f from above:
"$($ArchFld)\EITTraining_$(Get-Date -format 'yyyyMMddHHmmss').zip"

Open in new window

Mark RoddySystem AdministratorAuthor Commented:
Ok...A little farther.  Thanks guys for the assist.  Not quite there yet.
$EITTrain = "C:\test"
$ArchFld = "C:\test\archive"  
$fileAge = 7  
$archiveAge = 30  
  
$logFiles = Get-ChildItem $EITTrain -Filter *.csv | Where LastWriteTime -lt  (Get-Date).AddDays(-1 * $fileAge)  
#$destinationPath = $ArchFld   (Get-Date -format "yyyyMMddHHmmss")   ".zip"
#$destinationPath = $ArchFld   "EITTraining_"+"(Get-Date -format 'yyyyMMddHHmmss')"+".zip"  
$destinationPath = '{0}\{1}_{2}.{3}' -f $ArchFld, 'EITTraining', (Get-Date -format "yyyyMMddHHmmss"), "zip"

$logFilePaths = @()  
  
foreach($logFile in $logFiles){  
    $logFilePaths  = $logFile.FullName  
    }  
  
Compress-Archive -Path $logFilePaths -DestinationPath $destinationPath -CompressionLevel Optimal  
Remove-Item –path $logFilePaths  
  
$archiveFiles = Get-ChildItem $ArchFld -Filter *.zip | Where LastWriteTime -lt  (Get-Date).AddDays(-1 * $archiveAge)  
  
foreach($archiveFile in $archiveFiles){  
    Remove-Item –path $archiveFile.FullName  
}  

Open in new window


Errors out with this...

Compress-Archive : Cannot validate argument on parameter 'Path'. The argument is null, empty, or an element of the argument collection 
contains a null value. Supply a collection that does not contain any null values and then try the command again.
At line:17 char:24
+ Compress-Archive -Path $logFilePaths -DestinationPath $destinationPat ...
+                        ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Compress-Archive], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Compress-Archive
 
Remove-Item : Cannot bind argument to parameter 'Path' because it is an empty array.
At line:18 char:19
+ Remove-Item –path $logFilePaths
+                   ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Remove-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyArrayNotAllowed,Microsoft.PowerShell.Commands.RemoveItemCommand

Open in new window

oBdACommented:
Somewhat rearranged; the "destructive" commands are in test mode; remove the "-WhatIf" in lines 11 and 14 to run it for real.
$EITTrain = "C:\test"
$ArchFld = "C:\test\archive"
$fileAge = 1
$archiveAge = 1
  
If ($logFileItems = Get-ChildItem $EITTrain -Filter *.csv | Where-Object LastWriteTime -lt (Get-Date).AddDays(-1 * $fileAge)) {
	$destinationPath = '{0}\{1}_{2}.{3}' -f $ArchFld, 'EITTraining', (Get-Date -format "yyyyMMddHHmmss"), "zip"
	$logFilePaths = $logFileItems | Select-Object -ExpandProperty FullName
	Try {
		Compress-Archive -Path $logFilePaths -DestinationPath $destinationPath -CompressionLevel Optimal -ErrorAction Stop
		$logFileItems | Remove-Item -Verbose -WhatIf
		Get-ChildItem -Path $ArchFld -Filter *.zip |
			Where-Object LastWriteTime -lt  (Get-Date).AddDays(-1 * $archiveAge) |
			Remove-Item -Verbose -WhatIf
	} Catch {
		Throw
	}
} Else {
	Write-Warning "Found no files to archive!"
}

Open in new window

Mark RoddySystem AdministratorAuthor Commented:
It worked! Sweet! I had to add a csv file to the source, set it to -1 days, and whalla...worked like a champ.  

Thank you guys for the help!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark RoddySystem AdministratorAuthor Commented:
One more question guys.  What if there is multiple files in the source?  Not just one?  

1. Add all files from the source into the zip file
2. Remove all files from the source directory

$EITTrain = "C:\test"
$ArchFld = "C:\test\archive"
$fileAge = 1
$archiveAge = 1
  
If ($logFileItems = Get-ChildItem $EITTrain -Filter *.csv | Where-Object LastWriteTime -lt (Get-Date).AddDays(-1 * $fileAge)) {
	$destinationPath = '{0}\{1}_{2}.{3}' -f $ArchFld, 'EITTraining', (Get-Date -format "yyyyMMddHHmmss"), "zip"
	$logFilePaths = $logFileItems | Select-Object -ExpandProperty FullName
	Try {
		Compress-Archive -Path $logFilePaths -DestinationPath $destinationPath -CompressionLevel Optimal -ErrorAction Stop
		$logFileItems | Remove-Item -Verbose
		Get-ChildItem -Path $ArchFld -Filter *.zip |
			Where-Object LastWriteTime -lt  (Get-Date).AddDays(-1 * $archiveAge) |
			Remove-Item -Verbose
	} Catch {
		Throw
	}
} Else {
	Write-Warning "Found no files to archive!"
}

Open in new window

Mark RoddySystem AdministratorAuthor Commented:
I figured it out..thanks guys!

#Set variables
$EITTrain = "C:\test"
$ArchFld = "C:\test\archive"  
$ExtFilter = "*.csv"
$ExtFilter2 = "*.zip"
$fileAge = -1  
$archiveAge = 15  

If ($logFileItems = Get-ChildItem $EITTrain -Filter $ExtFilter | Where-Object LastWriteTime -lt (Get-Date).AddDays(-1 * $fileAge)) {
	$destinationPath = '{0}\{1}_{2}.{3}' -f $ArchFld, 'EITTraining', (Get-Date -format "yyyyMMddHHmmss"), "zip"
	$logFilePaths = $logFileItems | Select-Object -ExpandProperty FullName
	Try {
		Compress-Archive -Path $logFilePaths -DestinationPath $destinationPath -CompressionLevel Optimal -ErrorAction Stop
		$logFileItems | Remove-Item -Verbose
		Get-ChildItem -Path $ArchFld -Filter $ExtFilter2 |
			Where-Object LastWriteTime -lt  (Get-Date).AddDays(-1 * $archiveAge) |
			Remove-Item -Verbose
	} Catch {
		Throw
	}
} Else {
	Write-Warning "Found no files to archive!"
}

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.