PowerShell脚本循环 - 创建警报新文件

时间:2016-08-17 00:59:10

标签: csv powershell

我希望有PowerShell经验的人能够如此友好地建议(如果可能的话)我们如何循环这个脚本。不幸的是,我们还不太熟悉PowerShell脚本循环,并寻找一些建议,这是最好的方法/方法。我们使用下面的工作脚本,在指定日期开始在所提到的文件夹和/或文件夹中创建新文件时向员工发送电子邮件警报,并且它包含列出该文件的.csv文件,然后将其删除。我们不需要在发送警报后保留生成的.csv文件。现在我们一遍又一遍地复制和粘贴代码,并在添加新文件夹时修改部分代码。但是,我们担心的是,随着周/月/年过去添加文件夹并且必须添加新文件夹,这个文件最终会长一页。

理想情况下,我们对脚本所做的唯一更改是在网络驱动器中查看的文件夹路径,创建时间以及它将要访问的收件人。

任何关于以最佳方式实现所有或任何此类建议的建议都非常受欢迎。

非常感谢!!!

$arr = @()
$file = "\\DesignatedNetworkDrive\NewFileFinderReport.csv"
$mesg = "Please see the attached file for a list of files that were created after July 1, 2016."


# Folder: Corporateshare

gci \\DesignatedNetworkDrive\,\\DesignatedNetworkDrive\Corporateshare\  | Where-Object { $_.CreationTime -ge "07/02/2016" } | % {
  $obj = New-Object PSObject
  $obj | Add-Member NoteProperty FullName $_.FullName
  $obj | Add-Member NoteProperty Length $_.Length
  $obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
  $obj | Add-Member NoteProperty LastAccessTime $_.LastAccessTime
  $obj | Add-Member NoteProperty LastWriteTime $_.LastWriteTime
  $obj | Add-Member NoteProperty CreationTime $_.CreationTime

  $arr += $obj
  }
  $arr | Export-CSV -notypeinformation "$file"



if ($(get-item -path "$file").length -gt 0) {send-mailmessage -from "sysadmin@something.com" -to "jendoe@something.com","johndoe@something.com" -subject "New File Found in Corporateshare" -body "$mesg" -Attachments "$file" -smtpServer mail.somecompany.com}

Remove-Item $file



# Folder: Administration
$arr = @()
$file = "\\DesignatedNetworkDrive\NewFileFinderReport.csv"
$mesg = "Please see the attached file for a list of files that were created after May 3, 2016."


gci \\DesignatedNetworkDrive\Administration\,\\DesignatedNetworkDrive\Administration\_Private\  | Where-Object { $_.CreationTime -ge "05/04/2016" } | % {
  $obj = New-Object PSObject
  $obj | Add-Member NoteProperty FullName $_.FullName
  $obj | Add-Member NoteProperty Length $_.Length
  $obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
  $obj | Add-Member NoteProperty LastAccessTime $_.LastAccessTime
  $obj | Add-Member NoteProperty LastWriteTime $_.LastWriteTime
  $obj | Add-Member NoteProperty CreationTime $_.CreationTime

  $arr += $obj
  }
  $arr | Export-CSV -notypeinformation "$file"



if ($(get-item -path "$file").length -gt 0) {send-mailmessage -from "jendoe@something.com","johndoe@something.com" -body "$mesg" -Attachments "$file" -smtpServer mail.somecompany.com}

Remove-Item $file


# Folder: Procurement
$arr = @()
$file = "\\DesignatedNetworkDrive\NewFileFinderReport.csv"
$mesg = "Please see the attached file for a list of files that were created after May 24, 2016."


gci \\DesignatedNetworkDrive\Procurement\  | Where-Object { $_.CreationTime -ge "05/25/2016" } | % {
  $obj = New-Object PSObject
  $obj | Add-Member NoteProperty FullName $_.FullName
  $obj | Add-Member NoteProperty Length $_.Length
  $obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
  $obj | Add-Member NoteProperty LastAccessTime $_.LastAccessTime
  $obj | Add-Member NoteProperty LastWriteTime $_.LastWriteTime
  $obj | Add-Member NoteProperty CreationTime $_.CreationTime

  $arr += $obj
  }
  $arr | Export-CSV -notypeinformation "$file"



if ($(get-item -path "$file").length -gt 0) {send-mailmessage -from "jendoe@something.com","johndoe@something.com" -subject "New File Found in Procurement" -body "$mesg" -Attachments "$file" -smtpServer mail.somecompany.com}

Remove-Item $file

1 个答案:

答案 0 :(得分:5)

您需要将程序抽象为函数

将一段PowerShell代码转换为有用的功能的关键是识别它的变量部分 - 这些应该变成参数然后你可以传递调用它时的功能。

查看你的代码,很少有部分变量,即:

  • 创作日期
  • 文件夹路径

此外,您可能希望允许更改电子邮件收件人,邮件主题和SMTP服务器 - 这些可能在将来发生变化,或者出于特定目的而发生偏差。

考虑到这一点,我们最终得到的结论是:

