functions/get-d365environment.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

<#
    .SYNOPSIS
        Cmdlet to get the current status for the different services in a Dynamics 365 Finance & Operations environment
         
    .DESCRIPTION
        List status for all relevant services that is running in a D365FO environment
         
    .PARAMETER ComputerName
        An array of computers that you want to query for the services status on.
         
    .PARAMETER All
        Set when you want to query all relevant services
         
        Includes:
        Aos
        Batch
        Financial Reporter
        DMF
         
    .PARAMETER Aos
        Instruct the cmdlet to query the AOS (IIS) service
         
    .PARAMETER Batch
        Instruct the cmdlet query the batch service
         
    .PARAMETER FinancialReporter
        Instruct the cmdlet query the financial reporter (Management Reporter 2012)
         
    .PARAMETER DMF
        Instruct the cmdlet query the DMF service
         
    .PARAMETER OnlyStartTypeAutomatic
        Instruct the cmdlet to filter out services that are set to manual start or disabled
         
    .PARAMETER OutputServiceDetailsOnly
        Instruct the cmdlet to exclude the server name from the output
         
    .EXAMPLE
        PS C:\> Get-D365Environment
         
        Will query all D365FO service on the machine.
         
    .EXAMPLE
        PS C:\> Get-D365Environment -All
         
        Will query all D365FO service on the machine.
         
    .EXAMPLE
        PS C:\> Get-D365Environment -OnlyStartTypeAutomatic
         
        Will query all D365FO service on the machine.
        It will filter out all services that are either configured as manual or disabled.
         
    .EXAMPLE
        PS C:\> Get-D365Environment -ComputerName "TEST-SB-AOS1","TEST-SB-AOS2","TEST-SB-BI1" -All
         
        Will query all D365FO service on the different machines.
         
    .EXAMPLE
        PS C:\> Get-D365Environment -Aos -Batch
         
        Will query the Aos & Batch services on the machine.
         
    .EXAMPLE
        PS C:\> Get-D365Environment -FinancialReporter -DMF
         
        Will query the FinancialReporter & DMF services on the machine.
         
    .EXAMPLE
        PS C:\> Get-D365Environment -OutputServiceDetailsOnly
         
        Will query all D365FO service on the machine.
        Will omit the servername from the output.
         
    .EXAMPLE
        PS C:\> Get-D365Environment -FinancialReporter | Set-Service -StartupType Manual
         
        This will configure the Financial Reporter services to be start type manual.
         
    .NOTES
        Tags: Environment, Service, Services, Aos, Batch, Servicing
         
        Author: Mötz Jensen (@Splaxi)
         
#>

function Get-D365Environment {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidDefaultValueSwitchParameter", "")]
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    
    param (
        [Parameter(Mandatory = $false, ParameterSetName = 'Default', Position = 1 )]
        [Parameter(Mandatory = $false, ParameterSetName = 'Specific', Position = 1 )]
        [string[]] $ComputerName = @($env:computername),

        [Parameter(Mandatory = $false, ParameterSetName = 'Default')]
        [switch] $All = $true,

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

        [Parameter(Mandatory = $false, ParameterSetName = 'Specific')]
        [switch] $Batch,

        [Parameter(Mandatory = $false, ParameterSetName = 'Specific')]
        [switch] $FinancialReporter,

        [Parameter(Mandatory = $false, ParameterSetName = 'Specific')]
        [switch] $DMF,

        [switch] $OnlyStartTypeAutomatic,

        [switch] $OutputServiceDetailsOnly
    )

    if ($PSCmdlet.ParameterSetName -eq "Specific") {
        $All = $false
    }

    if ( (-not ($All)) -and (-not ($Aos)) -and (-not ($Batch)) -and (-not ($FinancialReporter)) -and (-not ($DMF))) {
        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
    }

    $Params = Get-DeepClone $PSBoundParameters
    if($Params.ContainsKey("ComputerName")){$null = $Params.Remove("ComputerName")}
    if($Params.ContainsKey("OutputServiceDetailsOnly")){$null = $Params.Remove("OutputServiceDetailsOnly")}
    if($Params.ContainsKey("OnlyStartTypeAutomatic")){$null = $Params.Remove("OnlyStartTypeAutomatic")}

    $Services = Get-ServiceList @Params

    $Results = foreach ($server in $ComputerName) {
        Get-Service -ComputerName $server -Name $Services -ErrorAction SilentlyContinue | Select-Object @{Name = "Server"; Expression = {$Server}}, Name, Status, StartType, DisplayName
    }
    
    $outputTypeName = "D365FO.TOOLS.Environment.Service"

    if($OutputServiceDetailsOnly) {
        $outputTypeName = "D365FO.TOOLS.Environment.Service.Minimal"
    }

    if($OnlyStartTypeAutomatic){
        $Results = $Results | Where-Object StartType -eq "Automatic"
    }

    $Results | Select-PSFObject -TypeName $outputTypeName Server, DisplayName, Status, StartType, Name
}