Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Powershell script to identify todays created Users/groups/Computers and email using Outlook as a client.

Hi,

Powershell script to identify todays created Users/groups/Computers and email using Outlook as a client.
I want help with a script that i can use as a scheduled task and run dail. I want a report of users/Computers/Groups created with the OU path in an email sent to 3 persons.

Regards
Sharath
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image


Using Outlook? As in not using SMTP?

If it's just SMTP then it can go like this using the Quest CmdLets and PowerShell 2.

Chris
$Recipients = "bob@domain.com", "dave@domain.com", "john@domain.com"

$LdapFilter = "(|(&(objectClass=user)(objectCategory=person))(objectClass=computer)(objectClass=group))"
$Content = [String](Get-QADObject -LdapFilter $LdapFilter `
  -CreatedAfter $((Get-Date).AddDays(-1)) | Select-Object Name, DN | 
  ConvertTo-Html)

Send-MailMessage -To $Recipients -Body $Content -BodyAsHtml `
  -From "auto@domain.com" -Subject "New Objects Today" `
  -SmtpServer "someserver"

Open in new window


Missed the OU, added here.

Chris
$Recipients = "bob@domain.com", "dave@domain.com", "john@domain.com"

$LdapFilter = "(|(&(objectClass=user)(objectCategory=person))(objectClass=computer)(objectClass=group))"

$Content = [String](Get-QADObject -LdapFilter $LdapFilter `
  -SearchRoot "OU=somewhere,DC=domain,DC=com" `
  -CreatedAfter $((Get-Date).AddDays(-1)) | Select-Object Name, DN | 
  ConvertTo-Html)

Send-MailMessage -To $Recipients -Body $Content -BodyAsHtml `
  -From "auto@domain.com" -Subject "New Objects Today" `
  -SmtpServer "someserver"

Open in new window

Avatar of bsharath

ASKER

Thanks
I need it via Outlook only
Will this use outlook
And i have group /Users /Computers in different Ou's
So can we mentione 3 or 4 Ou paths?

The version above uses SMTP. I can't help with Outlook; sending a mail through that is far more complex than SMTP.

Chris
$OUs = "OU=users,OU=somewhere,DC=domain,DC=com", "OU=computers,OU=somewhere,DC=domain,DC=com", "OU=groups,OU=somewhere,DC=domain,DC=com"

$Recipients = "bob@domain.com", "dave@domain.com", "john@domain.com"

$LdapFilter = "(|(&(objectClass=user)(objectCategory=person))(objectClass=computer)(objectClass=group))"

$Content = $OUs | ForEach-Object {
  Get-QADObject -LdapFilter $LdapFilter `
    -SearchRoot $_ `
    -CreatedAfter $((Get-Date).AddDays(-1)) | Select-Object Name, DN
} | ConvertTo-Html

$Content = [String]$Content

Send-MailMessage -To $Recipients -Body $Content -BodyAsHtml `
  -From "auto@domain.com" -Subject "New Objects Today" `
  -SmtpServer "someserver"

Open in new window

Thanks
If i mention just the root path can all sub Ou's be scanned and send users\Groups\Computers created for the day?

Is there no way for outlook
As SMTP is blocked via my exchange 2007
Exchange 2003 server i have now but next few weeks it will be removed

> If i mention just the root path can all sub Ou's be scanned and send
> users\Groups\Computers created for the day?

That's fine, that's what the original did.

> Is there no way for outlook

Yes, but I can't tell you how to do it. Needs MAPI programming which isn't something I've ever studied. It will be extremely difficult to find examples of that.

Chris
Thanks
Can i have them categorized in the email

Like

Computers
Data

Groups
Data

Users
Data

As now all come is one set
And how can i schedule this code to run each day

How about this one?

The sections aren't split, but it's ordered by type then name (and actually includes the type now).

Do you want the creation date shown as well?

Chris
$Recipients = "bob@domain.com", "dave@domain.com", "john@domain.com"

$LdapFilter = "(|(&(objectClass=user)(objectCategory=person))(objectClass=computer)(objectClass=group))"

$Content = [String](Get-QADObject -LdapFilter $LdapFilter `
    -SearchRoot "OU=somewhere,DC=domain,DC=com" `
    -CreatedAfter $((Get-Date).AddDays(-1)) | 
  Select-Object Name, Type, DN | 
  Sort-Object Type, Name | 
  ConvertTo-Html)

Send-MailMessage -To $Recipients -Body $Content -BodyAsHtml `
  -From "auto@domain.com" -Subject "New Objects Today" `
  -SmtpServer "someserver"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The scheduled task looks as this

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe "D:\Created today.ps1"

But email does not come
I can see some thing in red when i run manually via scheduled task

You'll have to check that SMTP is not blocked to the specified Smtp Server. Perhaps run this from the server:

telnet SmtpServerName 25

If it fails to connect then the message will fail to send.

Chris
The same code directly pasting on quest shell works

Are you able to run it as the user account you use for scheduling? It may be a problem specific to that account.

It could be that it doesn't have permission to execute scripts. If that is the case you will need to run "Set-ExecutionPolicy RemoteSigned" for that user first.

Chris
Still same issue
I am using the same account as logged in

Can we see whats happening and pause after run. Just to see the screen error

We can try...

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe "D:\Created today.ps1" -NoExit

Chris
I think the space in the file name was causing the error.
Now i removed it and when run on the cmd i get an email but not when run the same via task scheduler
Sorry works perfect
Thank you