tecgate
asked on
Powershell - Create file from foreach when file counts greater than 250
$fso = New-Object -com "Scripting.FileSystemObject"
$f = $fso.GetFolder("D:\websphere\logs")
I have a directory called D:\websphere\logs, and D:\websphere\logs has many of sub-directories. Each sub-directory doesn't any child sub-directory, so the directory structure is:
D:\websphere\logs
\aaa
\bbb
\ccc
\ddd
etc etc
I need to count the files under each sub-directory, and write to a .txt file when the file count of each sub-folder is greater than 250.
I have the script below, but not working really.
foreach ($folder in $f.subfolders)
{
Write-Host $((get-childitem $folder.path).count)','$folder.Path
New-Object -TypeName PSObject -Property @{
SystemName = $((get-childitem $folder.path).count)
Reachable = $folder.Path
} | Export-Csv -path D:\websphere\output.txt -Append
$data = import-csv -path D:\websphere\output.txt -header close, server
$data | where-object{$_.close -gt 250} |format-table -autosize
The count is working fine, but I can't make the next piece (write file count to a .txt file when the file count of each sub-folder is greater than 250).
So, my working segment so far is:
$fso = New-Object -com "Scripting.FileSystemObject"
$f = $fso.GetFolder("C:\Windows\Media")
foreach ($folder in $f.subfolders){Write-Host $((get-childitem $folder.path).count)','$folder.Path}
Please let me know how to make this piece (write file count to a .txt file when the file count of each sub-folder is greater than 250) working.
Appreciate it!
ASKER
Thank you so much, Bob!
I am using PS Version 4. Your script generates an output.
Ultimately, I need to accomplish these two things:
- Sort the output by descending order of the counts so that the largest count is on the top
- Send out e-mail alerts to e-mail recipients via a scheduler
The 2nd question would be another post, but can you please let me know how to sort first?
Appreciate it!
I am using PS Version 4. Your script generates an output.
Ultimately, I need to accomplish these two things:
- Sort the output by descending order of the counts so that the largest count is on the top
- Send out e-mail alerts to e-mail recipients via a scheduler
The 2nd question would be another post, but can you please let me know how to sort first?
Appreciate it!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also, what outbound email system are you using?
ASKER
Thank you, Bob!
The sorting works just charming.
I am using MS Outlook. The alert e-mail will be using all e-mails inside network.
Please advise.
The sorting works just charming.
I am using MS Outlook. The alert e-mail will be using all e-mails inside network.
Please advise.
No, what server are you using for outbound email?
ASKER
OK, I am using exmail.webarp.com (Exchange Server). Is it what you are asking?
Thank you!
Thank you!
ASKER
I opened the mail question at #28932187.
Thank you
Thank you
This is also being addressed at powershell.com.
Open in new window