Forked from bytemech/BYT-ServiceAccountInventory.ps1
Created
August 23, 2022 18:21
-
-
Save noblevarghese/2d0e3ce98ccaacaeb342ab09e067f3ad to your computer and use it in GitHub Desktop.
Service Account Inventory Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [CmdletBinding()] | |
| param () | |
| <# | |
| .SYNOPSIS | |
| Collects service account information from servers in a domain to a central file. | |
| .DESCRIPTION | |
| Service Account Scrobbler Script v.1 | |
| Date: Jan 2017 | |
| https://bytemech.com | |
| .NOTES | |
| Requirements: | |
| 1. Windows 2008 or higher Active Directory Powershell Module installed on local machine (script checks for this) | |
| 2. Directory with R/W permissons for current user (script checks for this) | |
| 3. Current user member of “Server Operators” or equivalent within the domain (script will log unable to check otherwise on a per-server basis) | |
| 4. WMI or Remote Powershell/WinRM enabled on domain servers. (script will log unable to check otherwise on a per-server basis) | |
| 5. Local Machine PS Execution Policy set corecctly to allow the running of scripts | |
| ---------------------------------------------------------------------------- | |
| "THE BEER-WARE LICENSE" (Revision 42): | |
| <byron@bytemech.com> wrote this file. As long as you retain this notice you | |
| can do whatever you want with this stuff. If we meet some day, and you think | |
| this stuff is worth it, you can buy me a beer in return. Byron Glover | |
| ---------------------------------------------------------------------------- | |
| #> | |
| # Check Prerequsities | |
| if ($psversiontable.psversion.major -lt "3") { | |
| Write-Warning "Powershell version is not v3 or higher, will not be able to record information. Script terminated" | |
| Pause | |
| Exit | |
| } | |
| try { | |
| Import-Module ActiveDirectory -ErrorAction Stop | |
| } | |
| catch { | |
| Write-Warning "Unable to load module for active directory, script terminated" | |
| Pause | |
| Exit | |
| } | |
| try { | |
| if (Test-Path .\test.txt) {Remove-Item test.txt} #Check for previous file and remove if found, confirms delete permission. | |
| New-Item -ItemType File -Name test.txt -ErrorAction Stop | Out-Null | |
| } | |
| catch { | |
| Write-Warning "Unable to create file in current directory or unable to remove previously created file. Script terminated." | |
| Pause | |
| Exit | |
| } | |
| #Storefile name | |
| $date = Get-Date -Format g | |
| $path = ".\ServiceAccountInventory" + $date.string + ".csv" | |
| Write-Host "Information will be written to $path in current directory" -Foregroundcolor Green | |
| # Build list of servers | |
| Write-Host "Building List of Domain Servers" | |
| $comps = Get-ADComputer -filter * -properties * | |
| $servers = $comps | Where {$_.OperatingSystem -match "Server"} | |
| #Begin scrobble foreach loop | |
| foreach ($server in $servers) { | |
| $i++ | |
| Write-Progress -Activity "Gathering Service Accounts" -Status "Percent Complete:" -percentcomplete (($i / $servers.count) * 100) | |
| if (Test-Connection $server.name -ErrorAction SilentlyContinue){ | |
| $canping = "Yes" | |
| Write-Host $server.name "is online, trying WMI now" | |
| try { | |
| $services = Get-WmiObject win32_service -ComputerName $server.name -ErrorAction Stop | |
| } | |
| catch { | |
| Write-Warning $("Unable to connect to WMI for " + $server.name) | |
| $services = $null | |
| } | |
| if ($services -eq $null) { | |
| try { | |
| $services = Invoke-PSSession -Computername $server.name -ScriptBlock { | |
| Get-WmiObject win32_service -ComputerName $server.name | |
| } -ErrorAction Stop | |
| } | |
| catch { | |
| Write-Warning $("Unable to connect to WinRM for " + $server.name) | |
| $services = $null | |
| } | |
| } | |
| if ($services -eq $null) { | |
| Write-Warning "Error retrieving WMI information" | |
| $wmi = "Error" | |
| } | |
| else { | |
| Write-Host "Retrieved WMI Information" | |
| $wmi = "OK" | |
| } | |
| } | |
| else { | |
| Write-Warning $("No connectivity to " + $server.name) | |
| $canping = "No" | |
| $wmi = "Error" | |
| } | |
| if ($wmi -match "Error" -or $canping -match "No") { | |
| #Create object and export | |
| $obj = New-Object -TypeName PSObject -Property @{ | |
| Hostname = $($server.name) | |
| Online = $($canping) | |
| WMI = $($wmi) | |
| OS = $($server.operatingsystem) | |
| ServiceName = "N/A" | |
| ServiceAcc = "N/A" | |
| StartMode = "N/A" | |
| CompLastLogonDate = $($server.lastlogondate) | |
| } | |
| $obj | Export-Csv $path -Append | |
| } | |
| else { | |
| Write-Host "Auditing services for" $server.name | |
| foreach ($service in $services) { | |
| $servicename = $service.name | |
| $serviceacc = $service.startname | |
| $servicestart = $service.startmode | |
| #Create object and export | |
| $obj = New-Object -TypeName PSObject -Property @{ | |
| Hostname = $($server.name) | |
| Online = $($canping) | |
| WMI = $($wmi) | |
| OS = $($server.operatingsystem) | |
| ServiceName = $servicename | |
| ServiceAcc = $serviceacc | |
| StartMode = $servicestart | |
| CompLastLogonDate = $($server.lastlogondate) | |
| } | |
| $obj | Export-Csv $path -Append | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment