0
0
PowerShellscripting~10 mins

Configuration drift detection in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AW32Time
BDhcp
CBITS
DSpooler
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong service name that does not exist.
Forgetting to specify the -Name parameter.
2fill in blank
medium

Complete 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'
A$expectedStatus
B'Stopped'
C'Paused'
D'Running'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing to a hardcoded string instead of the variable.
Using quotes around the variable name.
3fill in blank
hard

Fix 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'
A'Running'
BRunning
Crunning
D'running'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted strings causing errors.
Using lowercase 'running' which does not match the exact status.
4fill in blank
hard

Fill 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'
A'Running'
B'Stopped'
C'Paused'
D'Disabled'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted status values.
Mixing up the expected statuses for each service.
5fill in blank
hard

Fill 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'
AStatus
B$currentStatus
C$expectedConfig[$service]
DName
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property names like 'Name' instead of 'Status'.
Not using variables correctly in the output string.