Skip to content

Powershell script to detect that service has been stopped

Featured Replies

I have a script that shuts the PcaSvc (Program Compatibility Assistant Service) before installing another program. However, I need a script to detect that it's Stopped so I can use it for SCCM AppModel detection.

 

I have this, but I'm getting erros......yes I'm a Powershell newbie......

 

$PcaSvc = Get-WmiObject win32_service | where Name -eq "PcaSvc"
if ($PcaSvc.State -eq "Stopped") {
$true
} else {
$false
}
Please assist...thanks!

It makes no sense to use it for the detection rule, because the status will alter once the service gets started again and your application will start to install once again. You will create a endless loop.

Just create an installation wrapper script that shuts down the service before starting the setup and restart the service when the installer is finished.

 

Anyways, the answer to your question is a simple single line.

 

if ((Get-Service -Name PcaSvc ).Status -eq "Stopped"){Return $true}

 

Any output in a scripted detection counts as success.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.