Class: Inspec::Resources::WindowsSrv
- Inherits:
-
ServiceManager
- Object
- ServiceManager
- Inspec::Resources::WindowsSrv
- Defined in:
- lib/resources/service.rb
Overview
Determine the service state from Windows Uses Powershell to retrieve the information
Instance Attribute Summary
Attributes inherited from ServiceManager
Instance Method Summary collapse
-
#info(service_name) ⇒ Object
Determine service details PS: Get-Service -Name ‘dhcp’| Select-Object -Property Name, DisplayName, Status | ConvertTo-Json { “Name”: “dhcp”, “DisplayName”: “DHCP Client”, “Status”: 4 }.
Methods inherited from ServiceManager
Constructor Details
This class inherits a constructor from Inspec::Resources::ServiceManager
Instance Method Details
#info(service_name) ⇒ Object
Determine service details PS: Get-Service -Name ‘dhcp’| Select-Object -Property Name, DisplayName, Status | ConvertTo-Json
"Name": "dhcp",
"DisplayName": "DHCP Client",
"Status": 4
Until StartMode is not added to Get-Service, we need to do a workaround @see: connect.microsoft.com/PowerShell/feedback/details/424948/i-would-like-to-see-the-property-starttype-added-to-get-services Use the following powershell to determine the start mode PS: Get-WmiObject -Class Win32_Service | Where-Object -eq $name -or $_.DisplayName -eq $name | Select-Object -Prop erty Name, StartMode, State, Status | ConvertTo-Json
"Name": "Dhcp",
"StartMode": "Auto",
"State": "Running",
"Status": "OK"
Windows Services have the following status code: @see: msdn.microsoft.com/en-us/library/windows/desktop/ms685996(v=vs.85).aspx
-
1: Stopped
-
2: Starting
-
3: Stopping
-
4: Running
-
5: Continue Pending
-
6: Pause Pending
-
7: Paused
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'lib/resources/service.rb', line 529 def info(service_name) cmd = inspec.command("New-Object -Type PSObject | Add-Member -MemberType NoteProperty -Name Service -Value (Get-Service -Name #{service_name}| Select-Object -Property Name, DisplayName, Status) -PassThru | Add-Member -MemberType NoteProperty -Name WMI -Value (Get-WmiObject -Class Win32_Service | Where-Object {$_.Name -eq '#{service_name}' -or $_.DisplayName -eq '#{service_name}'} | Select-Object -Property StartMode) -PassThru | ConvertTo-Json") # cannot rely on exit code for now, successful command returns exit code 1 # return nil if cmd.exit_status != 0 # try to parse json begin service = JSON.parse(cmd.stdout) rescue JSON::ParserError => _e return nil end # check that we got a response return nil if service.nil? || service['Service'].nil? { name: service['Service']['Name'], description: service['Service']['DisplayName'], installed: true, running: service_running?(service), enabled: service_enabled?(service), type: 'windows', } end |