Hello Team,
I wrote a code to get the service accounts list from remote server where i skipped Slandered Service accounts .
What i am looking is ...
I want a services which are running with Service Accounts from multiple remote servers in single .csv file.
But I am not getting the output, service related output is blank.
Tried all the possibilities but not getting any hint. :(
Kindly assist.
#@================Code Start===================
$SCRIPT_PARENT = Split-Path -Parent $MyInvocation.MyCommand.Definition
$Result = @()
#$Srvc = @()
$Servers = Get-Content ($SCRIPT_PARENT + "\Servers.txt")
#$Servers = Read-Host "Enter server name"
$outfile = ($SCRIPT_PARENT + "\ServiceAccounts_{0:yyyyMMdd-HHmm}.csv" -f (Get-Date))
Foreach ($Server in $servers) {
$i++
Write-Progress -activity "Running....." -status "Count: $i of $($Servers.count)" -percentComplete (($i / $Servers.count) * 100)
$Ping =Test-Connection -Quiet -ComputerName $server -Count 1
If ($ping -eq "True") {
# $SrvcS = ""
# $StandardServiceAccounts = ""
# $Resolve = ""
# $FQDN = ""
$Resolve = [System.Net.Dns]::Resolve($server)
$FQDN = $Resolve.HostName
write-Host "Getting Service details from - ($Server)" -ForegroundColor Magenta
$StandardServiceAccounts = "LocalSystem", "NT AUTHORITY\LocalService", "NT AUTHORITY\NetworkService"
$SrvcS = Get-WmiObject win32_service -ComputerName $Server | Where-Object { $StandardServiceAccounts -notcontains $_.StartName }
Foreach($Srvc in $SrvcS ) {
# If ($ping -eq "True") {
# $Srvc = $Srvcs
# }
# Else{
# $Srvc = " "
# }
$Result += New-Object PSObject -Property @{
ServerName = $Server
FQDN = $FQDN
Name = $Srvc.Name
StartName = $Srvc.StartName
StartMode = $Srvc.StartMode
#Systemuptime = $serveruptime
#Uptimehours = $uptime.hours
#Uptimemins = $uptime.minutes
}
}
}
else {
write-Host "Server is not accessible - ($Server)" -ForegroundColor Red
$Result += New-Object PSObject -Property @{
ServerName = $Server
FQDN = $FQDN
Name = "NA"
StartName = "NA"
StartMode = "NA"
#Systemuptime = $serveruptime
#Uptimehours = $uptime.hours
#Uptimemins = $uptime.minutes
}
}
$Result | select ServerName, FQDN, Name, StartName, StartMode | Export-csv -NoTypeInformation -Path $OutFile -UseCulture
}
Invoke-Expression $OutFile
#$report | Export-Csv $outfile -NoTypeInformation
#@================Code End=====================