Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Verify backup file exists
- If(-not (Test-Path $outputpath))
- {
- $CustomErrorMessage = "Error creating backup file at " + $outputpath + "`n`n" + "Verify backup path is correct, backup location is available, and that your account has write permission."
- Write-Host $CustomErrorMessage
- EmailError($CustomErrorMessage)
- Continue #Exits the ForEach loop
- }
- #Compare Downloaded File to last existing backup and only keep if different (so you don't have a hundred copies of the same file)
- #Get latest file (excluding the one just downloaded)
- $lastbackupfile = $(Get-ChildItem -Path $outputfolder | Sort-Object LastWriteTime -Descending | Select-Object -Index 1)
- $lastbackupfilepath = $outputfolder + $lastbackupfile
- Try
- {
- If($null -ne $lastbackupfile) #make sure that there actually is an existing backup to compare to
- {
- if(-NOT(Compare-Object -ReferenceObject $(Get-Content $outputpath) -DifferenceObject $(Get-Content $lastbackupfilepath)))
- {
- #Files are the same; no need to keep backup
- Remove-Item -Path $outputpath
- Write-Host("No Changes to Configuration for Switch " + $line.hostname + ".")
- }
- Else
- {
- #Files are different; keep latest backup
- Write-Host New Backup File Created: $outputpath
- }
- }
- else
- {
- #If no previous backup file; keep latest backup
- Write-Host New Backup File Created: $outputpath
- }
- }
- Catch
- {
- $CustomErrorMessage = "Error Comparing previous config for switch " + $line.hostname + "`n`n" + "Error Message is: " + $_.Exception.Message + "`n`n" + "If the object is null it may be you have an empty file in the directory."
- Write-Host $CustomErrorMessage
- EmailError($CustomErrorMessage)
- Continue #Exits the ForEach loop
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement