简体   繁体   English

Powershell脚本:列出具有特定更改日期的文件(可能的话,数量)

[英]Powershell script: List files with specific change date (Amount if possible)

For license porpuses I try to automate the counting process instead of having to login into every single server, go into directory, search a file name and count the results based on the change date. 对于许可证文件,我尝试自动进行计数过程,而不必登录到每个服务器,进入目录,搜索文件名并根据更改日期对结果进行计数。

Want I'm aiming for: Running a powershell script every month that checks the directory "C:\\Users" for the file "Outlook.pst" recursively. 我是否要针对:每个月运行一个Powershell脚本,以递归方式在目录“ C:\\ Users”中检查文件“ Outlook.pst”。 And then filters the result by change date (one month or newer). 然后按更改日期(一个月或更晚)过滤结果。 Then packing this into an email to send to my inbox. 然后将其打包到电子邮件中以发送到我的收件箱。

I'm not sure if that's possible, cause I am fairly new to powershell. 我不确定这是否可行,因为我对Powershell相当陌生。 Would appreciate your help! 感谢您的帮助!

It is possible. 有可能的。

I dont know how to start a ps session on a remote computer, but I think the cmdlet Enter-PSSession will do the trick. 我不知道如何在远程计算机上启动ps会话,但是我认为cmdlet Enter-PSSession可以解决问题。 Or at least it was the first result while searching for "open remote powershell session". 或至少它是搜索“打开远程Powershell会话”时的第一个结果。 If that does not work use the Invoke-Command as suggested by lit to get $outlookFiles as suggested below. 如果这样不起作用,请按lit的建议使用Invoke-Command来获取$ outlookFiles,如下所示。

For the rest use this. 其余使用此。

$outlookFiles = Get-ChildItem -Path "C:\Users" -Recurse | Where-Object { $_.Name -eq "Outlook.pst" }

Now you have all files that have this name. 现在,您拥有了所有具有该名称的文件。 If you are not familiar with the pipe in powershell it redirects all objects it found with the Get-ChildItem to the next pipe section and here the Where-Object will filter the received objects. 如果您不熟悉Powershell中的管道,它将使用Get-ChildItem找到的所有对象重定向到下一个管道部分,此处Where-Object将过滤接收到的对象。 If the current object ($_) will pass the condition it is returned by the whole command. 如果当前对象($ _)将通过条件,则它将由整个命令返回。

Now you can filter these objects again to only include the latest ones with. 现在,您可以再次过滤这些对象,使其仅包含最新对象。

$latestDate = (Get-Date).AddMonths(-1)
$newFiles = $outlookFiles | Where-Object { $_.LastAccessTime -gt $latestDate }

Now you have all the data you want in one object. 现在,您可以在一个对象中拥有所需的所有数据。 Now you only have to format this how you like it eg you could use $mailBody = $newFiles | Out-String 现在,您只需$mailBody = $newFiles | Out-String自己喜欢的方式对其进行格式化,例如,可以使用$mailBody = $newFiles | Out-String $mailBody = $newFiles | Out-String and then use Send-MailMessage -To x@yz -From r@gb -Body $mailBody to send the mail. $mailBody = $newFiles | Out-String ,然后使用Send-MailMessage -To x@yz -From r@gb -Body $mailBody发送邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM