Tuesday, August 5, 2014

Powershell: Get list of all services accounts used on Windows servers in domain.


Purpose: This script will search AD for Windows Servers that are enabled and attempted to connect to each server and get a listing of all services and the accounts used to run them.
Note: This will take a while to run unless someone wants to make it multithreaded.
I just wrote this so there may need to be some bug fixes but in general it seemed to work.
Requirements: RSAT tools WMI



Import-Module activedirectory
cls

$ServicesReport = @()

$AllServers = Get-ADComputer -Filter {(OperatingSystem -Like "Windows Server*")-and (enabled -eq "true")} -Property SamAccountName | select -expand Name

$i = 0
foreach($Server in $AllServers) {
$i++
Write-Host "Working on Server: $Server " $i " of " $AllServers.Count 

try
{
if (Test-Connection -ComputerName $Server -Quiet) {
$Services = Get-WmiObject win32_service -ComputerName $Server | select Name,
  @{N="StartupType";E={$_.StartMode}},
  @{N="ServiceAccount";E={$_.StartName}},
  @{N="SystemName";E={$_.Systemname}}
    foreach ($Service in $Services) {
       $Detail = New-Object PSObject 
    $Detail | Add-Member Noteproperty ServiceName $($Service.Name)
    $Detail | Add-Member Noteproperty StartupType $Service.StartupType
    $Detail | Add-Member Noteproperty ServiceAccount $Service.ServiceAccount
    $Detail | Add-Member Noteproperty SystemName $Service.Systemname
       $ServicesReport += $Detail
    }
 }
}
Catch
{
$Detail = New-Object PSObject 
    $Detail | Add-Member Noteproperty ServiceName "NA"
    $Detail | Add-Member Noteproperty StartupType "NA"
    $Detail | Add-Member Noteproperty ServiceAccount "Error"
    $Detail | Add-Member Noteproperty SystemName $Server
       $ServicesReport += $Detail
  
 }
  
  } 
  
$ServicesReport | Export-Csv -Path c:\temp\test.csv -NoTypeInformation


No comments:

Post a Comment