Skip to content

Instantly share code, notes, and snippets.

@quonic
Created January 1, 2018 05:31
Show Gist options
  • Select an option

  • Save quonic/d1c35f5a9aa196f5864a4e7e922a934c to your computer and use it in GitHub Desktop.

Select an option

Save quonic/d1c35f5a9aa196f5864a4e7e922a934c to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Collects service account information from servers in a domain to a central file.
.DESCRIPTION
Service Account Scrobbler Script v.1.0.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
----------------------------------------------------------------------------
#>
function Get-ComputerInventory {
[CmdletBinding()]
param (
#CSV Export File Name
[string]$FileOut=$null
)
# Check Prerequsities
begin {
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
if(-not (Test-Path -Path $FileOut)){
$path = ".\ServiceAccountInventory" + $date.string + ".csv"
}else{
$path = $FileOut
}
Write-Verbose "Information will be written to $path in current directory" -Foregroundcolor Green
# Build list of servers
Write-Verbose "Building List of Domain Servers"
$comps = Get-ADComputer -filter * -properties *
$servers = $comps | Where-Object {$_.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-Verbose $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-Verbose "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)
}
if(Test-Path -Path $FileOut){
$obj | Export-Csv $path -Append
}
$obj
}
else {
Write-Verbose "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)
}
if(Test-Path -Path $FileOut){
$obj | Export-Csv $path -Append
}
$obj
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment