functions/SPClientType/Use-SPClientType.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
#Requires -Version 3.0

<#
  Use-SPClientType.ps1
 
  Copyright (c) 2017 karamem0
 
  This software is released under the MIT License.
  https://github.com/karamem0/SPClient/blob/master/LICENSE
#>


function Use-SPClientType {

<#
.SYNOPSIS
  Loads SharePoint client components assemblies.
.DESCRIPTION
  The Use-SPClientType function loads SharePoint client components assemblies.
    - Microsoft.SharePoint.Client.dll
    - Microsoft.SharePoint.Client.Runtime.dll
  Usually, this function is called first in your script.
.PARAMETER LiteralPath
  Indicates the path that locates SharePoint client components. If not specified, loads from the location below.
    - Current working directory
    - Global assembly cache (GAC)
.PARAMETER PassThru
  If specified, returns loaded assemblies.
.EXAMPLE
  Use-SPClientType
.EXAMPLE
  Use-SPClientType -LiteralPath "C:\Users\admin\Documents"
.INPUTS
  None or System.String
.OUTPUTS
  None or System.Reflection.Assembly[]
.LINK
  https://github.com/karamem0/SPClient/blob/master/doc/Use-SPClientType.md
#>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true)]
        [string]
        $LiteralPath,
        [Parameter(Mandatory = $false)]
        [switch]
        $PassThru
    )

    process {
        $AssemblyNames = @(
            'Microsoft.SharePoint.Client'
            'Microsoft.SharePoint.Client.Runtime'
        )
        $AssemblyPaths = @()
        if ($PSBoundParameters.ContainsKey('LiteralPath')) {
            if (Test-Path -Path $LiteralPath) {
                foreach ($AssemblyName in $AssemblyNames) {
                    $AssemblyPath = Join-Path -Path $LiteralPath -ChildPath "$($AssemblyName).dll"
                    if (Test-Path -Path $AssemblyPath) {
                        $AssemblyPaths += $AssemblyPath
                    }
                }
            }
        }
        if ($AssemblyNames.Length -ne $AssemblyPaths.Length) {
            $AssemblyPaths.Clear()
            $LiteralPath = Get-Location
            foreach ($AssemblyName in $AssemblyNames) {
                $AssemblyPath = Join-Path -Path $LiteralPath -ChildPath "$($AssemblyName).dll"
                if (Test-Path -Path $AssemblyPath) {
                    $AssemblyPaths += $AssemblyPath
                }
            }
        }
        if ($AssemblyNames.Length -ne $AssemblyPaths.Length) {
            $AssemblyPaths.Clear()
            $LiteralPath = "$($Env:WinDir)\Microsoft.NET\Assembly\GAC_MSIL"
            foreach ($AssemblyName in $AssemblyNames) {
                $AssemblyPath = Join-Path -Path $LiteralPath -ChildPath "$($AssemblyName)"
                $AssemblyPath = Get-ChildItem -Path $AssemblyPath -Recurse |
                    Where-Object { $_.Extension -eq '.dll' } |
                    ForEach-Object { $_.FullName } |
                    Sort-Object -Descending |
                    Select-Object -First 1
                $AssemblyPaths += $AssemblyPath
            }
        }
        if ($AssemblyNames.Length -ne $AssemblyPaths.Length) {
            throw 'Cannot find SharePoint Client Component assemblies.'
        }
        $Assemblies = $AssemblyPaths | ForEach-Object { [System.Reflection.Assembly]::LoadFrom($_) }
        Get-ChildItem -Path $SPClient.ModulePath -Recurse |
            Where-Object { -not $_.FullName.Contains('.Tests.') } |
            Where-Object { $_.Extension -eq '.cs' } |
            ForEach-Object { Add-Type -Path $_.FullName -ReferencedAssemblies $Assemblies }
        if ($PassThru) {
            Write-Output $Assemblies
        }
    }

}