Tuesday, October 8, 2013

Windows Service Account Report (Power Shell v2)

Well it has been busy.  After My two weeks away we let a group of users go which has lead to a whole new task of automating on boarding and off boarding users.  Still tweaking the scripts and will post them once I feel they are solid.  But here is a script that I pieced together in response to a post on SpiceWorks (see post here).

 Request was to pull Service Accounts from servers to see what account is running what.

Some quick searching had this resolved and then I wanted to export to file and offer up emailing the reports.

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/2212-service-account-report)

# +-----------------------------------------------------------------------------------
# | File : Service Account Report.ps1                                          
# | Version : 1.01                                          
# | Purpose : Pulls Service Accounts from list of servers
# |           Saves to individual CSV files
# |           Can Email reports
# |           Can remove reports before script exits
# +-----------------------------------------------------------------------------------
# | 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 START###

###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 service account information for each server
foreach ($i in $a)
  Get-WmiObject win32_service -computer $i | select name, startname, startmode | Export-csv "$temppath$i.csv" -notype
  $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 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