检查多个文件夹中的新文件,报告新文件是否存在

时间:2015-02-04 21:34:18

标签: powershell-v2.0

我是Powershell 2.0的2天新手。我已经搜索到了这么远,但我仍然坚持做进一步的事情。请耐心等我试着去搜索!

任务:创建一个PS文件,搜索文件夹中的子文件夹。每个文件夹包含一个站点备份文件。我需要知道是否创建了一个新文件,并报告该文件是否已创建。

PS文件需要执行以下操作:

  1. 检查文件夹
  2. 如果存在新文件,请说" ok"否则"不行"
  3. 为28个文件夹创建28个语句
  4. 执行此操作
  5. 将写入输出转储到公共功能(电子邮件)
  6. 使用电子邮件正文中的电子邮件数据。
  7. 问题:

    1. 一次搜索就行了,当我添加另一个时,我会得到错误的答案。我怀疑这与一遍又一遍地使用相同的变量有关。

    2. 我仍然对如何让回车在电子邮件正文中工作有点迷失。一切都在一条线上。

    3. 我怀疑我的创作中有不必要的陈述。该文件是我在这里找到的点点滴滴。我无法正确地将它放在一起以满足我的需求。非常感谢指导。

      #Site #1
      $fullPath = "\\10.10.1.5\working\$folder"
      $folder1= "test1"
      $numdays = 0
      $numhours = 10
      $nummins = 0
      $site = "site 1"
      
      function Verify1($path, $days, $hours, $mins)
      {$files = @(get-childitem $path -include *.* -recurse | where {($_.LastWriteTime -gt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) -and ($_.psIsContainer -eq $false)})
      if ($files -ne $NULL)
      {$file = $files
      write-host ("The backup File for " + $site + " was backed up successfully!!")}
      else {write-host ("The backup File for " + $site + " WAS NOT backed up successfully!!")}}
      Verify1 $fullPath $numdays $numhours $nummins
      
      #Site #2
      $fullPath = "\\10.10.1.5\working\$folder"
      $folder2= "test2"
      $numdays2 = 0
      $numhours2 = 10
      $nummins2 = 0
      $site = "site 2"
      
      function Verify2($path, $days, $hours, $mins)
      {$files = @(get-childitem $path -include *.* -recurse | where {($_.LastWriteTime -gt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) -and ($_.psIsContainer -eq $false)})
      if ($files -ne $NULL)
      {$file = $files
      write-host ("The backup File for " + $site + " was backed up successfully!!")}
      else {write-host ("The backup File for " + $site + " WAS NOT backed up successfully!!")}}
      Verify2 $fullPath2 $numdays $numhours $nummins
      
      function email {
      process { Write-Host $_ }
      }
      
      #verify1 | email
      #verify2 | email
      
      $date = (Get-Date).ToString('MM/dd/yyyy')
      
      $EmailFrom = "email@gmail.com"
      $EmailTo = "email@gmail.com" 
      $Subject = ("Daily Database Report for " + $date)
      [string]$body = ""
      
      
      
      foreach ($email in $emails) {$body = $body + "`r`n"}
      
      
      
      $SMTPServer = "smtp.gmail.com" 
      $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
      $SMTPClient.EnableSsl = $true 
      $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("user", "password"); 
      $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
      

1 个答案:

答案 0 :(得分:0)

问题1

问题看起来像来自具有相同名称的变量。 如果你查看站点2,它有完整的路径查看$ folder,这两个配置块中都不存在。

这样的东西
#Config Block
#Folders
$folder1= "test1"
$folder2= "test2"

#Remote Paths
$fullPath1 = "\\10.10.1.5\working\$folder1"
$fullPath2 = "\\10.10.1.5\working\$folder2"

#Site Names
$site1 = "Site 1"
$site2 = "Site 2"

#Time Difference
$numdays = 0
$numhours = 10
$nummins = 0

#Function
function VerifySite($Path, $Site, $Days, $Hours, $Mins)
{$files = @(get-childitem $path -include *.* -recurse | where {($_.LastWriteTime -gt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) -and ($_.psIsContainer -eq $false)})
if ($files -ne $NULL)
{$file = $files
write-host ("The backup File for " + $site + " was backed up successfully!!")}
else {write-host ("The backup File for " + $site + " WAS NOT backed up successfully!!")}}


VerifySite $fullPath1 $Site1 $numdays $numhours $nummins
VerifySite $fullPath2 $Site2 $numdays $numhours $nummins

可能会归档所需的结果。清理配置,以便更容易阅读,并删除第二个验证功能,因为它不是必需的。

有机会复活这个并看问题2。

问题2.

电子邮件正文。

我重做了一下以生成日志文件。然后我从日志文件构建了电子邮件正文,最后删除了日志文件

#Config Block
#Folders
$folder1= "test1"
$folder2= "test2"
$folder3= "test3"
$folder4= "test4"
$folder5= "test5"

#Remote Paths
$fullPath1 = "\\10.10.1.5\working\$folder1"
$fullPath2 = "\\10.10.1.5\working\$folder2"
$fullPath3 = "\\10.10.1.5\working\$folder3"
$fullPath4 = "\\10.10.1.5\working\$folder4"
$fullPath5 = "\\10.10.1.5\working\$folder5"

#Site Names
$site1 = "Site 1"
$site2 = "Site 2"
$site3 = "Site 3"
$site4 = "Site 4"
$site5 = "Site 5"

#Time Difference
#Days
$numdays1 = 0
$numdays2 = 1
#Hours
$numhours1 = 10
$numhours2 = 15
#Mins
$nummins1 = 0
$nummins2 = 0

#Dates
$date = (Get-Date).ToString('MM/dd/yyyy')
$Logdate = (Get-Date).ToString('MMddyyyy')

#logs
$logFile = "D:\Log_$Logdate.txt"

#Email
$EmailFrom = "email@gmail.com"
$EmailTo = "email@gmail.com" 
$Subject = ("Daily Database Report for " + $date)

#Function
function VerifySite($Path, $Site, $Days, $Hours, $Mins)
{$files = @(get-childitem $path -include *.* -recurse | where {($_.LastWriteTime -gt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) -and ($_.psIsContainer -eq $false)})
if ($files -ne $NULL)
{$file = $files
write-output ($Date + " The backup File for " + $site + " was backed up successfully!!") | Out-File $logFile -Append }
else {write-output ($Date + " The backup File for " + $site + " WAS NOT backed up successfully!!") | Out-File $logFile -Append}}

#Commands 
VerifySite $fullPath1 $Site1 $numdays2 $numhours1 $nummins1
VerifySite $fullPath2 $Site2 $numdays1 $numhours2 $nummins2
VerifySite $fullPath3 $Site3 $numdays1 $numhours1 $nummins1
VerifySite $fullPath4 $Site4 $numdays1 $numhours2 $nummins1
VerifySite $fullPath5 $Site5 $numdays2 $numhours1 $nummins2

[string]$body = ""
$logs = Get-Content $logFile

foreach ($log in $logs )
{
    $body = $body + $log + "`r`n"
}

$SMTPServer = "smtp.gmail.com" 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("user", "password"); 
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

#Cleanup if required.
remove-item $logFile

你应该让$ body返回类似

的东西
11/02/2015 The backup File for Site 1 WAS NOT backed up successfully!!
11/02/2015 The backup File for Site 2 was backed up successfully!!

还对变量进行了重新设计,以显示它是如何扩展的。