Public/AppPool.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
<#
.SYNOPSIS
    Test an Application Pool
.DESCRIPTION
    Used To Determine if Application Pool is Running and Validate Various Properties
.PARAMETER Target
    The name of the App Pool to be Tested
.PARAMETER Property
    The Property to be expanded. If Ommitted, Property Will Default to Status. Can handle nested objects within properties
.PARAMETER Should
    A Script Block defining a Pester Assertion.
.EXAMPLE
    AppPool TestSite { Should be Started }
.EXAMPLE
    AppPool TestSite ManagedPipelineMode { Should be 'Integrated' }
.EXAMPLE
    AppPool TestSite ProcessModel.IdentityType { Should be 'ApplicationPoolIdentity'}
.NOTES
    Assertions: be
#>

function AppPool {
    [CmdletBinding(DefaultParameterSetName="Default")]
    param(
        [Parameter(Mandatory, Position=1, ParameterSetName="Default")]
        [Parameter(Mandatory, Position=1, ParameterSetName="Property")]
        [Alias("Path")]
        [string]$Target,

        [Parameter(Position=2, ParameterSetName="Property")]
        [string]$Property,

        [Parameter(Mandatory, Position=2, ParameterSetName="Default")]
        [Parameter(Mandatory, Position=3, ParameterSetName="Property")]
        [scriptblock]$Should
    )

    $IISAdmin = Get-Module -Name 'IISAdministration'
    if ($IISAdmin) {
      Import-Module IISAdministration
    }
    else {
      . $GetIISAppPool
    }

    $expression = $null
    $params = $null

    if (-not $PSBoundParameters.ContainsKey('Property')) {
      $Property = 'State'
      $PSBoundParameters.add('Property', $Property)
      $expression = { Get-IISAppPool -Name '$Target' -ErrorAction SilentlyContinue }
      $params = Get-PoshspecParam -TestName AppPool -TestExpression $expression @PSBoundParameters
    }

    if ($Property -like '*.*' -or $Property -like '*(*' -or $Property -like '*)*') {
      $expression = { Get-IISAppPool -Name '$Target' -ErrorAction SilentlyContinue }
      $params = Get-PoshspecParam -TestName AppPool -TestExpression $expression -Target $Target -Should $Should -PropertyExpression $Property
    }

    else {
      $expression = { Get-IISAppPool -Name '$Target' -ErrorAction SilentlyContinue }
      $params = Get-PoshspecParam -TestName AppPool -TestExpression $expression @PSBoundParameters
    }

    Invoke-PoshspecExpression @params
}

$GetIISAppPool = {
  function Get-IisAppPool
  {
    [CmdletBinding()]
    Param
    (
      [Parameter(Mandatory=$true,
                 Position=1)]
      [string]$Name
    )

    Begin
    {
      [System.Reflection.Assembly]::LoadFrom("$($Env:windir)\system32\inetsrv\Microsoft.Web.Administration.dll") | Out-Null
      $ServerManager = [Microsoft.Web.Administration.ServerManager]::OpenRemote("localhost")
    }

    Process
    {
      try
      {
        Write-Verbose "Getting application pool $Name"
        Write-Output $ServerManager.ApplicationPools[$Name]
      }

      catch
      {
        Write-Warning $Error[0]
      }
    }

    End
    {
      $ServerManager.Dispose()
    }
  }
}