Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the current configuration of a service named 'Spooler'.
PowerShell
Get-Service -Name [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong service name that does not exist.
Forgetting to specify the -Name parameter.
✗ Incorrect
The 'Spooler' service manages print jobs. Using Get-Service with -Name 'Spooler' retrieves its current status.
2fill in blank
mediumComplete the code to compare the current service status with the expected status stored in $expectedStatus.
PowerShell
$currentStatus = (Get-Service -Name 'Spooler').Status; if ($currentStatus -eq [1]) { Write-Output 'No drift detected' } else { Write-Output 'Configuration drift detected' }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing to a hardcoded string instead of the variable.
Using quotes around the variable name.
✗ Incorrect
We compare the current status to the expected status stored in the variable $expectedStatus to detect drift.
3fill in blank
hardFix the error in the code that checks if the 'Spooler' service is running.
PowerShell
if ((Get-Service -Name 'Spooler').Status -eq [1]) { Write-Output 'Service is running' } else { Write-Output 'Service is not running' }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted strings causing errors.
Using lowercase 'running' which does not match the exact status.
✗ Incorrect
The Status property returns a string, so the comparison value must be a string literal with quotes, like 'Running'.
4fill in blank
hardFill both blanks to create a hashtable that stores service names as keys and their expected statuses as values.
PowerShell
$expectedConfig = @{ 'Spooler' = [1]; 'W32Time' = [2] } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted status values.
Mixing up the expected statuses for each service.
✗ Incorrect
The 'Spooler' service is expected to be 'Running' and 'W32Time' expected to be 'Stopped' in this example.
5fill in blank
hardFill all three blanks to create a script that detects configuration drift by comparing current service statuses to expected ones.
PowerShell
$expectedConfig = @{ 'Spooler' = 'Running'; 'W32Time' = 'Stopped' }
foreach ($service in $expectedConfig.Keys) {
$currentStatus = (Get-Service -Name $service).[1]
if ($currentStatus -ne $expectedConfig[$service]) {
Write-Output "$service has drifted: current status is [2], expected is [3]"
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property names like 'Name' instead of 'Status'.
Not using variables correctly in the output string.
✗ Incorrect
We get the current service Status property, then compare it to the expected status from the hashtable, and output both values if they differ.