function Check-NewFiles 
{
    param(
        [Parameter(Mandatory)]
        [string[]]$Path,
        [Parameter(Mandatory)]
        [datetime]$CreatedAfter,
        [string]$Subject = "New files found",
        [string[]]$Recipients = @("jendoe@something.com","johndoe@something.com"),
        [string]$MailFrom = "noreply@something.com",
        [string]$SmtpServer
    )

    # Folder: Administration
    $arr = @()
    $file = "\\DesignatedNetworkDrive\NewFileFinderReport.csv"
    $mesg = "Please see the attached file for a list of files that were created after {0:MMM dd, yyyy}" -f $CreatedAfter

    Get-ChildItem $Path  | Where-Object { $_.CreationTime -ge $CreatedAfter } | ForEach-Object {
        $obj = New-Object PSObject
        $obj | Add-Member NoteProperty FullName $_.FullName
        $obj | Add-Member NoteProperty Length $_.Length
        $obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
        $obj | Add-Member NoteProperty LastAccessTime $_.LastAccessTime
        $obj | Add-Member NoteProperty LastWriteTime $_.LastWriteTime
        $obj | Add-Member NoteProperty CreationTime $_.CreationTime

        $arr += $obj
    }
    $arr | Export-CSV -notypeinformation "$file"



    if ($(get-item -path "$file").length -gt 0) {
        Send-MailMessage -From $MailFrom -body "$mesg" -Attachments "$file" -smtpServer $SmtpServer -To $Recipients
    }

    Remove-Item $file
}

现在您可以每次使用一行代码调用它:

Check-NewFiles -Path \\DesignatedNetworkDrive\,\\DesignatedNetworkDrive\Corporateshare\ -CreatedAfter '07/02/2016'
Check-NewFiles -Path \\DesignatedNetworkDrive\Administration\,\\DesignatedNetworkDrive\Administration\_Private\ -CreatedAfter '05/04/2016'

对于具有大量参数的同一调用的许多实例,我喜欢使用参数参数设置哈希表的集合 - 这样我可以轻松地遍历整个集合并在{{3}时调用函数参数:

$FoldersToWatch = @(
    # Folder: Corporateshare
    @{
        Path = '\\DesignatedNetworkDrive\','\\DesignatedNetworkDrive\Corporateshare\'
        CreatedAfter = "07/02/2016"
        MailFrom = "sysadmin@something.com" 
        Recipients = "jendoe@something.com","johndoe@something.com"
    },

    # Folder: Administration
    @{
        Path = '\\DesignatedNetworkDrive\Administration\','\\DesignatedNetworkDrive\Administration\_Private\'
        CreatedAfter = "05/04/2016"
        Recipients = "jendoe@something.com","johndoe@something.com"
    },

    # Folder: Procurement
    @{
        Path = '\\DesignatedNetworkDrive\Procurement\'
        CreatedAfter = "05/25/2016"
        Subject = "Procurement, IMPORTANT!"
    }
)

现在,将上面的内容放在自己的文件中,称之为FoldersToWatch.ps1

然后,在定义Check-NewFiles函数的脚本中,点源FoldersToWatch.ps1文件,然后遍历哈希表并调用函数:

# function Check-NewFiles goes up here

. .\FoldersToWatch.ps1

$FoldersToWatch |Foreach-Object {
    Check-NewFiles @_
}

你有它。现在任何人都可以更新FoldersToWatch.ps1中的参数而不会弄乱脚本本身而不会忽视正在发生的事情。

我还想指出,使用New-ObjectAdd-Member创建新对象然后将其添加到数组的方式完全没必要。使用Select-Object并将其直接发送到Export-Csv

Get-ChildItem $Path |Where-Object { $_.CreationTime -ge $CreatedAfter } |Select-Object FullName,Length,@{Label='Owner';Expression={(Get-Acl $_.FullName).Owner}},LastAccessTime,LastWriteTime,CreationTime |Export-CSV -notypeinformation "$file"

导致最终功能看起来更像:

function Check-NewFiles 
{
    param(
        [Parameter(Mandatory)]
        [string[]]$Path,
        [Parameter(Mandatory)]
        [datetime]$CreatedAfter,
        [string]$Subject = "New files found",
        [string[]]$Recipients = @("jendoe@something.com","johndoe@something.com"),
        [string]$MailFrom = "noreply@something.com",
        [string]$SmtpServer
    )

    $file = "\\DesignatedNetworkDrive\NewFileFinderReport.csv"
    $mesg = "Please see the attached file for a list of files that were created after {0:MMM dd, yyyy}" -f $CreatedAfter

    Get-ChildItem $Path |Where-Object { $_.CreationTime -ge $CreatedAfter } |Select-Object FullName,Length,@{Label='Owner';Expression={(Get-Acl $_.FullName).Owner}},LastAccessTime,LastWriteTime,CreationTime |Export-CSV -notypeinformation "$file"

    if ($(get-item -path "$file").length -gt 0) {
        Send-MailMessage -From $MailFrom -body "$mesg" -Attachments "$file" -smtpServer $SmtpServer -To $Recipients
    }

    Remove-Item $file
}