2017-02-23 35 views
0

我不是Powershell专家,但是你们中的任何一个人都有一些脚本来计算文件夹中的文件并自动发送邮件给用户?我们的用户有漫游配置文件在家中发送文件并发送邮件 - Powershell

(\\轮廓-SRV \%USERNAME%)

文件夹名称相同的用户名。是否可以有一个脚本来计算每个家庭文件夹中的文件并向用户发送电子邮件?

域名:FirmaBis.org用户总数:150

在EX所以计数。 aaba并发送邮件到[email protected] 指望下一个aaca并发送邮件到[email protected]

因此,脚本会根据文件夹名称和+ firmabis.org计算文件并将邮件发送给用户。

谢谢!

+0

你可以提供自己的工作代码示例? SO不是一个脚本写作服务。自己的努力是必需的。我们没有人在这里做别人的(付)工作。 – bluuf

+0

我不会推送任何人给我任何东西。这只是一个简单的问题,如果有人有东西。 –

回答

1

我还没有看到你到目前为止尝试过的任何东西。只是给你一个衬托:

可以使用GET-childitem.Count之间相结合的方法获得的文件数量的列表。

(Get-ChildItem D:\FolderName | measure-object).Count 

您可以将输出存储在变量中。

然后,您可以通过该变量作为BODY发送-MAILMESSAGE与您可以发送电子邮件。

+0

我没有做任何事情,我不知道从哪里开始。谢谢:) –

+0

@DavidJackowiak:接受答案将是可观的。你先试,然后我们在那里引导你。 :) –

+0

“测量对象”对于计数而言是多余的。 '(Get-ChildItem).Count' –

2
# Get just the directories in the user directory share, ignore any files, loop over them 
Get-ChildItem -Path '\\server\share' -Directory | ForEach-Object { 

    # List all the files in the current folder (loop variable $_ is the folder) 
    $FilesInFolder = @($_ | Get-ChildItem -Recurse -Force -File) 

    # Count the files 
    $NumFiles = $FilesInFolder.Count 

    # Calculate how many MB they take up, and show it to 2 decimal places 
    $FileSize = $FilesInFolder.Length | Measure-Object -Sum | Select-ExpandProperty Sum 
    $FileSize = "{0:.0}MB" -f ($FileSize/1MB) 

    # Build the email message 
    $Message = @" 
    Hi, 
    The folder for account ($($_.Name)) has $($FilesInFolder.Count) files in it. 
    They add up to $FileSize 
    "@ 

    # Send the message through an SMTP server which is configured to allow 
    # unauthenticated relay emails from the computer running the script. 
    Send-MailMessage -SmtpServer yourmailserver -To "$($_.Name)@FirmaBis.org" -From '[email protected]' -Body $Message 
} 

未经检验的,但是......