PowerShell – lister les services tournant avec un compte du domaine

Hello tout le monde,

Encore un petit script PowerShell de mon cru !

Celui-ci va lister tous les services de tous les serveurs contenus dans l’Active Directory qui tournent sous un compte du domaine. Utile en cas d’application d’un nouvelle politique de mots de passe.

Le fichier généré est au format CSV et reprend le nom du serveur, le nom du service et le user qui le fait tourner.

J’espère qu’il vous sera utile !

<#
.NAME
	Get-ServicesAccounts.ps1
.Synopsis
   PowerShell script to list all services running with domain accounts
.DESCRIPTION
	This script query Active Directory to search services running with domain accounts, on all Active Directory domain computers running any version of Windows Server OS.
	It will generate an output file named 'ServicesAccounts.csv' on your desktop.
.AUTHOR
    Antoine DELRUE
    Contact : antoine@delrue.me - https://obilan.be
#>

# [RECOMMENDED] Reseting variable.
$Servers = $ServerName = $Query = $Path = $Item = $null

# [OPTIONAL] We don't really want to see errors if servers are unavaillable.
$ErrorActionPreference= 'silentlycontinue'

# [REQUIRED] Importing Active Directory Module (must be present in your system).
Import-Module ActiveDirectory

# [OPTIONAL] Afficher les messages de Write-Verbose
$VerbosePreference = "continue"

# [REQUIRED] Creating a variable with the path of the result file.
$Path = "$home/Desktop/ServicesAccounts.csv"

# [REQUIRED] Querying Active Directory for servers running any Windows Server OS version. Please note it will return all servers in AD, even offline ones.
$Servers = Get-ADComputer -LDAPFilter "(&(objectcategory=computer)(OperatingSystem=*server*))"

# [REQUIRED] Querying Active Directory for the NETBIOS name. It will be used in the loop to search services running with Domain Accounts only.
$NetBIOS = (Get-ADDomain).NetbiosName

# [OPTIONAL] Display the number of server I will try to query, in the console, and the path of the result CSV file.
$Number = $Servers.count
Write-verbose -message "Trying to query $Number servers, searching services running with Domain Accounts."
Write-verbose -message "When completed, you will be able to open the file generated here : `n $path" 

# BEGINING OF THE LOOP

foreach ($Item in $Servers) {

    $ServerName = $Item.name
    # [OPTIONAL] Display the number of server I will try to query, in the console.
    Write-Verbose -Message "I'm now trying to query $ServerName"

    <# [REQUIRED] Bellow is the main query. It searches services with a StartName (Log on As) containing "DOMAIN", for instance DOMAIN\administrator or administrator@domain.local.
    Finally, it export the result in a CSV file (append).
    #>
    $Query = Get-WmiObject -Class win32_Service -ComputerName $ServerName | ? {$_.Startname -ilike "*$NetBIOS*" } | Select PSComputerName,Name,StartName,StartMode,State
    $Query | Export-Csv -Path $Path -Append -NoTypeInformation
}

# END OF THE LOOP 

 

Laisser un commentaire