Skip to content

Latest commit

 

History

History
65 lines (45 loc) · 1.98 KB

File metadata and controls

65 lines (45 loc) · 1.98 KB

Active Directory domain joined workstations report

PowerShell script connects to your Active Directory and gets information about workstations and servers in a specific OU, then emails the report in a CSV format.

Requirements

Permissions to run Get-ADComputer cmdlet.
Mail relay configured.

Script Output (CSV)

Name - AD Computer Object name.
OperatingSystem - Operating System.
OperatingSystemServicePack - OS Service Pack.
OU - Organizational Unit where the object is located.
whenChanged - Date when AD object was changed last time.

Script Syntax

Specify date format:

$ReportDate = Get-Date -format ddMMyyyy

Fill in mail related variables:

$smtpServer="SMTP_SERVER_IP_OR_DNS"
$MailTo="MailToAddress@example.com"
$MailFrom="MailFromAddress@example.com"
$MailSubject="Domain PC Report - $ReportDate"
$MailBody="CSV Report of current domain joined workstation and servers. Run date - $ReportDate"

Let the script know the OU and path for CSV report:

$SearchBase="OU=Workstations,DC=example,DC=com"
$ExportPath="C:\Report\"

Set below setting $true or $false if you want to keep reports or remove them after the script execution:

$KeepHistory=$true

Main part of the script. One trick to get OU name, just removing workstation name from CanonicalName attribute - {$_.CanonicalName -replace $_.Name,''}

Get-ADComputer -SearchBase $SearchBase -Filter * -Properties * | select Name,OperatingSystem,OperatingSystemServicePack,@{Name = "OU"; Expression = {$_.CanonicalName -replace $_.Name,''}},whenChanged | Export-Csv $ExportPath$ReportDate.csv -NoTypeInformation

Send email with CSV file attached:

Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -Attachments $ExportPath$ReportDate.csv -SmtpServer $smtpServer

Remove the report if needed:

if (!$KeepHistory) {Remove-Item $ExportPath$ReportDate.csv -Force}