Первый постер здесь.
Я начал писать сценарии с помощью PowerShell, и я очень новичок в этом, однако я понимаю, как это может быть очень полезно для меня.
Я пытаюсь проверить, существуют ли файлы. Если файл существует, добавьте его в переменную / список $ Successful.
Если файл не существует, добавьте его в $ Failed.
Вот полный сценарий:
#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 ("alerts@domain.com", $secpasswd)
If ($Successful -lt $Total){
$SuccessMailParams = @{
To = "myemailaddress@domain.com"
From = "alerts@domain.com"
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 = "myemailaddress@domain.com"
From = "alerts@domain.com"
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
}
Что возвращено:
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
Почему во втором $ Destination отображается «Success». и "False".
Это ложное назначение должно быть добавлено к $ Failed.