Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Include server name in Get-WmiObject win32_service script output

$
0
0

I've put together a script which checks for services that are using the administrator account.

Within the script I am having to output the server name for each server I am scanning so I can see which server it was that fits the criteria.

If anyone can show me how I can write the computer name to the $output only if there is a service running as administrator and preferably only the once in the case when there is more than one service running as administrator on a $computer it would be greatly appreciated.

Please see code below. Any advice on how to improve this code would also be appreciated although please take into account I'm just starting out with this.

thanks.

Pete.

Function ServiceAdmin
{
 "Scan results for running a service as administrator $Date" | Out-File $output
 "Scan results for running a service as administrator Error Log - $Date" | Out-File $ErrorLog
 Foreach ($Computer In $ComputerList)
 {
 TRY
  {
  $ErrorActionPreference = "Stop";
  "Scanning $Computer " | Out-File $output -Append
  Get-WmiObject win32_service -computername $Computer | select Name,StartName | where {$_.startname -Like "*administrator*"} | Out-File $output -Append
  }
 CATCH
 {
 "Error on $Computer 'nError:$($_.Exception.Message)" | Out-File $ErrorLog -Append
 }
 }
}

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles