functions/SPClientObject/Debug-SPClientObject.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
#Requires -Version 3.0

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


function Debug-SPClientObject {

<#
.SYNOPSIS
  Dumps a client object.
.DESCRIPTION
  The Debug-SPClientObject function converts a client object to hashtable that contains loaded properties.
.PARAMETER InputObject
  Indicates the client object.
.EXAMPLE
  Debug-SPClientObject $object
.INPUTS
  None or Microsoft.SharePoint.Client.ClientObject
.OUTPUTS
  System.Collections.Generic.Dictionary`2[System.String,System.Object]
.LINK
  https://github.com/karamem0/SPClient/blob/master/doc/Debug-SPClientObject.md
#>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [Microsoft.SharePoint.Client.ClientObject]
        $InputObject
    )

    process {
        if ($InputObject -is [System.Collections.IEnumerable]) {
            if ($InputObject.AreItemsAvailable -ne $false) {
                $List = New-Object 'System.Collections.Generic.List`1[object]'
                $InputObjectCollection = $InputObject.GetEnumerator()
                foreach ($InputObject in $InputObjectCollection) {
                    $Value = Debug-SPClientObject -InputObject $InputObject
                    $List.Add($Value)
                }
                Write-Output $List
            }
        } else {
            $Dict = New-Object 'System.Collections.Generic.Dictionary`2[string,object]'
            foreach ($Property in $InputObject.GetType().GetProperties()) {
                $Type = $Property.PropertyType
                $Name = $Property.Name
                if ($Type.IsSubclassOf([Microsoft.SharePoint.Client.ClientObject])) {
                    if ($InputObject.IsObjectPropertyInstantiated($Name)) {
                        $Value = $Property.GetValue($InputObject)
                        $Value = Debug-SPClientObject -InputObject $Value
                        $Dict.Add($Name, $Value)
                    }
                } else {
                    if ($InputObject.IsPropertyAvailable($Name)) {
                        $Value = $Property.GetValue($InputObject)
                        $Dict.Add($Name, $Value)
                    }
                }
            }
            if ($InputObject -is [Microsoft.SharePoint.Client.ListItem]) {
                $Dict.Add('FieldValues', $InputObject.FieldValues)
            }
            Write-Output $Dict
        }
    }

}