Hi,
I was wondering if someone could shed some light on what im trying to do.
Currently running 0.5.85 mesh central sometimes i have noticed that the computers of the mesh agent sometimes it stops or gets disabled.
I have this bat script to install automatically
IF NOT EXIST "%ProgramFiles%\Mesh Agent\MeshAgent.exe" (
"\\192.168.3.150\shares\publica\sistemas\remote\meshagent64-aa" -fullinstall -resetnodeid
)
I wanted to see how i can also add to the script to see if the service stops to start it
What i did see, running these commands
C:\Users\windows10>P:\sistemas\remote\meshagent64-aa.exe state
Running
and when its stopped i get this
C:\Users\windows10>P:\sistemas\remote\meshagent64-aa.exe state
Stopped
and to restart it would this
C:\Users\windows10\Desktop>meshagent64-aa.exe restart
Restarted the mesh agent
Thank you
I'm not really a Windows person, but I've taken most of the code from this fine gentlemen ( https://stackoverflow.com/questions/35064964/powershell-script-to-check-if-service-is-started-if-not-then-start-it ), and modified it for MeshCentral and lightly tested on Windows 10. If I stop the background service, it starts back up as expected when I execute the script.
If you are not opposed to using PowerShell, you can call the PowerShell script from your batch file like this:
Powershell -noprofile -executionpolicy bypass -File C:\Path\To\Script\EnsureMeshRunning.ps1
Note This script must be run as Administrator! In other words, if your testing this from command prompt, make sure you start from an Administrator command prompt, or you will get the following error:
Start-Service : Service 'Mesh Agent background service (Mesh Agent)' cannot be started due to the following error:
Cannot open Mesh Agent service on Computer '.'."
...
PowerShell Script (EnsureMeshRunning.ps1):
$ServiceName = 'Mesh Agent'
$arrService = Get-Service -Name $ServiceName
while ($arrService.Status -ne 'Running')
{
Start-Service $ServiceName
write-host $arrService.status
write-host 'Starting MeshCentral Agent background service'
Start-Sleep -seconds 60
$arrService.Refresh()
if ($arrService.Status -eq 'Running')
{
Write-Host 'MeshCentral Agent background service is now in a running state'
}
}
This will check to see if the MeshCentral background service is not in a running state, and then try to start it. After 60 seconds, it will check to see that it has actually started, print a message and will then exit the script.
Actual Mileage May Vary.
Thank you so much for the reply,
this does start the service if it stops but doesn't powershell vary from windows 7 to windows 8 and 10?
Not really a windows guy, but from my research, Windows 7 comes with the default PowerShell version 2.0, however, Windows 10 comes with PowerShell version 5.0, and you can check your installed version by running the following command in PowerShell:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\Users\Darryl\Desktop> Get-Host | Select-Object Version
Version
-------
5.1.18362.752
For older versions of windows, you can also upgrade PowerShell, (Assuming you are running Windows 7 SP1), you can get that download from here : https://www.microsoft.com/en-us/download/details.aspx?id=54616
In modern versions of Windows, you can also start PowerShell using a specific version (Say 2.x) with the following command:
PowerShell.exe -Version 2
We can then ask PowerShell what version it is (We also see the 2009 copyright, giving us further hints that it's using the expected version.
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\Users\Darryl\Desktop> Get-Host | Select-Object Version
Version
-------
2.0
Using this knowledge, we can weave together the command in the last comment to get something like this:
Powershell.exe -version 2 -noprofile -executionpolicy bypass -File C:\Path\To\Script\EnsureMeshRunning.ps1
Which, results in the expected results, that is if the service is not running, it runs :partying_face:
Upon further testing, (In the event it's not honoring the -version 2?), I started PowerShell explicitly by running PowerShell.exe -Version 2 and ran Get-Host | Select-Object Version which reports Version: 2.0 as demonstrated above, and tested the Start-Service, Write-Host and Start-Sleep cmdlets, which all seem to function as expected.
I don't have a real windows 7 box to test against unfortunately. :( If you have a windows 7 box to test against, it would likely be a more viable test to try it there and see if it works, if not, it should give an error, and you should (hopefully) be able to use that error to figure out how to retrofit the code to use an older cmdlet that works in that version of PowerShell, or, upgrade the version of PowerShell if your organization allows that. If your older Windows machines already has PowerShell 5.0 installed, then you should be off to the races. :)
thanks so much, it did work but im in a pickle because i have windows 7 and 10 and some 8.1 whats odd is that the mesh agent though cmd does not appear as a service to start it
i tried something like this but didnt seem to work
Set MeshAgntExe=P:\sistemas\remote\meshagent64-aa.exe
rem Lets assume meshagent64-aa isn't running
Set MeshIsRunning=0
if exist "%MeshAgntExe%" "%MeshAgntExe%" state | Find "Running" && Set MeshIsRunning=1
IF "%MeshIsRunning%"=="1" Echo meshagent64-aa is running!
rem Lets assume meshagent64-aa isn't stopped.
Set MeshIsStopped=0
if exist "%MeshAgntExe%" "%MeshAgntExe%" state | Find "Stopped" && Set MeshIsStopped=1
IF "%MeshIsStopped%"=="1" Echo meshagent64-aa is stopped!
rem IF meshagent64-aa is stopped then restart
rem IF "%MeshIsStopped%"=="1" "%MeshAgntExe%" restart
rem IF meshagent64-aa is not running then restart
rem IF "%MeshIsRunning%"=="0" "%MeshAgntExe%" restart
rem IF meshagent64-aa is stopped and not running then restart
IF "%MeshIsStopped%"=="1" IF "%MeshIsRunning%"=="0" "%MeshAgntExe%" restart
thanks so much, it did work but im in a pickle because i have windows 7 and 10 and some 8.1
I'm a bit confused, if PowerShell works, then why not use it if we know it _should_ work in all those versions of windows? Are you not allowed to use PowerShell for some reason? Strictly speaking, I don't think batch files are the best tool for the job here.
i tried something like this but didnt seem to work
As per the Microsoft documentation ( https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/if ), it looks like if exist is meant to test against the existence of a file, not the running of a service (I haven't written a batch file in probably 20+ years or so, I may not be groking the documentation correctly, or groking your batch file correctly) :
exist聽<filename> | Specifies a true condition if the specified file name exists.
With the following example to delete a file called Product.dat, or report that it cannot find Product.dat:
IF EXIST Product.dat (
del Product.dat
) ELSE (
echo The Product.dat file is missing.
)
That said, if you are restricted to only using batch files for some reason, you can have a look at the following post which uses the service control command sc and the net start commands which, hopefully exists and works as expected in Windows 7, 8.1 and Windows 10.
From https://stackoverflow.com/questions/3325081/how-to-check-if-a-service-is-running-via-batch-file-and-start-it-if-it-is-not-r which gives this example code:
for /F "tokens=3 delims=: " %%H in ('sc query "MyServiceName" ^| findstr " STATE"') do (
if /I "%%H" NEQ "RUNNING" (
REM Put your code you want to execute here
REM For example, the following line
net start "MyServiceName"
)
)
As per the same post, be mindful that the argument you will want to pass to net start is the service name, not the display name.
Thank you so much, your right what i did these two days i updated all the windows 7 and 8 to windows 10 better and put the powershell script and it worked flawless
Thank you