Menu

Newsletter

PowerShell: Generate GPO Report

Shinish 7 years ago 192
0 0
Read Time:1 Minute, 44 Second

PowerShell: Generate GPO Report

We often come across a situation, where we have to generate the GPO Report for multiple GPOs from several Domains. It could be a cumbersome process and requires serious work force.

This PowerShell script generates the GPO report from various domains and send the reports to the requested email address.

<#
.SYNOPSIS
Generates a report in HTML format for a specified GPO in a domain.

.DESCRIPTION

Generates a report in HTML format for a specified GPO in a domain.

Please fill the required GPO and corresponding  domain in the .\db.csv 

(Contents of the CSV file)
GPO    Domain
XerGlobalTime    AMG.Domain.com
XerForceRestriction    RTG.Domain.com
XerGoogleDesktopRestrict    AMG.Domain.com



Script Written by Shinish Sasidharan

.EXAMPLE
.\GPO-Report.ps1 

.EXAMPLE
.\GPO-Report.ps1 yourmail@Domain.com

.NOTES
Please feel free to reach out to me if you need any assistance. E-mail: shinish@live.com
#>
Param (
    $email ='yourmail@email.com' # Default Recipent is yourmail@email.com
)
#Create new Folders, If Dir exist, silently Continue
    Remove-item C:\Reports\ -Recurse -ErrorAction SilentlyContinue
    New-Item C:\Reports\ -type directory -ea SilentlyContinue
    New-Item C:\Zip -type directory -ea SilentlyContinue
#Generate  Report 
$Inputfile = "./db.csv"
    ForEach ($row in (Import-Csv $Inputfile)) {
      $filename = $row.GPO
      Get-GPOReport -name  $row.Gpo  -Domain $row.Domain -ReportType Html  -Path C:\Reports\$filename.html  -Verbose -ErrorAction SilentlyContinue
      Write-host "Report Generated for : "$filename  -ForegroundColor Green
    }

    Write-host "Information: Reports generation completed (Reports Copied to c:\Reports)" -ForegroundColor Yellow
#compression 
$source = "C:\Reports"
$destination = "C:\zip\GPO-Report.zip"
If(Test-path $destination) {Remove-item $destination}
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($Source, $destination) 
#Email Headers
    if ($error.Count -eq 0)
    {
        #Does not run on empty folder
        if( (Get-ChildItem $source) -eq $null)
        {
            Write-Error "Error compressing folder. Source folder is empty: $source"
        }
    }
$smtphost="mail.Domain.com"
$from = "mail@Domain.com"
$to = "yourmail@Domain.com"
$attachment = "C:\zip\GPO-Report.zip"
    Send-MailMessage `
    -From $email `
    -To $email `
    -SmtpServer $smtphost `
    -Subject "GPO Report Exports" `
    -Body "This is an automated email.GPO Report as an attachment. Thanks" `
    -Attachments $attachment 
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %
Written By

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%