@@ -14,58 +14,58 @@ $global:digestValue = ''
14
14
15
15
function Get-AADAuthToken ([Uri ] $Uri )
16
16
{
17
- # NOTE: Create an azure app and update $clientId and $redirectUri below
18
- $clientId = " "
19
- $redirectUri = " https://login.microsoftonline.com/common/oauth2/nativeclient"
17
+ # NOTE: Create an azure app and update $clientId and $redirectUri below
18
+ $clientId = " "
19
+ $redirectUri = " https://login.microsoftonline.com/common/oauth2/nativeclient"
20
20
21
- $authority = " https://login.microsoftonline.com/common"
22
- $resource = $Uri.GetLeftPart ([System.UriPartial ]::Authority);
21
+ $authority = " https://login.microsoftonline.com/common"
22
+ $resource = $Uri.GetLeftPart ([System.UriPartial ]::Authority);
23
23
24
- $promptBehavior = [Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior ]::Always
25
- $platformParam = New-Object " Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters" - ArgumentList $promptBehavior
24
+ $promptBehavior = [Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior ]::Always
25
+ $platformParam = New-Object " Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters" - ArgumentList $promptBehavior
26
26
$authenticationContext = New-Object " Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" - ArgumentList $authority , $False
27
27
$authenticationResult = $authenticationContext.AcquireTokenAsync ($resource , $clientId , $redirectUri , $platformParam ).Result
28
28
29
- return $authenticationResult
29
+ return $authenticationResult
30
30
}
31
31
32
32
function Set-SPOAuthenticationTicket ([string ] $siteUrl )
33
33
{
34
- $siteUri = New-Object Uri - ArgumentList $siteUrl
34
+ $siteUri = New-Object Uri - ArgumentList $siteUrl
35
35
36
- $authResult = Get-AADAuthToken - Uri $siteUri
37
- if ($authResult -ne $null )
38
- {
39
- $global :accessHeader = $authResult.AccessTokenType + " " + $authResult.AccessToken
40
- }
41
-
42
- if ([String ]::IsNullOrEmpty($global :accessHeader ))
43
- {
44
- throw ' Could not obtain authentication ticket based on provided credentials for specified site'
45
- }
36
+ $authResult = Get-AADAuthToken - Uri $siteUri
37
+ if ($authResult -ne $null )
38
+ {
39
+ $global :accessHeader = $authResult.AccessTokenType + " " + $authResult.AccessToken
40
+ }
41
+
42
+ if ([String ]::IsNullOrEmpty($global :accessHeader ))
43
+ {
44
+ throw ' Could not obtain authentication ticket based on provided credentials for specified site'
45
+ }
46
46
}
47
47
48
48
function Build-ReSTRequest ([string ] $siteUrl , [string ]$endpoint , [string ]$method , [string ]$body = $null )
49
49
{
50
- $url = ([string ]$siteUrl ).TrimEnd(" /" ) + " /_api/" + $endpoint
51
- $req = [System.Net.WebRequest ]::Create($url )
52
- $req.Method = $method
53
-
54
- [bool ]$isReadOnly = ((' GET' , ' HEAD' ) -contains $req.Method )
55
- [bool ]$isDigestRequest = $endpoint -contains ' contextinfo'
56
-
57
- if ([String ]::IsNullOrEmpty($body ))
58
- {
59
- $req.ContentLength = 0 ;
60
- }
61
- else
62
- {
63
- $req.ContentLength = $body.Length
64
- $req.ContentType = " application/json"
65
- }
50
+ $url = ([string ]$siteUrl ).TrimEnd(" /" ) + " /_api/" + $endpoint
51
+ $req = [System.Net.WebRequest ]::Create($url )
52
+ $req.Method = $method
53
+
54
+ [bool ]$isReadOnly = ((' GET' , ' HEAD' ) -contains $req.Method )
55
+ [bool ]$isDigestRequest = $endpoint -contains ' contextinfo'
56
+
57
+ if ([String ]::IsNullOrEmpty($body ))
58
+ {
59
+ $req.ContentLength = 0 ;
60
+ }
61
+ else
62
+ {
63
+ $req.ContentLength = $body.Length
64
+ $req.ContentType = " application/json"
65
+ }
66
66
67
- # set Authorization header
68
- $req.Headers.Add (" Authorization" , $global :accessHeader )
67
+ # set Authorization header
68
+ $req.Headers.Add (" Authorization" , $global :accessHeader )
69
69
70
70
if (-not $isDigestRequest )
71
71
{
@@ -74,57 +74,57 @@ function Build-ReSTRequest([string] $siteUrl, [string]$endpoint, [string]$method
74
74
$req.Headers.Add (" X-RequestDigest" , $global :digestValue )
75
75
}
76
76
}
77
-
78
- if (-not [String ]::IsNullOrEmpty($body ))
79
- {
80
- $writer = New-Object System.IO.StreamWriter $req.GetRequestStream ()
81
- $writer.Write ($body )
82
- $writer.Close ()
77
+
78
+ if (-not [String ]::IsNullOrEmpty($body ))
79
+ {
80
+ $writer = New-Object System.IO.StreamWriter $req.GetRequestStream ()
81
+ $writer.Write ($body )
82
+ $writer.Close ()
83
83
$writer.Dispose ()
84
- }
85
-
86
- return $req
84
+ }
85
+
86
+ return $req
87
87
}
88
88
89
89
function Set-DigestValue ([string ]$siteUrl )
90
90
{
91
- $request = Build-ReSTRequest $siteUrl ' contextinfo' ' POST' $null
92
- if ($request -eq $null )
93
- {
94
- throw ' Could not obtain a request digest value based on provided credentials for specified site'
95
- }
96
-
97
- try
98
- {
99
- $resp = $request.GetResponse ()
100
- $reader = [System.Xml.XmlReader ]::Create($resp.GetResponseStream ())
101
- if ($reader.ReadToDescendant (" d:FormDigestValue" ))
102
- {
103
- $global :digestValue = $reader.ReadElementContentAsString ()
104
- }
105
- else
106
- {
107
- throw ' Could not obtain a request digest value based on provided credentials for specified site'
108
- }
109
- }
110
- finally
111
- {
112
- if ($reader -ne $null )
113
- {
114
- $reader.Close ()
115
- $reader.Dispose ()
116
- }
117
- if ($resp -ne $null )
118
- {
119
- $resp.Close ()
120
- $resp.Dispose ()
121
- }
122
- }
91
+ $request = Build-ReSTRequest $siteUrl ' contextinfo' ' POST' $null
92
+ if ($request -eq $null )
93
+ {
94
+ throw ' Could not obtain a request digest value based on provided credentials for specified site'
95
+ }
96
+
97
+ try
98
+ {
99
+ $resp = $request.GetResponse ()
100
+ $reader = [System.Xml.XmlReader ]::Create($resp.GetResponseStream ())
101
+ if ($reader.ReadToDescendant (" d:FormDigestValue" ))
102
+ {
103
+ $global :digestValue = $reader.ReadElementContentAsString ()
104
+ }
105
+ else
106
+ {
107
+ throw ' Could not obtain a request digest value based on provided credentials for specified site'
108
+ }
109
+ }
110
+ finally
111
+ {
112
+ if ($reader -ne $null )
113
+ {
114
+ $reader.Close ()
115
+ $reader.Dispose ()
116
+ }
117
+ if ($resp -ne $null )
118
+ {
119
+ $resp.Close ()
120
+ $resp.Dispose ()
121
+ }
122
+ }
123
123
}
124
124
125
125
function Post-ReSTRequest ([string ]$siteUrl , [string ]$endpoint , [string ]$body = $null )
126
126
{
127
- $request = Build-ReSTRequest $siteUrl $endpoint ' POST' $body
127
+ $request = Build-ReSTRequest $siteUrl $endpoint ' POST' $body
128
128
$resp = $request.GetResponse ()
129
129
if ($resp -ne $null )
130
130
{
@@ -136,7 +136,7 @@ function Post-ReSTRequest([string]$siteUrl, [string]$endpoint, [string]$body = $
136
136
137
137
function Patch-ReSTRequest ([string ]$siteUrl , [string ]$endpoint , [string ]$body )
138
138
{
139
- $request = Build-ReSTRequest $siteUrl $endpoint ' PATCH' $body
139
+ $request = Build-ReSTRequest $siteUrl $endpoint ' PATCH' $body
140
140
$resp = $request.GetResponse ()
141
141
if ($resp -ne $null )
142
142
{
@@ -148,12 +148,12 @@ function Patch-ReSTRequest([string]$siteUrl, [string]$endpoint, [string]$body)
148
148
149
149
function Get-ReSTRequest ([string ]$siteUrl , [string ]$endpoint )
150
150
{
151
- $request = Build-ReSTRequest $siteUrl $endpoint ' GET'
151
+ $request = Build-ReSTRequest $siteUrl $endpoint ' GET'
152
152
$resp = $request.GetResponse ()
153
153
if ($resp -ne $null )
154
154
{
155
155
$reader = New-Object System.IO.StreamReader $resp.GetResponseStream ()
156
156
$reader.ReadToEnd ()
157
- $reader.Dispose ()
157
+ $reader.Dispose ()
158
158
}
159
159
}
0 commit comments