Get-IISSite.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
<#
.Synopsis
   Get IIS Site information from a local or remote computer.
.DESCRIPTION
   Get IIS Site information from a local or remote computer.
.NOTES
   Created by: Jason Wasser @wasserja
   Modified: 5/3/2017 02:13:17 PM
.EXAMPLE
   Get-IISSite
.EXAMPLE
   Get-IISSite -ComputerName webserver01
#>

function Get-IISSite
{
    [CmdletBinding()]
    Param
    (
        # ComputerName
        [parameter(ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string[]]$ComputerName = $env:COMPUTERNAME,

        # Credential
        [System.Management.Automation.PSCredential]$Credential=[System.Management.Automation.PSCredential]::Empty
    )

    Begin
    {
        
        # Helper Function to do the work of gathering site detail information.
        function Get-IISSiteInformation {
            #$VerbosePreference = 'Continue'
            try {
                Import-Module -Name WebAdministration -ErrorAction Stop
                $Websites = Get-ChildItem IIS:\Sites

                foreach ($Site in $Websites) {
                    Write-Verbose "$($Site.Name) is $($Site.State)"
                    Write-Verbose "$($Site.PhysicalPath)"
    
                    Write-Verbose "Gathering site binding information for $($Site.Name)."
                    [string[]]$Bindings = @()
                    foreach ($Binding in $Site.bindings.Collection) {
                        Write-Verbose "$($Binding.BindingInformation)"
                        $Bindings += $Binding.Protocol + ': ' + $Binding.BindingInformation
                        }

                    # Gather Application Pool Information
                    Write-Verbose -Message 'Gather Application Pool Information'
                    $AppPool = Get-Item -Path "IIS:\AppPools\$($Site.ApplicationPool)"
                    
                    # Creating hash table for object properties
                    if ($PSVersionTable.PSVersion.Major -lt 3) {
                        $SiteProperties = @{
                            Name = $Site.name
                            ID = $Site.ID
                            State = $Site.state
                            PhysicalPath = $Site.physicalPath
                            Bindings = $Bindings
                            AppPool = $Site.ApplicationPool
                            ManagedRunTimeVersion = $AppPool.managedRuntimeVersion
                            AppPoolState = $AppPool.state
                            AppPoolIdentityType = $AppPool.processmodel.identityType
                            AppPoolUsername = $AppPool.processmodel.userName
                            ComputerName = $env:COMPUTERNAME
                            }
                        }
                    else {
                        $SiteProperties = [ordered]@{
                            Name = $Site.name
                            ID = $Site.ID
                            State = $Site.state
                            PhysicalPath = $Site.physicalPath
                            Bindings = $Bindings
                            AppPool = $Site.ApplicationPool
                            ManagedRunTimeVersion = $AppPool.managedRuntimeVersion
                            AppPoolState = $AppPool.state
                            AppPoolIdentityType = $AppPool.processmodel.identityType
                            AppPoolUsername = $AppPool.processmodel.userName
                            ComputerName = $env:COMPUTERNAME
                            }
                        }
                    
                    $Site = New-Object -TypeName pscustomobject -Property $SiteProperties
                    if ($PSVersionTable.PSVersion.Major -lt 3) {
                        $Site | Select-Object -Property Name,ID,State,PhysicalPath,Bindings,AppPool,ManagedRunTimeVersion,AppPoolState,AppPoolIdentityType,AppPoolUsername,ComputerName
                        }
                    else {
                        $Site
                        }
                    
                    }
                }
            catch {
                Write-Error $Error[0].Exception.Message
                }
            }

    }
    Process
    {
        
        # Loop through each supplied computer name.
        foreach ($Computer in $ComputerName) {
            
            # Run function locally if localhost.
            if ($Computer -eq $env:COMPUTERNAME) {
                Write-Verbose 'Getting IIS website information from localhost'
                Get-IISSiteInformation
                }
            # Run function via Invoke-Command on remote computers.
            else {
                Write-Verbose "Getting IIS website information from remote computer $Computer"
                Invoke-Command -ScriptBlock ${function:Get-IISSiteInformation} -ComputerName $Computer -Credential $Credential
                }
            }
    }
    End
    {
    }
}