Skip to content

Instantly share code, notes, and snippets.

@gislig gislig/AuditServices.ps1
Last active May 10, 2018

What would you like to do?
# Module Name : AuditServices.ps1
# Created : 08.11.2016
# Created by : Gisli Gudmundsson
# LinkedIN :
# License Usage :
# This code can be used for private use only
# You may modify this code and distribute
Import-Module ActiveDirectory
Import-Module C:\DynamicDocumentation\Dependencies\DEPMSSQLActions.psm1 -Force
#OU is not needed in this scenario#$OU = "OU=Departments,DC=tstdomain,DC=com"$Environment = "production"#This will set the default max days for the service to be required to change password$MaxDays = 30
#Get All servers in Active Directory$DNSHostNames = Get-ADComputer -Filter * | Select-Object DNSHostName
foreach($DNSHostName in $DNSHostNames){
#Check if the server is online
if(Test-Connection -ComputerName $DNSHostName.DNSHostName -Quiet){
#Capture All Services running on server
$Services = Get-WmiObject Win32_Service -ComputerName $DNSHostName.DNSHostName | Select-Object Name, Caption, StartName, Started | where { $_.StartName -notlike "NT Authority*" -and $_.StartName -notlike "*LocalSystem*" -and $_.StartName -notlike "*NT Service*" }
foreach($Service in $Services){
#Audit the service user and add it to the database
if($Service.StartName -ne$null){
$UserName = $Services.StartName -split "\\"
$User = Get-ADUser $UserName[1] -Properties UserPrincipalName, PasswordLastSet | select UserPrincipalName, PasswordLastSet
$GetDays = New-TimeSpan -Start $User.PasswordLastSet -End (Get-Date)
$DaysCounted = $GetDays.Days
$Notes = ""
$Notes = "User has not change password for at least "+ $DaysCounted + " days, max days are $MaxDays"
addMSSQLAuditServices -ServerName $DNSHostName.DNSHostName -ServiceName $Service.Name -Caption $Service.Caption -RunningAs $Service.StartName -Notes $Notes -Environment $Environment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.