Posted On March 29, 2019

Check a List of Servers to Find Currently Stopped Autorun Services

kimconnect 0 comments
blog.KimConnect.com >> Codes >> Check a List of Servers to Find Currently Stopped Autorun Services
$username = (Get-ADDomain).name+'\USERNAME'
$Password = 'PASSWORD'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$pass 



function checkServices($x){
# Setup trap to catch exceptions
trap [Exception]
{
	write-error $("TRAPPED: " + $_.Exception.Message);
} 
 
$computers = $x;
$start = $true;

# Setup the Service array with the service names we want to check are running. Please note that these services should be retrieved from a .txt or .cvs file for more robust implementation.
$serviceArray = "AppHostSvc","CryptSvc","DcomLaunch","Dhcp","DiagTrack","Dnscache","eventlog","EventSystem","FontCache","LanmanServer","LanmanWorkstation","lmhosts","macmnsvc","masvc","MSDTC","mvagtsvc","Netlogon","NlaSvc","nsi","PlugPlay","Power","ProfSvc","RemoteRegistry","RpcEptMapper","RpcSs","RServer3","SamSs","Schedule","SNMP","Spooler","VMTools","W3SVC","Winmgmt","WinRM","wuauserv"
 
foreach($computer in $computers)
{
	Write-Host "---------------------------`nChecking $computer for Expected Autorun Services`n---------------------------";
	$objWMIService = Get-WmiObject -Class win32_service -computer $computer
 
	foreach($service in $objWMIService)
	{
		# Check each service specicfied in the $serviceArray
		foreach($srv in $serviceArray)
		{
			if($service.name -eq $srv)
			{
				if($service.state -eq "running")
				{
					Write-Host "$srv is running";
				}
				else
				{
					Write-Host "$srv is NOT running on $computer";
					# If $start is true the script will attempt to start the service if it is stopped
					if($start -eq $true)
					{
						# Attempt to start the current service on the current computer
						$serviceInstance = (Get-WmiObject -computer $computer Win32_Service -Filter "Name='$srv'");
						$name = $serviceInstance.Name;
						Write-Host "Attempting to start $name  on $computer."
						$serviceInstance.StartService() | Out-Null;
						# Refresh the object instance so we get new data
						$serviceInstance = (Get-WmiObject -computer $computer Win32_Service -Filter "Name='$srv'");
						$state = $serviceInstance.State;
						Write-Host "$name is ""$state"" on  $computer.";
					}
				}
			}
		}
	}
}
}

Invoke-Command -ComputerName "JUMPBOX01" -credential $cred -ScriptBlock ${function:checkServices}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Office 365 Active Directory Hybrid Accounts Administration

Intro This scenario assumes that Azure AD Sync has been used to synchronize on premise…

PowerShell: Split Array into Multiple Sub-Arrays and Inflate Strings to Certain Lengths to Create Visual Columns

What does a nerd do on his free time? Give himself little puzzles to solve.…

PowerShell: Obtain Date Time Stamp And Convert to Pacific Standard Zone

# 1-liner date stamp $dateStamp=[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId( (Get-Date), 'Pacific Standard Time').tostring("MM-dd-yyyy-HHmm")+'_PST' #sample output #06-15-2020-1949_PST $timeZone='Pacific Standard Time'…
Other project: DragonCoin.com