Get-SitecoreSite.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
#Set-StrictMode -Version Latest
#####################################################
# Get-SitecoreSite
#####################################################
<#PSScriptInfo
 
.VERSION 0.3
 
.GUID 731386ca-0f32-4eea-ac72-0b67f84ede51
 
.AUTHOR David Walker, Sitecore Dave, Radical Dave
 
.COMPANYNAME David Walker, Sitecore Dave, Radical Dave
 
.COPYRIGHT David Walker, Sitecore Dave, Radical Dave
 
.TAGS powershell sitecore iis
 
.LICENSEURI https://github.com/SharedSitecore/Get-SitecoreSite/blob/main/LICENSE
 
.PROJECTURI https://github.com/SharedSitecore/Get-SitecoreSite
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
#>


<#
 
.DESCRIPTION
Get-SitecoreSite by name - default return list of all Sitecore sites
 
.PARAMETER name
Name of site - if empty returns list of all Sitecore sites within wwwroot
 
.PARAMETER wwwroot
Path of IIS Inetpub WWWROOT - if empty reads from registry
 
.PARAMETER mode
Mode of operation - default: registry/filesystem, requires admin: IISAdministration and Microsoft.Web.Administration.dll
 
.EXAMPLE
PS> .\Get-SitecoreSite
 
.EXAMPLE
PS> .\Get-SitecoreSite 'sitename in iis'
 
#>
 
#####################################################
# Get-SitecoreSite
#####################################################
Param(
    # Name of Sitecore Site in IIS
    [Parameter(Mandatory = $false, position=0)] [string]$name,
    [Parameter(Mandatory = $false, position=1)] [string]$wwwroot,
    [Parameter(Mandatory = $false, position=2)] [string]$mode = 'registry'
)
begin {
    $ProgressPreference = 'SilentlyContinue'
    $ErrorActionPreference = 'Stop'
    $PSScriptVersion = (Test-ScriptFileInfo -Path $MyInvocation.MyCommand.Path | Select-Object -ExpandProperty Version)
    $PSScriptName = ($MyInvocation.MyCommand.Name.Replace(".ps1",""))
    $PSCallingScript = if ($MyInvocation.PSCommandPath) { $MyInvocation.PSCommandPath | Split-Path -Parent } else { $null }
    Write-Verbose "$PSScriptRoot\$PSScriptName v$PSScriptVersion $name $mode called by:$PSCallingScript"
}
process {
    Write-Verbose "$PSScriptName $name $mode start"
    
    switch ($mode) {
        'registry' { 
            if (!$wwwroot) {$wwwroot = (Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp -Name "PathWWWRoot").PathWWWRoot}
            Write-Verbose "wwwroot:$wwwroot"
            if (!$name) {
                [array]$sites = @(Get-ChildItem $wwwroot -Directory | ForEach-Object { $_.FullName})
            } else {
                if ((Test-Path "$name")) {
                    [array]$sites = @($name)
                } else {
                    [array]$sites = @(Get-ChildItem $wwwroot -Directory -Filter $name | ForEach-Object { $_.FullName})
                }
            }
        }
        'iis' { 
            try {
                $command = Get-Command -Name Get-IISSite #'IISAdministration'
            } catch {
            }
            if (!$command) {
                Install-Module IISAdministration -Confirm:$False -Force -Scope AllUsers
                Import-Module IISAdministration -Force -Scope Global
            }
    
            if (!$name) {
                $sites = Get-IISSite
                #$sites = Get-website | select name,id,state,physicalpath
            } else {
                $sites = Get-IISSite $name
                #$sites = Get-website $name | select name,id,state,physicalpath
            }
            #$path = $site.physicalpath
        }
        'dll' { 
            [Void][Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
            #[Void][Reflection.Assembly]::UnsafeLoadFrom("Microsoft.Web.Administration")
            
            $server = New-Object Microsoft.Web.Administration.ServerManager
            $sites = @()
            foreach($site in $server.Sites) {
                foreach ($app in $site.Applications) {
                    if ($site.Name -like $name) {
                        sites.Add($app.VirtualDirectories["/"].PhysicalPath)
                    }
                }
            }
        }
    }

    if (!$sites) {
        throw "$PSScriptName ERROR no sites found in IIS named:$name"
    }
    Write-Verbose "sites:$($sites -join ',')"
    #$results = @()
    $results = [System.Collections.ArrayList]$results = @();
    #$collection = {$results}.Invoke()
    foreach($site in $sites)
    {
        if (!$site) {
            Write-Verbose "$PSScriptName NO site/path!"
        } else {
            if (!(Test-Path ($site))) {
                Write-Verbose "$PSScriptName ERROR site not found? $site"
            } else {
                $sitecoreSite = Test-Path (Join-Path $site '/bin/Sitecore.Kernel.dll')
                #Write-Host "$($site):$sitecoreSite"
                if ($sitecoreSite) { $results.Add($site) | Out-Null } else {Write-Verbose "SKIP $site - Sitecore.Kernel.dll NOT FOUND"}
            }
        }
    }
    Write-Verbose "results:$($results -join ',')"
    Write-Verbose "$PSScriptName $name end"
    return @($results)
}