Good afternoon,
I am trying to solve the problem and already so tired to look for a solution
You need:
1. Get a list of machines from OU
2. Scan each drive for files
3. Get the mail address of the user from AD to a remote computer
4. Send scan results to the post office box of the user and administrator
5. Run all this in a variety of flow-parallel
Searching I found, Invoke-Parallel
https://gallery.technet.microsoft.com/scriptcenter/Run-Parallel-Parallel-377fd430
The script works fine if he is looking for files - create a report for a single computer
When the script runs with the list, miracles begin. Come reports only one computer from the list (for the rest does not come), or do not come at any recipients. Also, if you use the file search as C:\* e scan result is duplicated in the report file. If you
specify a UNC path \\servername\c$ log files fill the correct data.
Here is what I got and what I have is not working:
$OU = 'ou=test,dc=root,dc=local'
$Throttle = 10
(Get-ADComputer -Filter * -SearchBase $OU).Name | Out-File $env:TEMP\Servers.txt
$servers = Get-Content $env:TEMP\Servers.txt
Invoke-Parallel -InputObject $servers -Throttle 10 -ScriptBlock {
if($ping = Test-Connection -ComputerName $_ -BufferSize 16 -quiet -count 2)
{
Try {
Get-WMIObject Win32_LogicalDisk -filter "DriveType = 3" -ComputerName localhost | select DeviceID | %{(Get-ChildItem ($_.DeviceID + '\') -Include *.doc -Recurse -Force -ErrorAction Ignore).FullName} | Out-File $env:TEMP\$_.txt
$username=((gwmi win32_computersystem -computer $_).UserName -split '\\')[1]
if($user=Get-AdUser $username -properties mail){
$email=$user.mail
Send-MailMessage -SmtpServer smtpserver.root.local -to admin@email.com -Bcc ((Get-AdUser $email -properties mail).mail) -Body $_ -From sender@email.com -Subject $_ -Attachments $env:TEMP\$_.txt
}
else{Send-MailMessage -SmtpServer smtpserver.root.local -to admin@email.com -Body $_ -From sender@email.com -Subject $_ -Attachments $env:TEMP\$_.txt
}
}
Catch {''}
}
}
This one script works, but as soon as I put the variable in a copy of the recipient reports come just for one computer from the list (last on the list). If I do not specify a variable after the search report is generated, the recipient receives all the letters.
$OU = 'ou=test,dc=root,dc=local'
$Throttle = 10
(Get-ADComputer -Filter * -SearchBase $OU).Name | Out-File $env:TEMP\Servers.txt
$servers = Get-Content $env:TEMP\Servers.txt
Invoke-Parallel -InputObject $servers -Throttle 10 -ScriptBlock {
if($ping = Test-Connection -ComputerName $_ -BufferSize 16 -quiet -count 2)
{
Try {
(Get-ChildItem ('\\' + $_ + '\c$') -Include *.doc -Recurse -Force -ErrorAction Ignore).FullName} | Out-File $env:TEMP\$_.txt
Send-MailMessage -SmtpServer smtpserver.root.local -to admin@email.com -Body $_ -From sender@email.com -Subject $_ -Attachments $env:TEMP\$_.txt
}
Catch {''}
}
Please help, I have no strength and expertise to cope this task :(
- Edited by Denis.Pasternak 17 hours 50 minutes ago