SharePoint.2013.Toolbox.psm1

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
function Set-SP2013SuiteBarText{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true,Position=1)] [string]$WebApp,
        [Parameter(Mandatory = $true,Position=2)] [string]$Text,
        [Parameter(Mandatory = $false,Position=3)] [string]$Link = $null
    )
    Try {
        $ver = $host | Select-Object version
        if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 
        if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction Stop) -eq $null) {
            Write-Verbose "Adding SharePoint PowerShell Snapin"
            Add-PSSnapin "Microsoft.SharePoint.PowerShell"
        }
    }
    Catch [System.Exception]{
        Write-Warning "SharePoint PowerShell Snapin is not found"
    }
    
    $spWebapp = Get-SPWebApplication -Identity $WebApp 
    if ($Link -ne $null)
    {
        $spWebapp.SuiteBarBrandingElementHtml = '<div class="ms-core-brandingText" style="font-size: 15px !important; padding-left: 10px;"><a href="'+$Link+'" class="ms-core-suiteLink-a"><span>'+$Text+'</span></a></div>'
    }
    else
    {
        $webspWebappApp.SuiteBarBrandingElementHtml = '<div class="ms-core-brandingText">'+$Text+'</div>'
    }    
    $spWebapp.Update()
}

function Set-SP2013SiteLogo{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$True,Position=1)] [string]$SiteURL,
        [Parameter(Mandatory=$False,Position=2)] [string]$SiteLogoUrl = "",
        [Parameter(Mandatory=$False,Position=3)] [string]$SiteLogoDescription = ""
    )
    Try {
        $ver = $host | Select-Object version
        if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 
        if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction Stop) -eq $null) {
            Write-Verbose "Adding SharePoint PowerShell Snapin"
            Add-PSSnapin "Microsoft.SharePoint.PowerShell"
        }
    }
    Catch [System.Exception]{
        Write-Warning "SharePoint PowerShell Snapin is not found"
    }
    
    $SPSite = Get-SPSite $SiteURL
    $SPSite.AllWebs | ForEach-Object { $_.SiteLogoUrl = $SiteLogoUrl; $_.SiteLogoDescription = $SiteLogoDescription; $_.Update() }
}

function Set-SP2013PoralLink{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$True,Position=1)] [string]$SiteURL,
        [Parameter(Mandatory=$True,Position=2)] [string]$PortalUrl,
        [Parameter(Mandatory=$True,Position=3)] [string]$PortalName
    )
    Try {
        $ver = $host | Select-Object version
        if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 
        if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction Stop) -eq $null) {
            Write-Verbose "Adding SharePoint PowerShell Snapin"
            Add-PSSnapin "Microsoft.SharePoint.PowerShell"
        }
    }
    Catch [System.Exception]{
        Write-Warning "SharePoint PowerShell Snapin is not found"
    }
    
    $SPSite = Get-SPSite $SiteURL
    $SPSite.PortalName = $PortalName 
    $SPSite.PortalUrl = $PortalUrl
}

function Start-SP2013Farm{
    $Host.Runspace.ThreadOptions = "ReuseThread"
    Start-SPAssignment -Global
    $SPWebApps = Get-SPWebApplication -IncludeCentralAdministration
    foreach ($SPWebApp in $SPWebApps){
        foreach ($SPSite in $SPWebApp.Sites) { 
            foreach ($spWeb in $SPSite.AllWebs) {
                Write-Host -NoNewline "Warming up "$spWeb.url
                Try{
                    $r = Invoke-WebRequest -URI $spWeb.url -UseDefaultCredentials
                    if ($r.StatusCode -ne 200) { throw [System.IO.FileNotFoundException] $spWeb.Url + "Status Code is" + $r.StatusCode }
                    Write-Host -ForegroundColor Green " .....Done!"
                } Catch {
                    Write-Host -ForegroundColor Red " .....Error!"
                }
            }
        }
    }
    Stop-SPAssignment -Global
}

function Set-SP2013MaintenanceMode {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$false)][switch]$Online
    )
    Function Set-OfflinePage() {
        Param(
            [Parameter(Position=0, Mandatory=$true)][string]$Location
        )
        $file = $Location+"\app_offline.htm"
        Out-File -FilePath "\app_offline.htm"
        $html = "<html><head><title>SharePoint is currently offline</title></head>"
        $html += "<body><h1 style='text-align:center'>SharePoint is currently undergoing maintenance</h1></body>"
        $html += "</html>"
        Add-Content -Path $file -Value $html
    }

    Start-SPAssignment -Global
    $spWebApplications = Get-SPWebApplication
    if ($Online) {
        foreach ($spWebApplication in $spWebApplications){
            try {
                Write-Host -NoNewline "Putting"$spWebApplication.url"back online"
                $IISSettings = $spWebApplication.IisSettings[[Microsoft.SharePoint.Administration.SPUrlZone]::Default]
                $file = $IISSettings.Path.FullName+"\app_offline.htm"
                Remove-Item $file
                Write-Host -ForegroundColor Green ".....Done!"
            }
            catch {
                Write-Host -ForegroundColor Red ".....Error!"
            }
        }
        Start-SP2013Farm
    }
    else {
        foreach ($spWebApplication in $spWebApplications){
            try {
                Write-Host -NoNewline "Taking"$spWebApplication.url"offline"
                $IISSettings = $spWebApplication.IisSettings[[Microsoft.SharePoint.Administration.SPUrlZone]::Default]
                Set-OfflinePage $IISSettings.Path.FullName
                Write-Host -ForegroundColor Green " .....Done!"
            }
            catch {
                Write-Host -ForegroundColor Red ".....Error!"
            }
        }
    }
    Stop-SPAssignment -Global
}

function Backup-SP2013Farm{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$True,Position=1)]
        [string]$Path,
        [Parameter(Mandatory=$True,Position=2)]
        [int]$RetentionDays
    )
    Function Backup-SPSiteCollection {
        Param(
            [Parameter(Position=0, Mandatory=$true)][string]$Location,
            [Parameter(Position=0, Mandatory=$true)][string]$URL,
            [Parameter(Position=0, Mandatory=$true)][string]$SiteTitle
        )
        Try {
            Write-Host -NoNewline "Backing up Site Collection $SiteTitle"
            Backup-SPSite $URL -Path $Location
            Write-Host -ForegroundColor Green ".....Done!"
            Write-Host -ForegroundColor Yellow "Backup file located at $Location"
        }
        Catch {

        }
    }
    Start-SPAssignment -Global
    $SPWebApps = Get-SPWebApplication
    $BackupRetentionDate = (Get-Date).AddDays(-$RetentionDays)
    $currentDate = Get-Date
    $bkDate = $currentDate.Year.ToString("0000") + $currentDate.Month.ToString("00") + $currentDate.Day.ToString("00")
    foreach ($SPWebApp in $SPWebApps){
        Write-Host -ForegroundColor Yellow "Starting Backup of Site Collections in "$SPWebApp.URL
        $spWebAppBackupFolder = $SPWebApp.URL.Replace("http://","").Replace("https://","").Replace(".","-").Replace("/","\")
        $bkLocation = $Path + '\' + $spWebAppBackupFolder
        if(!(Test-Path -Path $bkLocation )){ New-Item -ItemType directory -Path $bkLocation | Out-Null }
        Get-ChildItem -Path $bkLocation -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $BackupRetentionDate } | Remove-Item -Force
        foreach ($SPSite in $SPWebApp.Sites) { 
        if ($SPSite.ServerRelativeUrl -eq "/") { $bkFileName = $bkDate + "-root.bak" }
        else { $bkFileName = $bkDate + $SPSite.ServerRelativeUrl.Replace(".","-").Replace("/","-").Replace(" ","-") + ".bak" }
        $bkFile = $bkLocation + $bkFileName
        Backup-SPSiteCollection -Location $bkFile -URL $SPSite.URL -SiteTitle $SPSite.RootWeb.Title
        $SPSite.Dispose()        
        }
        Write-Host "Backup of Site Collections in "$SPWebApp.URL" Completed"
    }
}

function Get-SP2013Permissions{
    [CmdletBinding()]
    param(
        [Parameter(Position=1,Mandatory=$true)][string]$Site,
        [Parameter(Position=2,Mandatory=$true)][string]$Output
    )
    Try {
        $ver = $host | Select-Object version
        if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 
        if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction Stop) -eq $null) {
            Write-Verbose "Adding SharePoint PowerShell Snapin"
            Add-PSSnapin "Microsoft.SharePoint.PowerShell"
        }
    }
    Catch [System.Exception]{
        Write-Warning "SharePoint PowerShell Snapin is not found"
    }

    $results = @()
    $spSite = Get-SPSite $Site
    Try{
        Write-Host -NoNewline "Getting Site Collections Administrators for"$spSite.URL
        if ($spSite.Owner -ne $null)
        {
            $details = @{
                SiteName    = $spWeb.Title
                SiteURL     = $spWeb.URL
                Group       = "Site Owner"      
                Role        = "Site Collection Administrator"
                User        = $spSite.Owner.DisplayName             
                UserName    = $spSite.Owner.UserLogin.Replace("i:0#.w|","")
                Inherited   = $false
            }                           
            New-Object PSObject -Property $details  | export-csv -Path $Output -NoTypeInformation -Append  
        }
        if ($spSite.SecondaryContact -ne $null)
        {
            $details = @{
                SiteName    = $spWeb.Title
                SiteURL     = $spWeb.URL
                Group       = "Site Owner"
                Role        = "Site Collection Administrator"      
                User        = $spSite.SecondaryContact.DisplayName             
                UserName    = $spSite.SecondaryContact.UserLogin.Replace("i:0#.w|","")
                Inherited   = $false
            }                           
            New-Object PSObject -Property $details  | export-csv -Path $Output -NoTypeInformation -Append  
        }
        Write-Host -f Green " .....Dome!"
    }
    Catch
    {
        Write-Host -f Red " .....Error!"
    }

    foreach ($spWeb in $spSite.AllWebs)
    {
        Write-Host -NoNewline "Getting Users and Permissions for"$spWeb.URL
        if ($spWeb.IsRootWeb)
        {
            foreach ($spSiteAdmin in $spWeb.SiteAdministrators)
            {
                if ($spSiteAdmin.UserLogin -ne $spSite.Owner.UserLogin -and 
                    $spSiteAdmin.UserLogin -ne $spSite.SecondaryContact.UserLogin)
                {
                    $details = @{
                        SiteName    = $spWeb.Title
                        SiteURL     = $spWeb.URL
                        Group       = "Administrator"        
                        Role        = "Site Collection Administrator"
                        User        = $spSiteAdmin.name              
                        UserName    = $spSiteAdmin.UserLogin.Replace("i:0#.w|","")
                        Inherited   = $spWeb.Permissions.Inherited
                    }                           
                    New-Object PSObject -Property $details  | export-csv -Path $Output -NoTypeInformation -Append  
                }
            } 
        }
        foreach ($spGroup in $spWeb.sitegroups)
        {
            foreach ($spUser in $spGroup.users)
            {
                if ($spGroup.Roles.Count -eq 1)
                {
                    $details = @{
                        SiteName    = $spWeb.Title
                        SiteURL     = $spWeb.URL
                        Group       = $spGroup.name      
                        Role        = $spGroup.Roles.Name      
                        User        = $spUser.name              
                        UserName    = $spUser.UserLogin.Replace("i:0#.w|","")
                        Inherited   = $spWeb.Permissions.Inherited
                    }                           
                    New-Object PSObject -Property $details  | export-csv -Path $Output -NoTypeInformation -Append  
                }
                elseif ($spGroup.Roles.Count -gt 1)
                {
                    foreach ($spRole in $spGroup.Roles)
                    {
                        if ($spRole.Name -ne "Limited Access"){
                            $details = @{
                                SiteName    = $spWeb.Title
                                SiteURL     = $spWeb.URL
                                Group       = $spGroup.name      
                                Role        = $spRole      
                                User        = $spUser.name              
                                UserName    = $spUser.UserLogin.Replace("i:0#.w|","")
                                Inherited   = $spWeb.Permissions.Inherited
                            }                           
                            New-Object PSObject -Property $details  | export-csv -Path $Output -NoTypeInformation -Append
                        }  
                    }
                }
            }
        }
        foreach ($spRole in $spWeb.Roles)
        {
            if ($spRole.Users -ne $null -and $spRole.Type -ne "Administrator")
            {
                foreach ($spUser in $spRole.Users)
                {
                    $details = @{
                        SiteName    = $spWeb.Title
                        SiteURL     = $spWeb.URL
                        Group       = "Given Explicitly"
                        Role        = $spRole.Type      
                        User        = $spUser.DisplayName             
                        UserName    = $spUser.UserLogin
                        Inherited   = $spWeb.Permissions.Inherited
                    }                           
                    New-Object PSObject -Property $details  | export-csv -Path $Output -NoTypeInformation -Append 
                }
            }
        }
        Write-Host -f Green " .....Done!"
    }

    $spSite.dispose()
}

Export-ModuleMember -Function *