Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Services using Domain admin account

$
0
0

You’re overwriting ServerList.htm every time through the loop with that call to Out-File. What you’re probably looking to do is something more like this (moving the calls to ConvertTo-Html and Out-File outside the loop):

$head = @'
<style>
BODY{background-color:#012456;}
H2{color:White;}
TABLE{border-width: 2px;border-style: solid;border-color:white;border-collapse: collapse;}
TH{border-width: 2px;padding: 10px;border-style: solid;border-color:white;color:#FFFFFF;}
TD{border-width: 2px;padding: 10px;border-style: solid;border-color: white;color:white}
</style>
'@

$ifeverything_ok = $true

Get-Content C:\Allwindows.csv |
ForEach-Object {
    $comp = $_
    try
    {
        $ErrorActionPreference = "Stop"
        
        $properties = @(
            @{Expression={$_.systemName};Label = "Server Name"},
            @{Expression={$_.DisplayName};Label = "Service Name"},
            @{Expression={$_.Name};Label = "Service"},
            @{Expression= {$_.StartName};Label = "Account"},
            'State'
        )

        Get-WmiObject win32_service -ComputerName $comp -filter "StartName Like '%Administrator%'" |
        Select-Object -Property $Properties
        
        $comp | Out-File C:\Temp\Logs.txt -Append -Encoding ascii

    }
    Catch [system.exception]
    {
        $ifeverything_ok = $false
        $comp | Out-File C:\Temp\Logs.txt -Append -encoding ASCII
        Add-Content -Value $_.Exception -Path C:\Temp\Logs.txt
    }
} |
ConvertTo-Html -Head $head -Body "<H2>Service Accounts Running As Domain Administrator</H2>" |
Out-File C:\ServerList.htm

Invoke-Expression C:\ServerList.htm

On a side note, this script doesn’t actually identify services running as a Domain Admin account (which would imply you should be checking for group membership). It just looks for services running as any account with “Administrator” in the name.


Viewing all articles
Browse latest Browse all 13067

Trending Articles