3
3
See LICENSE in the project root for license information.
4
4
#>
5
5
6
+ $ErrorActionPreference = " Stop" # http://technet.microsoft.com/en-us/library/dd347731.aspx
7
+ Set-StrictMode - Version " Latest" # http://technet.microsoft.com/en-us/library/dd347614.aspx
8
+
6
9
# PS helper methods to call ReST API methods targeting Project Online tenants
7
- $global :fedAuthTicket = ' '
10
+ $global :accessHeader = ' '
8
11
$global :digestValue = ' '
9
12
13
+ [Reflection.Assembly ]::LoadFrom(" $ ( $PSScriptRoot ) \Microsoft.IdentityModel.Clients.ActiveDirectory.dll" ) | Out-Null
14
+
15
+ function Get-AADAuthToken ([Uri ] $Uri )
16
+ {
17
+ # NOTE: Create an azure app and update $clientId and $redirectUri below
18
+ $clientId = " "
19
+ $redirectUri = " https://login.microsoftonline.com/common/oauth2/nativeclient"
20
+
21
+ $authority = " https://login.microsoftonline.com/common"
22
+ $resource = $Uri.GetLeftPart ([System.UriPartial ]::Authority);
23
+
24
+ $promptBehavior = [Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior ]::Always
25
+ $platformParam = New-Object " Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters" - ArgumentList $promptBehavior
26
+ $authenticationContext = New-Object " Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" - ArgumentList $authority , $False
27
+ $authenticationResult = $authenticationContext.AcquireTokenAsync ($resource , $clientId , $redirectUri , $platformParam ).Result
28
+
29
+ return $authenticationResult
30
+ }
31
+
10
32
function Set-SPOAuthenticationTicket ([string ] $siteUrl )
11
33
{
12
-
13
- Write-Host ' Enter password for user' $username ' on site' $siteUrl
14
- $password = Read-Host - AsSecureString
34
+ $siteUri = New-Object Uri - ArgumentList $siteUrl
35
+
36
+ $authResult = Get-AADAuthToken - Uri $siteUri
37
+ if ($authResult -ne $null )
38
+ {
39
+ $global :accessHeader = $authResult.AccessTokenType + " " + $authResult.AccessToken
40
+ }
15
41
16
- # load the SP client runtime code
17
- [System.Reflection.Assembly ]::LoadWithPartialName(" Microsoft.SharePoint.Client.Runtime" )
18
- $onlineCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username , $password )
19
- if ($onlineCredentials -ne $null )
20
- {
21
- $global :fedAuthTicket = $onlineCredentials.GetAuthenticationCookie ($SiteUrl , $true ).TrimStart(' SPOIDCRL=' )
22
- }
23
-
24
- if ([String ]::IsNullOrEmpty($global :fedAuthTicket ))
25
- {
26
- throw ' Could not obtain authentication ticket based on provided credentials for specified site'
27
- }
42
+ if ([String ]::IsNullOrEmpty($global :accessHeader ))
43
+ {
44
+ throw ' Could not obtain authentication ticket based on provided credentials for specified site'
45
+ }
28
46
}
29
47
30
48
function Build-ReSTRequest ([string ] $siteUrl , [string ]$endpoint , [string ]$method , [string ]$body = $null )
31
49
{
32
- $url = ([string ]$siteUrl ).TrimEnd(" /" ) + " /_api/" + $endpoint
33
- $req = [System.Net.WebRequest ]::Create($url )
34
- $req.Method = $method
35
-
36
- [bool ]$isReadOnly = ((' GET' , ' HEAD' ) -contains $req.Method )
37
- [bool ]$isDigestRequest = $endpoint -contains ' contextinfo'
38
-
39
- if ([String ]::IsNullOrEmpty($body ))
40
- {
41
- $req.ContentLength = 0 ;
42
- }
43
- else
44
- {
45
- $req.ContentLength = $body.Length
46
- $req.ContentType = " application/json"
47
- }
48
-
49
- $domain = (New-Object System.Uri($url )).Authority
50
- $cookies = New-Object System.Net.CookieContainer
51
- $fedCookie = New-Object System.Net.Cookie ' SPOIDCRL' , $global :fedAuthTicket , " " , $domain
52
- $cookies.Add ($fedCookie )
50
+ $url = ([string ]$siteUrl ).TrimEnd(" /" ) + " /_api/" + $endpoint
51
+ $req = [System.Net.WebRequest ]::Create($url )
52
+ $req.Timeout = 120000
53
+ $req.Method = $method
53
54
54
- $req.CookieContainer = $cookies
55
+ [bool ]$isReadOnly = ((' GET' , ' HEAD' ) -contains $req.Method )
56
+ [bool ]$isDigestRequest = $endpoint -contains ' contextinfo'
57
+
58
+ if ([String ]::IsNullOrEmpty($body ))
59
+ {
60
+ $req.ContentLength = 0 ;
61
+ }
62
+ else
63
+ {
64
+ $req.ContentLength = $body.Length
65
+ $req.ContentType = " application/json"
66
+ }
67
+
68
+ # set Authorization header
69
+ $req.Headers.Add (" Authorization" , $global :accessHeader )
55
70
56
71
if (-not $isDigestRequest )
57
72
{
@@ -60,57 +75,57 @@ function Build-ReSTRequest([string] $siteUrl, [string]$endpoint, [string]$method
60
75
$req.Headers.Add (" X-RequestDigest" , $global :digestValue )
61
76
}
62
77
}
63
-
64
- if (-not [String ]::IsNullOrEmpty($body ))
65
- {
66
- $writer = New-Object System.IO.StreamWriter $req.GetRequestStream ()
67
- $writer.Write ($body )
68
- $writer.Close ()
78
+
79
+ if (-not [String ]::IsNullOrEmpty($body ))
80
+ {
81
+ $writer = New-Object System.IO.StreamWriter $req.GetRequestStream ()
82
+ $writer.Write ($body )
83
+ $writer.Close ()
69
84
$writer.Dispose ()
70
- }
71
-
72
- return $req
85
+ }
86
+
87
+ return $req
73
88
}
74
89
75
90
function Set-DigestValue ([string ]$siteUrl )
76
91
{
77
- $request = Build-ReSTRequest $siteUrl ' contextinfo' ' POST' $null
78
- if ($request -eq $null )
79
- {
80
- throw ' Could not obtain a request digest value based on provided credentials for specified site'
81
- }
82
-
83
- try
84
- {
85
- $resp = $request.GetResponse ()
86
- $reader = [System.Xml.XmlReader ]::Create($resp.GetResponseStream ())
87
- if ($reader.ReadToDescendant (" d:FormDigestValue" ))
88
- {
89
- $global :digestValue = $reader.ReadElementContentAsString ()
90
- }
91
- else
92
- {
93
- throw ' Could not obtain a request digest value based on provided credentials for specified site'
94
- }
95
- }
96
- finally
97
- {
98
- if ($reader -ne $null )
99
- {
100
- $reader.Close ()
101
- $reader.Dispose ()
102
- }
103
- if ($resp -ne $null )
104
- {
105
- $resp.Close ()
106
- $resp.Dispose ()
107
- }
108
- }
92
+ $request = Build-ReSTRequest $siteUrl ' contextinfo' ' POST' $null
93
+ if ($request -eq $null )
94
+ {
95
+ throw ' Could not obtain a request digest value based on provided credentials for specified site'
96
+ }
97
+
98
+ try
99
+ {
100
+ $resp = $request.GetResponse ()
101
+ $reader = [System.Xml.XmlReader ]::Create($resp.GetResponseStream ())
102
+ if ($reader.ReadToDescendant (" d:FormDigestValue" ))
103
+ {
104
+ $global :digestValue = $reader.ReadElementContentAsString ()
105
+ }
106
+ else
107
+ {
108
+ throw ' Could not obtain a request digest value based on provided credentials for specified site'
109
+ }
110
+ }
111
+ finally
112
+ {
113
+ if ($reader -ne $null )
114
+ {
115
+ $reader.Close ()
116
+ $reader.Dispose ()
117
+ }
118
+ if ($resp -ne $null )
119
+ {
120
+ $resp.Close ()
121
+ $resp.Dispose ()
122
+ }
123
+ }
109
124
}
110
125
111
126
function Post-ReSTRequest ([string ]$siteUrl , [string ]$endpoint , [string ]$body = $null )
112
127
{
113
- $request = Build-ReSTRequest $siteUrl $endpoint ' POST' $body
128
+ $request = Build-ReSTRequest $siteUrl $endpoint ' POST' $body
114
129
$resp = $request.GetResponse ()
115
130
if ($resp -ne $null )
116
131
{
@@ -122,7 +137,7 @@ function Post-ReSTRequest([string]$siteUrl, [string]$endpoint, [string]$body = $
122
137
123
138
function Patch-ReSTRequest ([string ]$siteUrl , [string ]$endpoint , [string ]$body )
124
139
{
125
- $request = Build-ReSTRequest $siteUrl $endpoint ' PATCH' $body
140
+ $request = Build-ReSTRequest $siteUrl $endpoint ' PATCH' $body
126
141
$resp = $request.GetResponse ()
127
142
if ($resp -ne $null )
128
143
{
@@ -134,12 +149,12 @@ function Patch-ReSTRequest([string]$siteUrl, [string]$endpoint, [string]$body)
134
149
135
150
function Get-ReSTRequest ([string ]$siteUrl , [string ]$endpoint )
136
151
{
137
- $request = Build-ReSTRequest $siteUrl $endpoint ' GET'
152
+ $request = Build-ReSTRequest $siteUrl $endpoint ' GET'
138
153
$resp = $request.GetResponse ()
139
154
if ($resp -ne $null )
140
155
{
141
156
$reader = New-Object System.IO.StreamReader $resp.GetResponseStream ()
142
157
$reader.ReadToEnd ()
143
- $reader.Dispose ()
158
+ $reader.Dispose ()
144
159
}
145
160
}
0 commit comments