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

1-Liner to Find Host of Virtual Machine in Hyper-V

PS C:\Users\testAdmin> (get-item "HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters").GetValue("HostName")hyperv01.intranet.kimconnect.com

CSS: How To Set Color Gradient and Animation to Text and Background

// Static color gradient .colorGradientClass { background-color: #ffffff !important; background-image: linear-gradient(315deg, #ffffff 0%, #d9d9d9 74%)…

Reference Entries of Office 365 records for Internal & External DNS

General Office 365 email setup check list: MS requires that each smart-host configuration or send…