Tuesday, October 8, 2013

Windows Scheduled Tasks Service Account Report (Power Shell v2)

Wiped this one together to verify none of our scheduled tasks are using our domain administrator account prior to password change.

First step is setting up the source file.
On my servers I have the following structure: %root%\_scripts\source files

  • Create a text file in your source location, ensure it is a txt file
  • one server name per line
Copy the code below into a .ps1 file or you can download from my Google drive here
Update the fields highlighted in blue to meet your requirements. 

Hope this is helpful.
(If you find it helpful please head over to Spiceworks and Spice up the code to help other IT members find it: http://community.spiceworks.com/scripts/show/2213-scheduled-tasks-service-accounts)

# +-----------------------------------------------------------------------------------
# | File : Scheduled Tasks Service Accounts.ps1                                        
# | Version : 1.01                                        
# | Purpose : Pulls Scheduled Tasks from list of servers
# |           Saves to individual CSV files
# |           Can Email reports
# |           Can remove reports before script exits
# | Based on: Ryan Schlagel's Scripts
# |           http://ryanschlagel.wordpress.com/2012/07/09/managing-scheduled-tasks-with-powershell/
# +-----------------------------------------------------------------------------------
# | Maintenance History                                          
# | -------------------                                          
# | Name            Date        Version  C/R  Description      
# | ----------------------------------------------------------------------------------
# | Chris Lee     2013-10-08     1.01         Initial scirpt build
# +-----------------------------------------------------------------------------------


###SETUP START###
#-------DO NOT MODIFY-------#
#Add Exchange 2007 commandlets (if not added)
if(!(Get-PSSnapin | Where-Object {$_.name -eq "Microsoft.Exchange.Management.PowerShell.Admin"})) {ADD-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin}
#Add Quest commandlets (if not added)
if(!(Get-PSSnapin | Where-Object {$_.name -eq "Quest.Activeroles.ADManagement"})) {ADD-PSSnapin Quest.Activeroles.ADManagement}
#Defines Time/Date Stamp used
$CreateStamp = Get-Date -UFormat %d_%m_%Y
###SETUP END###

###USER VARIABLES START###
#-------MODIFY AS NEEDED-------#
#Define path to server list to be used
    $path = "[PATHTOYOURSERVERLISTTEXTDOCUMENT]"
#Define path to temp/report folder include trailing \
    $temppath = "[PATHTOYOURTEMPFOLDER]"
#Email reports to admin "True" | "False"
    $email = "True"
#Delete files after script runs "True" | "False"
    $delete = "True"
#Enter System Admin
    $AdminName="[YOURADMINNAME]"
#Enter Admin Email Address
    $to="[YOURADMIN]@[YOURDOMAIN].com"
# SMTP Server to be used
    $smtp = "[YOURSMTP]"
# "From" address of the email
    $from = "ServerReports@[YOURDOMAIN].com"
#Enter Path to reports
    $file="C:\_temp\"
# Define font and font size
# ` or \ is an escape character in powershell
    $font = "<font size=`"3`" face=`"Calibri`">"
###USER VARIABLES END###

###PROGRAM VARIABLES START###
#-------DO NOT MODIFY-------#
# Get today's day, date and time
$today = (Get-date)
# Newline character
$newline = "<br>"
#Enter Subject line required for ticketing system
$subject="Service Accounts Report for " + $domain + " servers."
#Section break
$secbreak="`r`n---------------------------------------------------------------------------------------------------------------------------`r`n"
#Pull Domain information for email
$Domain = ([adsi]'').distinguishedname -replace "DC=","" -replace ",","."
###PROGRAM VARIABLES END###

###PROGRAM START###
#Loads Server list into varialbe
$a = Get-Content $path
#Declare string for report structure
$attachment = @()
#Generates CSV file with scheduled service account information for each server
foreach ($i in $a)
    {
      $schedule = new-object -com("Schedule.Service")
      $schedule.connect("$i")
      $tasks = $schedule.getfolder("\").gettasks(0)
      $tasks  | Format-Table Name, @{Name="RunAs";Expression={[xml]$xml = $_.xml ; $xml.Task.Principals.principal.userID}}, LastRunTime, NextRunTime -AutoSize |  Export-csv "$temppath$i.csv" -notype
      IF($tasks.count -eq 0) {Write-Host “Schedule is Empty”}
      $attachment += "$temppath$i.csv"
    }

#Check if email of files is desired
IF ($email -eq "True")
    {
        # Message body is in HTML font        
        $body = $font
        $body += "Dear " + $AdminName + ","+ $newline + $newline
        $body += "Attached are report(s) for scheduled tasks service accounts on " + $domain + " servers ." + $newline

        # Put a timestamp on the email
        $body += $newline + $newline + $newline + $newline
        $body += "<h5>Message generated on: " + $today + ".</h5>"
        $body += "</font>"

        # Invokes the Send-MailMessage function to send notification email
        Send-MailMessage -smtpServer $smtp -from $from -to $to -subject $subject -BodyAsHtml $body -Attachments $attachment
     }

#Check if removal of files is desired
IF ($delete -eq "True")
  {
    #Removes created file
    foreach ($i in $a)
      {
        Remove-Item "c:\_temp\$i.csv" -Recurse
      }
  }

###PROGRAM END###

No comments:

Post a Comment