I wrote a script to tell me if the SEP Master Service is running for all computer listed in a file, start the service if its stopped, and let me know if it doesn't exist. It's working, but when the script hits a computer that is not online, it slows down until an error is returned and then finally goes to the next computer in the list. Is there a way to only scan for the service on the deivces in the list that are online and can be pinged on the network?
$computers = Get-Content -Path "C:\temp2\ComputerList.txt"
foreach ($computer in $computers) {
$service = Get-Service -name SepMasterService -computername $computer
$ServiceStatus = $service.Status
$ServiceDisplayName = $service.DisplayName
if ($ServiceStatus -eq 'Running') {
Write-Output "Service OK - Status of $ServiceDisplayName is $ServiceStatus on $computer"
}
elseif ($ServiceStatus -eq 'stopped') {
Start-Service -Name SepMasterService -PassThru
}
else {
Write-Output "Symantec Endpoint Protection doesn't exist on $computer"
}
}
Add a check for online machines using the test-connection cmdlet and go to the next computer.
$computers = Get-Content -Path "C:\temp2\ComputerList.txt"
foreach ($computer in $computers) {
if(!(Test-Connection -ComputerName $computer -Count 1 -Quiet))
{
Write-Output "$computer is offline"
continue
}
$service = Get-Service -name SepMasterService -computername $computer
$ServiceStatus = $service.Status
$ServiceDisplayName = $service.DisplayName
if ($ServiceStatus -eq 'Running') {
Write-Output "Service OK - Status of $ServiceDisplayName is $ServiceStatus on $computer"
}
elseif ($ServiceStatus -eq 'stopped') {
Start-Service -Name SepMasterService -PassThru
}
else {
Write-Output "Symantec Endpoint Protection doesn't exist on $computer"
}
}
The -Count parameter specifies how many time to ping the computer. The default is 4 but this will speed up your process. The continue statement will stop executing any of the code in the current iteration of the foreach loop and go to the next computer.
Hope this helps.
Related
I want to print error message when service is not found or machine is offline
$csv = Import-Csv "C:\Users\user\Desktop\computers.csv" -delimiter ","
foreach ($cs in $csv){
$computers = $cs.DNSHostname
foreach ($computer in $computers){
Try{
Invoke-Command -ComputerName $computer -ScriptBlock {
$ProcessCheck = Get-Process -Name agentid-service -ErrorAction Stop
if ($null -eq $ProcessCheck) {
Write-output "agentid-service IS not running $env:computername"
}
else {
Write-output "agentid-service IS running $env:computername"
}
}
}
catch{
Write-Warning $Error[0]
}
}
}
Instead, when service name is not found, i'm getting error:
Cannot find a process with the name "agentid-service". Verify the process name and call the cmdlet again.
And if connection to computer is not possible then getting:
[vm.domain.local] Connecting to remote server vm.domain.local failed with the following error message : The WinRM client cannot process the request because the server name cannot be resolved.
And script continue execution (as it should).
How can i get "Catch" block to be executed ?
You can do this in one foreach loop.
Try
$computers = (Import-Csv "C:\Users\user\Desktop\computers.csv").DNSHostname
foreach ($computer in $computers) {
if (!(Test-Connection -ComputerName $computer -Count 1 -Quiet)) {
Write-Warning "Computer $computer cannot be reached"
continue # skip this one and proceed with the next computer
}
try{
Invoke-Command -ComputerName $computer -ScriptBlock {
$ProcessCheck = Get-Process -Name 'agentid-service' -ErrorAction Stop
if (!$ProcessCheck.Responding) {
"agentid-service is NOT not running on computer '$env:computername'"
}
else {
"agentid-service is running on computer '$env:computername'"
}
}
}
catch{
Write-Warning $_.Exception.Message
}
}
Thanks to #Theo's answer, managed to fix it:
$csv = Import-Csv "C:\Users\user\Desktop\computers.csv" -delimiter ","
foreach ($cs in $csv){
$error.Clear()
$computers = $cs.DNSHostname
foreach ($computer in $computers){
Try{
Invoke-Command -ComputerName $computer -ScriptBlock {
$ProcessCheck = Get-Process -Name agentid-service -ErrorAction Stop
if ($null -ne $ProcessCheck) {
Write-output "agentid-service IS running $env:computername"
}
} -ErrorAction Stop
}
catch{
if ($null -eq $ProcessCheck){
Write-warning "agentid-service IS not running $env:computername"
}
Write-Warning $Error[0]
}
}
}
Hello I have been trying to make this script work for some time now, but I keep running into problems. The script is written with multiple servers in mind, although I am just testing it only on one test system for now.
Following I will post the script.
#List of Servers
$SQLServer = #('testserver.net')
$ApplicationServer = #('testserver.net')
$Servers = #($SQLServer, $ApplicationServer)
$ServiceName = #('tomcat9', 'MSSQLSERVER')
#Information Message
Write-Host "Starting update process for the following Servers:" $Servers "and Services:" $ServiceName
#Gets status of Servers
ForEach ($Index in $Servers)
{
Invoke-command {Get-Service tomcat9} -comp $Servers[$Index]
Invoke-command {Get-Service -Name 'MSSQLSERVER'} -comp $Servers[$Index]
}
#Information Message
Write-Host "Stopping the specified Services:" $ServiceName
#Stops services
#Sequentially stops TomCat and then DB Applications
ForEach ($Index in $Servers)
{
$ArrayService = Invoke-Command {Get-Service tomcat9} -comp $Servers[$Index]
if ($ArrayService.Status -ne "Stopped")
{
Invoke-Command {Stop-Service tomcat9} -comp $Servers[$Index]
}
$ArrayService = Invoke-Command {Get-Service -Name 'MSSQLSERVER'} -comp $Servers[$Index]
if ($ArrayService.Status -ne "Stopped")
{
Invoke-Command {Stop-Service -Name 'MSSQLSERVER'} -comp $Servers[$Index]
}
}
#Information Message
Write-Host "Will now start Windows Updates on servers and reboot"
#Will Update Windows, automatically reboot and out the log in the specified directory. This will be done to all listed Servers
ForEach ($Index in $Servers)
{
$Scriptblock = {
Invoke-WUJob -Comp $Servers[$Index] -RunNow -Confirm:$false -Verbose -Script {
Install-WindowsUpdate -MicrosoftUpdate -NotCategory "SilverLight" -NotTitle "Preview" -AcceptAll -AutoReboot | Out-File C:\PSWindowsUpdate.log
}
}
Invoke-Command -Comp $Servers[$Index] -ScriptBlock $Scriptblock
}
#Information Message
Write-Host "Starting up Services again"
#Starting SQL Server and checking if its running, if not running it will give more time before the Script continues
ForEach ($Index in $SQLServer)
{
$ArrayService = Invoke-Command {Get-Service -Name $ServiceName} -comp $SQLServer[$Index]
$FilteredService = $ArrayService | where {$_ -like 'MSQL'}
while ($FilteredService.Status -ne 'Running')
{
Invoke-Command {Start-Service $FilteredService} -comp $SQLServer[$Index]
Write-Host $FilteredService.Status
Write-Host "SQL Service starting on " $SQLServer[$Index]
function Check-Status
{
Start-Sleep -seconds 30
$FilteredService.Refresh()
if ($FilteredService.Status -eq 'Running')
{
Write-Host "SQL Service running"
#Invoke-Command {Start-Service $ArrayService | where {$_ -like 'tomcat9'}} -comp $ApplicationServer
}
else
{
Check-Status
}
}
}
}
#Starting TomCat Service again
ForEach ($Index in $ApplicationServer)
{
Write-Host "MUMService starting on " $ApplicationServer[$Index]
#Invoke-Command {Start-Service $ArrayService | where {$_ -like 'tomcat9'}} -comp $ApplicationServer[$Index]
$ServiceStatus = Invoke-Command {Get-Service tomcat9} -comp $ApplicationServer[$Index]
if ($ServiceStatus.Status -eq 'Running')
{
Invoke-Command {Start-Service $ArrayService | where {$_ -like 'tomcat9'}} -comp $ApplicationServer[$Index]
Write-Host "MUM Service running"
}
else
{
Invoke-Command {Start-Service $ArrayService | where {$_ -like 'tomcat9'}} -comp $ApplicationServer[$Index]
}
}
ForEach ($Index in $Servers)
{
invoke-command {Get-Service mum_software_tomcat9} -comp $Servers[$Index]
invoke-command {Get-Service -Name 'MSSQLSERVER'} -comp $Servers[$Index]
}
Either my logic is really wrong, or this doesnt work this way in powershell, but when I try to execute the script as is, there is an error for the parameter 'ComputerName'. It says that the argument is null or empty. Provide an argument that is not null or empty and then try the command again. Apparently it does not like $SQLServer[$Index].
The update functionality itself seems to work by itself though.
What is wrong with this?
Below is the script that I'm running that always gives either a Program error or a The term 'x86' is not recognized as the name of a cmdlet error. The service stops but the Invoke-Command seems to be the problem.
$Servers = Get-Content "C:\DTServerScript\Servers.txt"
ForEach ($Server in $Servers) {
$Status = ""
do
{
#Stopping Service
$ServiceAgent = Get-Service -ComputerName $Server | Where {$_.Name -like "*oneagent*"}
Write-Host "Pending Stop on $Server"
Stop-Service $ServiceAgent
sleep 1
$Status = $ServiceAgent.Status
} until ($Status -eq "Stopped")
Write-Host "Service state is $Status on $Server"
# Execute config change
# Invoking Commands on Server
Invoke-Command -ComputerName $Server {cmd.exe/ c:\Program Files (x86)\WinPcap\uninstall.exe /S }
write-host "Service is starting on $Server"
# Starting Service
Start-Service $ServiceAgent
$ServiceAgent = Get-Service -ComputerName $Server | Where {$_.Name -like "*oneagent*"}
$Status = $ServiceAgent.Status
}
Try quoting the path and using braces around the name containing ()..
Invoke-Command -ComputerName $Server { & $Env:ComSpec /C "${Env:ProgramFiles(x86)}\WinPcap\uninstall.exe" /S }
Is there a way of getting the below to run in parallel (multi-threading)? I have about 200 servers that need to run and was wondering if there is a way of checking say 10 servers at once rather then one at a time...WMI is very slow in checking this one at a time.
clear
Write-Host "Script to Check if Server is Alive and Simple WMI Check"
$servers = Get-Content -Path c:\Temp\Servers.txt
foreach($server in $servers)
{
if (Test-Connection -ComputerName $server -Quiet)
{
$wmi = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $server).Name
Write-Host "$server responds: WMI reports the name is: $wmi"
}
else
{
Write-Host "***$server ERROR - Not responding***"
}
}
Use powershell jobs:
$scriptblock = {
Param($server)
IF (Test-Connection $server -Quiet){
$wmi = (gwmi win32_computersystem -ComputerName $server).Name
Write-Host "***$server responds: WMI reports the name is: $wmi"
} ELSE { Write-Host "***$server ERROR -Not responding***" }
}
$servers | % {Start-Job -Scriptblock $scriptblock -ArgumentList $_ | Out-Null}
Get-Job | Wait-Job | Receive-Job
I want to run a script to check if 5 servers are up and running based on a specific service is running.If that service is running then we know that server is up and accesible. If it does not reply with a response back then I want it to continously check for it. Heres what I got so far:
Get-Service LANMANSERVER -ComputerName JOHNJ1
Get-Service LANMANSERVER -ComputerName JOHNJ2
Get-Service LANMANSERVER -ComputerName JOHND1
Get-Service LANMANSERVER -ComputerName JOHNM
Get-Service LANMANSERVER -ComputerName JOHNI
start-sleep 90
Yep you just need to check the Status property on the returned object like this:
$servers = "JOHNJ1", "JOHNJ2"
foreach ($server in $servers) {
$status = (get-service -Name lanmanserver -ComputerName $server).Status
if ($status -eq "Running") {
"Its Up!"
} else {
"Its Down!"
}
}
Update Here is an example of how to wait for a server to become online:
$servers = "JOHNJ1", "JOHNJ2"
foreach ($server in $servers) {
while ( (get-service -Name lanmanserver -ComputerName $server).Status -ne "Running" ) {
"Waiting for $server ..."
Start-Sleep -Seconds 10
}
"$server is Up!"
}