web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Using Powershell to script the restart of two Microsoft Dynamics AX services if either of them has stopped

(0) ShareShare
ReportReport
Posted on by 60

need some help writing a Powershell script or a way to create a scheduled task to restart two services if either of them isn't running.  The script needs to include logic to stop a process if the service doesn't stop within 10 minutes and needs to use sleep timers to wait for services to stop and then idle after both are stopped.  See details below:

Any help would be greatly appreciated especially since I'm a newbie with using Powershell.  

Dynamics AX Object Server

    5.0$01-DynamicsAX-Prod-PB-01

Dynamics AX Object Server

    5.0$02-DynamicsAX-Prod-PB-02

1.  If ANY of the services are

    not running, execute the following procedure to restart ALL of the

    services.

Stop all the AOS services using NT service account “******” on servers.  

Dynamics AX Object Server

      AX Object Server 5.0$01-DynamicsAX-Prod-PB-01

Dynamics AX Object Server

      5.0$02-DynamicsAX-Prod-PB-02

Note: These services can sometimes take several minutes to stop.

If the service does not stop after 10 minutes, use Task Manager to

terminate the “Ax32Serv.exe” processes.

Leave all

    AOS services in a stopped status for 5 minutes.

Start all the AOS services

    using NT service account “******” in servers

Dynamics

     AX Object Server 5.0$01-DynamicsAX-Prod-PB-01

Dynamics AX Object Server

     5.0$02-DynamicsAX-Prod-PB-02

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Brandon Wiese Profile Picture
    17,788 on at

    This is a snip from some powershell I once used to copy one environment to another fully automatically.  It stopped and started the target AOS service as part of that process.  Even starting an AX 2012 AOS can take longer than the built-in 5 minute timeout of operations.  

    See if this is helpful.  Note that you can specify the timeout period (60 in my example below) and then check the status for success/failure.

       $targetAosServiceName = "AOS60`$$($record.instance_name)"  # AOS service names format like AOS60$01

       # get the System.ServiceProcess.ServiceController object and the WMI WIN32_Service object

       $targetAosServiceObject = (get-service -ComputerName $targetAosServerName -Name $targetAosServiceName)

       $targetAosServiceWmiObject = (Get-WmiObject -computer $targetAosServerName Win32_Service -Filter "Name='$targetAosServiceName'")

       if ($targetAosServiceObject.status -eq 'Running')  # if the AOS is running

       {

           Write-Host ".. stopping service."

           $timeSpan = new-object system.timespan(0, 0, 60)  # 60 seconds

           $targetAosServiceObject | set-service -StartupType 'Disabled'  # Disable the service to prevent automatic restart.

           $targetAosServiceObject.Stop() | out-null  # Stop the service, but return immediately.

           $targetAosServiceObject.WaitForStatus('Stopped', $timeSpan)  # wait for the service to completely stop

           $targetAosServiceObject.Refresh()  # refresh the current status before checking the status condition      

           if ($targetAosServiceObject.Status -ne 'Stopped')

           {

               Write-Host "Aborting due to failure to stop service $($targetAosServiceObject.ServiceName) on server $($targetAosServiceObject.MachineName)." -foregroundColor White -backgroundColor Red

               Break

           }

           Write-Host ".. stopped."

       }

  • zzackw Profile Picture
    60 on at

    Yes it's very weird and I'm not sure how to explain what I need exactly.

    The gist of it is that I need a script (running as a scheduled task is fine if necessary) that someone (from the helpdesk) can execute weekly or just run as a task.  It should check to see if those two services are running, if they both are, end script.  If both of them are stopped, then restart them and run a check until they are started.  If only of them is stopped, then the other service, whichever one it is, needs to be stopped, wait 5 minutes, then start both services.  There's also the caveat that a process may hang during stopping of either service.  If this occurs, after a 10 minute delay, the process should be killed, wait 5 minutes and then start both services again.  

    This sounds like a lot of scripting work.  It may end up being easier to just have someone manually check the services on both servers.  

  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    Do you have a problem if any particular part? I might be able to help but I don't want to write the whole thing.

    Just few ideas, in addition to Brandon's answer:

    • Two more many ways to check a service status:
      • (gsv $aosName).Status  -eq [System.ServiceProcess.ServiceControllerStatus]::Running
      • (gsv $aosName | select -exp Status) -eq "Running"
    • Waiting can be done by Start-Sleep.
    • You can create background jobs with Start-Job.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans