$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}
Categories: