functions/get-axservice.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

<#
    .SYNOPSIS
        Get the status of the AX 2012 service(s)
         
    .DESCRIPTION
        Get the status of AX 2012 service(s) on the computer
         
    .PARAMETER ComputerName
        Name of the computer(s) that you want to work against
         
    .PARAMETER All
        Instruct the cmdlet to include all known AX 2012 services
         
    .PARAMETER AosInstanceName
        Name of the AOS instance that you are looking for
         
        Accepts wildcards for searching. E.g. -AosInstanceName "*DEV*"
         
        If AxActiveAosConfiguration has been configured, the default value is the name of the instance registered
         
        Default value is otherwise "*" which will search for all AOS instances
         
    .PARAMETER Aos
        Instruct the cmdlet to include the AOS service
         
    .PARAMETER ManagementReporter
        Instruct the cmdlet to include the ManagementReporter service
         
    .PARAMETER DIXF
        Instruct the cmdlet to include the DIXF service
         
    .PARAMETER ScanAllAosServices
        Instruct the cmdlet to look for all available AOS Instances on the computer
         
    .EXAMPLE
        PS C:\> Get-AxService
         
        This will get the status for all the default services from the local computer.
        If AxActiveAosConfiguration has been configured, it will work against the ComputerName and AosInstanceName registered.
         
    .EXAMPLE
        PS C:\> Get-AxService -ScanAllAosServices
         
        This will scan for all available AOS Services.
        If AxActiveAosConfiguration has been configured, it will work against the ComputerName registered otherwise localhost is used.
         
    .EXAMPLE
        PS C:\> Get-AxService -ComputerName TEST-AOS-01 -Aos
         
        This will get all AOS instances (services) from the server named "TEST-AOS-01".
        If AxActiveAosConfiguration has been configured, it will work against the AosInstanceName registered otherwise it will find all.
         
    .EXAMPLE
        PS C:\> Get-AxService -ComputerName TEST-AOS-01 -Aos -AosInstanceName *DEV*
         
        This will get all AOS instances (services) that match the search pattern "*DEV*" from the server named "TEST-AOS-01".
         
    .EXAMPLE
        PS C:\> Get-AxService -ComputerName TEST-AOS-01 -Aos | Start-AxService -ShowOriginalProgress
         
        This will scan the "TEST-AOS-01" server for all AOS instances (services) and start them.
        It will show the status for the service(s) on the server afterwards.
         
        If AxActiveAosConfiguration has been configured, it will work against the AosInstanceName registered otherwise it will find all.
         
    .EXAMPLE
        PS C:\> Get-AxService -ComputerName TEST-AOS-01 -Aos | Stop-AxService -ShowOriginalProgress
         
        This will scan the "TEST-AOS-01" server for all AOS instances (services) and stop them.
        It will show the status for the service(s) on the server afterwards.
         
        If AxActiveAosConfiguration has been configured, it will work against the AosInstanceName registered otherwise it will find all.
         
    .NOTES
        Author: Mötz Jensen (@Splaxi)
         
#>

function Get-AxService {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidDefaultValueSwitchParameter", "")]
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param (
        [Alias('ServerName')]
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string[]] $ComputerName = $Script:ActiveAosComputername,

        [Parameter(ParameterSetName = 'Default')]
        [switch] $All = $true,
        
        [Alias('InstanceName')]
        [string] $AosInstanceName = $(if (-not ([System.String]::IsNullOrEmpty($Script:ActiveAosInstancename))) { "*$Script:ActiveAosInstancename" } else { "*" }),

        [Parameter(ParameterSetName = 'Specific')]
        [switch] $Aos,

        [Parameter(ParameterSetName = 'Specific')]
        [switch] $ManagementReporter,

        [Parameter(ParameterSetName = 'Specific')]
        [switch] $DIXF,

        [switch] $ScanAllAosServices
    )

    begin {
        if ($PSCmdlet.ParameterSetName -eq "Specific") {
            $All = $false
        }
    
        if (!$All -and !$Aos -and !$ManagementReporter -and !$DIXF) {
            Write-PSFMessage -Level Host -Message "You have to use at least one switch when running this cmdlet. Please run the cmdlet again."
            Stop-PSFFunction -Message "Stopping because of missing parameters"
            return
        }

        $baseParams = Get-DeepClone $PSBoundParameters

        $params = @{ }
        $includeParamNames = @("ManagementReporter", "DIXF")

        foreach ($key in $baseParams.Keys) {
            if ($includeParamNames -notcontains $key ) { continue }

            Write-PSFMessage -Level Verbose -Message "Working on key: $key" -Target $key
        
            $null = $params.Add($key, $true)
        }

        if ($params.Count -eq 0) {
            if ($All) {
                $params.AllAxServices = $true

                $Services = @(Get-ServiceList @params)
            }
        }
        else {
            $Services = @(Get-ServiceList @params)
        }

        if ($PSBoundParameters.ContainsKey("Aos")) {
            Write-PSFMessage -Level Verbose -Message "Aos seems to be bound" -Target $key
        
            $searchServicesAos = @(Get-ServiceList -Aos)
        }

    }
    
    process {
        if (Test-PSFFunctionInterrupt) { return }
        
        $res = New-Object System.Collections.ArrayList
    
        foreach ($server in $ComputerName) {
            Write-PSFMessage -Level Verbose -Message "Working against: $server - listing services" -Target ($Services -Join ",")
            Write-PSFMessage -Level Verbose -Message "Working against: $server - listing Aos services" -Target ($searchServicesAos -Join ",")
        
            if ($null -ne $searchServicesAos -and $searchServicesAos.count -gt 0) {

                Write-PSFMessage -Level Verbose -Message "`$searchServicesAos used for searching"

                $colAosServices = Get-Service -ComputerName $server -Name $searchServicesAos -ErrorAction SilentlyContinue | Select-Object @{Name = "Server"; Expression = { $Server } }, Name, Status, DisplayName

                foreach ($service in $colAosServices) {
                    if ((-not $ScanAllAosServices) -and ($service.DisplayName -NotLike $AosInstanceName)) { continue }

                    $null = $res.Add($service)
                }
            }

            if ($null -ne $Services -and $Services.count -gt 0) {
            
                Write-PSFMessage -Level Verbose -Message "`$Services used for searching"

                $axServices = Get-Service -ComputerName $server -Name $Services -ErrorAction SilentlyContinue | Select-Object @{Name = "Server"; Expression = { $Server } }, Name, Status, DisplayName
    
                foreach ($service in $axServices) {
                    if ($service.DisplayName -like "*AX Object Server*" ) {
                        if ((-not $ScanAllAosServices) -and ($service.DisplayName -NotLike $AosInstanceName)) { continue }
                    }

                    $null = $res.Add($service)
                }
            }
        }
        
        $res.ToArray() | Select-Object Server, DisplayName, Status, Name
    }
}