mediumMultiple ChoiceObjective-mapped
220-1102 Practice Question: Write a script that runs a specific command only…
A technician needs to write a script that runs a specific command only if a Windows service is running. If the service is stopped, the script should start it first. Which scripting method is most appropriate?
⚠ Common exam trap
CompTIA often tests the distinction between conditional logic (if-else) and error handling (try-catch), leading candidates to mistakenly choose try-catch because they think it can 'handle' a stopped service, but it cannot evaluate the service state before the command runs.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Use an if-else statement to check the service status.
An if-else statement is the most appropriate scripting method to check the status of a specific Windows service and conditionally execute a command or start the service. In PowerShell, you can use `Get-Service` to retrieve the service status and then an if-else block to evaluate whether the `Status` property equals 'Running'. This provides clear, linear logic that directly matches the requirement without unnecessary complexity.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use a for loop to iterate over all services.
Why it's wrong here
A 'for' loop is designed to iterate over a collection of items or a predefined range, executing the same block of code for each iteration. While it could be used to enumerate all services on a system, it is fundamentally inefficient and inappropriate for the specific task of checking the status of one particular service. The requirement is a direct conditional check on a single entity, not a repetitive scan across multiple entities.
- ✓
Use an if-else statement to check the service status.
Why this is correct
An 'if-else' statement is the most appropriate control structure for evaluating a specific condition, such as whether a service's status is 'Running' or 'Stopped'. It enables the script to execute one block of commands if the condition is true and a different block if it's false. This direct conditional logic perfectly addresses the need to check a service's state and then perform specific actions based on that binary outcome.
- ✗
Use a switch statement with multiple conditions.
Why it's wrong here
A 'switch' statement is best utilized when a variable or expression can have multiple distinct, discrete values, and different actions are required for each specific value. While it could technically be configured to handle two states like 'Running' and 'Stopped', it introduces unnecessary complexity compared to a straightforward 'if-else' for a simple binary check. Therefore, it is an overly elaborate and less efficient choice for this particular scenario.
- ✗
Use a try-catch block to handle errors if the command fails.
Why it's wrong here
A 'try-catch' block is specifically designed for robust error handling, allowing a script to gracefully manage and recover from unexpected exceptions or failures that occur during execution. Its primary function is to react to errors, such as a command failing or a service not being found, rather than proactively determining the current operational status of a service. It does not provide the necessary conditional logic to check a service's state before attempting an action.
Go deeper
Related to this question
Learn chapter
Windows Editions and Features
Key term
PowerShell
PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and a scripting language built on the .NET framework.
Key term
Windows
Windows is a family of operating systems developed by Microsoft that manages computer hardware and software, providing a graphical user interface for users to interact with their devices.
About these practice questions
This 220-1202 question is part of Courseiva's 495-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 220-1202 practice question is part of Courseiva's free CompTIA certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 220-1202 exam.