Data On Demand windows service enumeration write action

Community.DataOnDemand.WriteAction.GetServices (WriteActionModuleType)

Displays windows services and their status.

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
InputTypeSystem.BaseData

Member Modules:

ID Module Type TypeId RunAs 
WA WriteAction Microsoft.Windows.PowerShellWriteAction Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
Formatstring$Config/Format$Output formatAllowed values: csv, json, text.
TimeoutSecondsint$Config/TimeoutSeconds$Timeout (Seconds)Script timeout in seconds

Source Code:

<WriteActionModuleType ID="Community.DataOnDemand.WriteAction.GetServices" Accessibility="Public" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Format" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TimeoutSeconds" type="xsd:int"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="Format" Selector="$Config/Format$" ParameterType="string"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation>
<Composite>
<MemberModules>
<WriteAction ID="WA" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>Get-Services.ps1</ScriptName>
<ScriptBody><Script>&lt;#
.SYNOPSIS
Community.DataOnDemand windows service enumeration script
.DESCRIPTION
This script enumerates windows services and outputs formatted text
.PARAMETER Format
Permitted values: text, csv, json
.NOTES
Copyright 2016 Squared Up Limited, All Rights Reserved.
#&gt;
Param(
[ValidateSet("text","csv","json","list")]
[string] $Format = "csv"
)

#Requires -Version 2.0
Set-StrictMode -Version 2.0
$ErrorActionPreference = "stop"

$Services = Get-Service

# Get properties of object to be displayed in output (Get-Memeber does not honor order of properties in object)
[System.Collections.ArrayList]$OutPutOrdering = $Services | Get-Member -MemberType AliasProperty,Property | Select-Object -ExpandProperty Name
# Add proprty being sorted, so it will be the first property to be displayed in output(will generate duplicate entry)
$OutPutOrdering.Insert(0,"Name")
# Remove the duplicate from the list of properties (will preserve the first one in the list)
$OutPutOrdering = $OutPutOrdering | Select-Object -Unique

if ($Format -eq 'text')
{
$Services `
| Sort-Object -Property Name `
| Select-Object -Property Name, Status, DisplayName `
| Format-Table -AutoSize `
| Out-String -Width 4096 `
| Write-Host
}
elseif ($Format -eq 'csv')
{
$Services `
| Sort-Object -Property Name `
| Select-Object -Property $OutPutOrdering `
| ConvertTo-Csv -NoTypeInformation `
| Out-String -Width 4096 `
| Write-Host
}
elseif ($Format -eq 'json')
{
$Services `
| Sort-Object -Property Name `
| Select-Object -Property $OutPutOrdering `
| ConvertTo-Json `
| Out-String -Width 4096 `
| Write-Host
}
elseif ($Format -eq 'list')
{
$Services `
| Sort-Object -Property Name `
| Select-Object -Property Name, Status, DisplayName `
| Format-List `
| Out-String -Width 4096 `
| Write-Host
}

# Done. (do not remove blank line following this comment as it can cause problems when script is sent to SCOM agent!)
</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>Format</Name>
<Value>$Config/Format$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="WA"/>
</Composition>
</Composite>
</ModuleImplementation>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>