Hi, I have a task to look for computers in all 25 different computer OUs for computers with no description and then email the result per each OU to 25 different people. I am a total newbie in PS and just took a PS class recently. Below is what I have. I run the “Get-ADComputer” 25 times and then has a 25 Send email steps below (each to be sent to different people). This is way overkill. I’ve looked at the “For each object” command and try creating script for it but don’t know how i can have it loop and run each ou and then create 25 files to email each person.
THIS IS THE SCRIPT (I cut off all the repetitive lines) and sub some of generic names into the fields.
# # Load the Microsoft Active Directory Module Import-Module ActiveDirectory # # #======================================================================================================================================================== # # Get all Computers under different country OUs with blank description field and then list out name / descripton column. # #======================================================================================================================================================== # Get-ADComputer -Filter 'Description -notlike "*"' -SearchBase "OU=1,OU=Z,DC=TEST,DC=local" -Properties description | Select-Object name,description | Out-File "C:\Test\OutFile\1ZComputers_No_Descriptions.txt" Get-ADComputer -Filter 'Description -notlike "*"' -SearchBase "OU=2,OU=Z,OU=DC=TEST,DC=local" -Properties description | Select-Object name,description | Out-File "C:\Test\OutFile\2ZComputers_No_Descriptions.txt"
REPEAT 23 MORE TIMES
#============================================================================== # # Send Email with attachment of computers with no descriptions to site admins. # #============================================================================== # $SMTPServer = "x.x.x.x" $SMTPPort = 587 $SMTPUsername = "test@test.com" $Attachment = "C:\Test\OutFile\1ZComputers_No_Descriptions.txt" $EncryptedPasswordFile = "C:\Test\MailRelayPswd.txt" $SecureStringPassword = Get-Content -Path $EncryptedPasswordFile | ConvertTo-SecureString $EmailCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $SMTPUsername,$SecureStringPassword $MailTo = "John Smith" $MailFrom = "Me" $MailSubject = "List of Computers with no description" $MailBody = "Hi, attached is a list of computers with no description." Send-MailMessage -From $MailFrom -To $MailTo -Subject $MailSubject -Body $MailBody -SmtpServer $SMTPServer -Port $SMTPPort -Credential $EmailCredential -Attachments $Attachment # # $SMTPServer = "x.x.x.x" $SMTPPort = 587 $SMTPUsername = "test@test.com" $Attachment = "C:\Test\OutFile\2ZComputers_No_Descriptions.txt" $EncryptedPasswordFile = "C:\Test\MailRelayPswd.txt" $SecureStringPassword = Get-Content -Path $EncryptedPasswordFile | ConvertTo-SecureString $EmailCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $SMTPUsername,$SecureStringPassword $MailTo = "John Doe" $MailFrom = "Me" $MailSubject = "List of Computers with no description" $MailBody = "Hi, attached is a list of computers with no description." Send-MailMessage -From $MailFrom -To $MailTo -Subject $MailSubject -Body $MailBody -SmtpServer $SMTPServer -Port $SMTPPort -Credential $EmailCredential -Attachments $Attachment
REPEAT EMAIL STEPS 23 MORE TIMES WITH DIFFERENT MAILTO
#THIS IS THE SCRIPT when I try using the ForEach -Object loop. It runs and then grabs all 25 different OUS and creates one txt #file but I want 25 different files so I can email the 25 different individuals
$ou = 'OU=1,OU=Z,DC=TEST,DC=local', 'OU=2,OU=Z,DC=TEST,DC=local', #... (the other 23 OUs) $ou | ForEach-Object { Get-ADComputer -SearchBase $_ -Filter 'Description -notlike "*"' | Select-Object Name,Description } | Out-File "c:\Test\Test.txt"
This just outputs everything into 1 file.