Payton Flint's Tech Blog
Menu
  • Home
  • Blog
  • Categories
  • Resources
  • About
  • Contact
Menu

Scripted Restart of Service – Azure Update Management

Posted on August 31, 2022June 4, 2023 by paytonflint

If you are using Update Management to handle patching of Windows VMs in Azure, you are bound to see devices display in the “Not Assessed” compliance state from time to time. To remedy this, one can restart the service “Microsoft Monitoring Agent (HealthService).” This service has a corresponding process as well, and I check the start time of the service to ensure it actually restarted. There are many ways one can go about restarting a service, but I opted for a PowerShell script so I can have a report of the information on the corresponding process as well.

Just take a look at that beautifully crafted output. Device names have been intentionally hidden.

Provide a list of devices in the parent folder, and the targeted Service Name in the respective location, and this script will restart the service, and report the Name, Service, Status, and StartType in an array, so you can validate that everything went smoothly.

# Script written by Payton Flint
# See https://paytonflint.com/scripted-restart-of-service-azure-update-management/

# Set target Service Name
$ServiceName = "HealthService"

# Identify location of script
$ScriptPath = Split-Path ($MyInvocation.MyCommand.Path) -Parent

# Set systems list location
$AllSystems = Get-Content "$ScriptPath\DeviceList.txt"

workflow RestartService {
    Param (
        [string[]]$ServiceName,
        [string[]]$ScriptPath,
        [string[]]$AllSystems,
        [string[]]$ItemArray
    )
    
    Foreach -Parallel ($system in $AllSystems) {
    
        inlinescript {
            # Create array
            $ItemArray= @()

            #Get service object
            $ServiceObj = Get-Service -ComputerName $Using:system -Name $Using:ServiceName

            # Gather info on target service for report array
            $ServiceDisplayName = $ServiceObj | Select-Object -ExpandProperty DisplayName
            $ServiceStatus =      $ServiceObj | Select-Object -ExpandProperty Status
            $ServiceStartType =   $ServiceObj | Select-Object -ExpandProperty StartType
 
            # Restart target service
            Write-Output "Restarting $ServiceDisplayName service on $Using:system"
            Restart-Service -InputObject $ServiceObj -Force -Verbose
            Write-Output ""
 
            # Identify process ID by name
            $ProcessID= Get-Process -ComputerName $Using:system -Name $Using:ServiceName | Select-Object -ExpandProperty ID
            # WMI query on targeted win32_process by ID
            $ProcessWMI = Get-WmiObject Win32_Process -ComputerName $Using:system -Filter "ProcessID='$ProcessID'"
            # Convert creation date to date+time
            $CreationDate = $ProcessWMI.ConvertToDateTime($ProcessWMI.CreationDate)
 
            # Append items to array   
            $obj = New-Object PSObject
            $obj | Add-Member -MemberType NoteProperty -Name "Name"               -Value $($Using:system)
            $obj | Add-Member -MemberType NoteProperty -Name "ServiceName"        -Value $($Using:ServiceName)
            $obj | Add-Member -MemberType NoteProperty -Name "ServiceDisplayName" -Value $($ServiceDisplayName)
            $obj | Add-Member -MemberType NoteProperty -Name "Status"             -Value $($ServiceStatus)
            $obj | Add-Member -MemberType NoteProperty -Name "StartType"          -Value $($ServiceStartType)
            $obj | Add-Member -MemberType NoteProperty -Name "ProcessID"          -Value $($ProcessID)
            $obj | Add-Member -MemberType NoteProperty -Name "ProcessStartTime"   -Value $($CreationDate)
            $ItemArray += $obj

            # Display array
            Write-Output $ItemArray
            # Clear/reset array
            $ItemArray = $null
        }
    }
}

RestartService -ServiceName $ServiceName -ScriptPath $ScriptPath -AllSystems $AllSystems

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

About The Author

Author's Portrait

In my journey as a technologist and 11 years of experience as an IT professional, I have found my niche as Director of Infrastructure Services; developing my skillsets in management, scripting, cloud infrastructure, identity management, and networking.

I have experience as a Systems Administrator and Engineer for large enterprises including the DoD, government agencies, and a nuclear-generation site.

I've been blessed to collaborate with engineers at esteemed Fortune 50 corporations, and one of Africa's largest, to ensure successful implementation of my work.

GitHub Button

Credentials

M365 Endpoint Administrator Associate
M365 Fundamentals
Microsoft AZ-900
CompTIA CSIS
CompTIA CIOS
CompTIA Security+
CompTIA Network+
CompTIA A+
  • April 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
© 2022 Payton Flint | The views and opinions expressed on this website belong solely to the author/owner and do not represent the perspectives of any individuals, institutions, or organizations, whether affiliated personally or professionally, unless explicitly stated otherwise. The content and products on this website are provided as-is with no warranties or guaranties, are for informational/demonstrative purposes only, do not constitute professional advice, and are not to be used maliciously. The author/owner is not responsible for any consequences arising from actions taken based on information provided on this website, nor from the use/misuse of products from this site. All trademarks are the property of their respective owners.