DSCResources/MSFT_SPServiceAppPool/MSFT_SPServiceAppPool.psm1
function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [parameter(Mandatory = $true)] [System.String] $Name, [parameter(Mandatory = $true)] [System.String] $ServiceAccount, [parameter(Mandatory = $false)] [ValidateSet("Present","Absent")] [System.String] $Ensure = "Present", [parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount ) Write-Verbose -Message "Getting service application pool '$Name'" $result = Invoke-SPDSCCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { $params = $args[0] $sap = Get-SPServiceApplicationPool -Identity $params.Name -ErrorAction SilentlyContinue if ($null -eq $sap) { return @{ Name = $params.Name ServiceAccount = $params.ProcessAccountName InstallAccount = $params.InstallAccount Ensure = "Absent" } } return @{ Name = $sap.Name ServiceAccount = $sap.ProcessAccountName InstallAccount = $params.InstallAccount Ensure = "Present" } } return $result } function Set-TargetResource { [CmdletBinding()] param ( [parameter(Mandatory = $true)] [System.String] $Name, [parameter(Mandatory = $true)] [System.String] $ServiceAccount, [parameter(Mandatory = $false)] [ValidateSet("Present","Absent")] [System.String] $Ensure = "Present", [parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount ) $CurrentValues = Get-TargetResource @PSBoundParameters Write-Verbose -Message "Creating service application pool '$Name'" if ($CurrentValues.Ensure -eq "Absent" -and $Ensure -eq "Present") { Write-Verbose -Message "Creating Service Application Pool $Name" Invoke-SPDSCCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { $params = $args[0] New-SPServiceApplicationPool -Name $params.Name -Account $params.ServiceAccount $sap = Get-SPServiceApplicationPool -Identity $params.Name -ErrorAction SilentlyContinue if ($null -eq $sap) { } else { if ($sap.ProcessAccountName -ne $params.ServiceAccount) { Set-SPServiceApplicationPool -Identity $params.Name -Account $params.ServiceAccount } } } } if ($CurrentValues.Ensure -eq "Present" -and $Ensure -eq "Present") { Write-Verbose -Message "Updating Service Application Pool $Name" Invoke-SPDSCCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { $params = $args[0] $sap = Get-SPServiceApplicationPool -Identity $params.Name -ErrorAction SilentlyContinue if ($sap.ProcessAccountName -ne $params.ServiceAccount) { Set-SPServiceApplicationPool -Identity $params.Name -Account $params.ServiceAccount } } } if ($Ensure -eq "Absent") { Write-Verbose -Message "Removing Service Application Pool $Name" Invoke-SPDSCCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { $params = $args[0] Remove-SPServiceApplicationPool -Identity $params.Name -Confirm:$false } } } function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( [parameter(Mandatory = $true)] [System.String] $Name, [parameter(Mandatory = $true)] [System.String] $ServiceAccount, [parameter(Mandatory = $false)] [ValidateSet("Present","Absent")] [System.String] $Ensure = "Present", [parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount ) $CurrentValues = Get-TargetResource @PSBoundParameters Write-Verbose -Message "Testing service application pool '$Name'" $PSBoundParameters.Ensure = $Ensure if ($Ensure -eq "Present") { return Test-SPDSCSpecificParameters -CurrentValues $CurrentValues -DesiredValues $PSBoundParameters -ValuesToCheck @("ServiceAccount", "Ensure") } else { return Test-SPDSCSpecificParameters -CurrentValues $CurrentValues -DesiredValues $PSBoundParameters -ValuesToCheck @("Ensure") } } Export-ModuleMember -Function *-TargetResource |