Reputation: 55
First time poster here.
I've begun scripting some things with PowerShell, and I'm very new to it, however I can see how it could be very beneficial to me.
I am trying to verify that files exist. If a file exists, add it to the $Successful variable/list.
If the file does not exist, add it to $Failed.
Here is the full script:
#Set Date Format
$Date = Get-Date -f _MM_dd_yyyy
$UnifiDate = Get-date -f yyyyMMdd
#Set FilePaths
$SourceDirectory = "C:\TFTP-Images"
$CheckPointFileName = Get-ChildItem -Filter "Gateway*" -Path
$SourceDirectory
$CheckPointDestination = "C:\TFTP-Images\Check Point\"
$CheckPointFile = "C:\TFTP-Images\Check Point\$CheckPointFileName"
$SwitchStackFile = "C:\TFTP-Images\Switch Stack\Switch_Stack_Startup-
Config$Date"
$LanRouterFile = "C:\TFTP-Images\LAN Router\LAN_Router_Startup-
Config$Date"
$GCASAFile = "C:\TFTP-Images\GC ASA\GC_ASA_Startup-Config$Date"
$GCASABackup = "C:\TFTP-Images\GC ASA\GC_ASA_Backup$Date"
$CorpASAFile = "C:\TFTP-Images\Corp ASA\Corp_ASA_Startup-Config$Date"
$CorpASABackup = "C:\TFTP-Images\Corp ASA\Corp_ASA_Daily_Backup$Date"
$PhoneASAFile = "C:\TFTP-Images\Phone ASA\Phone_ASA_Startup-Config$Date"
$PhoneASABackup = "C:\TFTP-Images\Phone ASA\Phone_ASA_Daily_Backup$Date"
$DHCPBackup_DC01 = "C:\TFTP-Images\DHCP Backups\DHCP01\Backup$Date"
$DHCPBackup_DC02 = "C:\TFTP-Images\DHCP Backups\DHCP02\Backup$Date"
$IIS_INETConfig = "C:\TFTP-Images\IIS_Backups\SYNCBOX\C Drive\Windows\System32\inetsrv\Backup\IIS_Config_Backup$Date"
$IIS_Backup = "C:\TFTP-Images\IIS_Backups\SYNCBOX\C Drive\Windows\System32\inetsrv\Backup\inetpub_Backup$Date"
$NPSBackup_RADIUS = "C:\TFTP-Images\NPS_Config\RADIUS1\RADIUS1_NPS_Backup$Date.xml"
$NPSBackup_GCDC1 = "C:\TFTP-Images\NPS_Config\RADIUS2\RADIUS2_Backup$Date.xml"
$UnifiBackupDestination = "C:\TFTP-Images\Unifi Backups\"
$UnifiFileName = Get-Childitem -Filter "*$UnifiDate*" -Path $UnifiBackupDestination -Name
$UnifiDate = Get-date -f yyyyMMdd
$UnifiFile = ""
If ($UnifiFileName -contains "*.unf"){
$UnifiFile = "C:\TFTP-Images\Unifi Backups\$UnifiFileName"
}
Else { $UnifiFile = "C:\invalid"}
#Setup Array
$Destinations = @($CheckPointFile,$UnifiFile,$SwitchStackFile,$LanRouterFile,$GCASAFile,$GCASABackup,$CorpASAFile,$CorpASABackup,$PhoneASAFile,$PhoneASABackup,$DHCPBackup_DC01,$DHCPBackup_DC02,$IIS_INETConfig,$IIS_Backup,$NPSBackup_GCDC1,$NPSBackup_RADIUS)
$Successful = 0
$Failed = 0
$Exists = Test-Path -Path "$Destination"
$Total = 0
ForEach ($Destination in $Destinations)
{#Get Totals
$Total = $Total + 1
Test-Path -Path $Destination
If ($Exists -eq $True)
{
write-host "Success."
$Successful = $Successful + 1
}
Else
{
write-host $Destination "Has Failed"
$Failed = $Failed + 1
}
}
$Total
$Successful
$Failed
#Sends Email
$secpasswd = ConvertTo-SecureString mypassword -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ("[email protected]", $secpasswd)
If ($Successful -lt $Total){
$SuccessMailParams = @{
To = "[email protected]"
From = "[email protected]"
Subject = "Network Device Backup Failure"
Body = "The backups Failed.
$Successful of $Total were OK, but $Failed failed to copy."
}
Send-MailMessage @SuccessMailParams -Port 587 -SmtpServer smtp.domain.com -UseSsl -Credential $creds
} Else {
$SuccessMailParams = @{
To = "[email protected]"
From = "[email protected]"
Subject = "Network Device Backup Successful"
Body = "The backups were successful.
$Successful of $Total items copied successfully."
}
Send-MailMessage @SuccessMailParams -Port 587 -SmtpServer smtp.domain.com -UseSsl -Credential $creds
}
What's returned:
True
Success.
False
Success.
True
Success.
True
Success.
True
Success.
True
Success.
True
Success.
True
Success.
True
Success.
True
Success.
True
Success.
True
Success.
True
Success.
True
Success.
True
Success.
True
Success.
16
16
0
Why is the 2nd $Destination showing "Success." and "False".
That false destination should be added to $Failed.
Upvotes: 0
Views: 77
Reputation: 7780
There might be something wrong with your script, or you haven't included the full script.
This is what I was able to infer:
function CheckFolder {
param($folder)
$Total = 0
$Successful = 0
$Failed = 0
$Exists = Test-Path -Path $folder
$Destinations = Get-ChildItem $folder
ForEach ($Destination in $Destinations)
{
#Get Totals
$Total = $Total + 1
$Exists = Test-Path -Path $Destination
If ($Exists -eq $True)
{
write-host "Success: $Destination"
$Successful = $Successful + 1
}
Else {
write-host "Fail: $Destination"
$Failed = $Failed + 1
}
}
$Total
$Successful
$Failed
}
CheckFolder .
Basically, you have an issue in your Else {($Exists -eq $False)
statement. Get rid of ($Exists -eq $False)
.
Upvotes: 1