-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathActivityFunctionApp.ps1
84 lines (62 loc) · 2.83 KB
/
ActivityFunctionApp.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
#Enumerators and object to wrap the incoming request
$pageArray = @()
$rawreq = @()
$rawreq = New-Object -TypeName psobject
$rawreq | Add-Member -name Content -value Content -membertype noteproperty
#Retrieve the content URI
$requestbody = Get-Content $req -Raw | ConvertFrom-Json
$rawreq.content = $requestbody | convertto-json
#Activity Feed webhook Body to process
$contenttype = $requestBody.contenttype
#$tenantguid = $requestBody.tenantid
$clientIdIn = $requestBody.clientid
$contentId = $requestBody.contentid
$contentUri = $requestBody.contentUri
$contentCreated = $requestBody.contentCreated
$contentExpiration = $requestBody.contentExpiration
#Sign in Parameters
$ClientID = "YOUR CLIENT ID A HEX”
$ClientSecret = "YOUR CLIENT SECRET
$loginURL = "https://login.windows.net"
$tenantdomain = "YOURDOMAIN.onmicrosoft.com"
$TenantGUID = "YOUR TenantGUID HEX"
$resource = "https://manage.office.com"
#Verify that it is the correct ID and import to Cosmos DB
if ($clientIdIn -eq $ClientId )
{
# Get an Oauth 2 access token based on client id, secret and tenant domain
$body = @{grant_type="client_credentials";resource=$resource;client_id=$ClientID;client_secret=$ClientSecret}
#oauthtoken in the header
$oauth = Invoke-RestMethod -Method Post -Uri $loginURL/$tenantdomain/oauth2/token?api-version=1.0 -Body $body
$headerParams = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"}
#If more than one page is returned capture and return in pageArray
if ($REQ_HEADERS_NextPageUri) {
$pageTracker = $true
$pagedReq = $REQ_HEADERS_NextPageUri
while ($pageTracker -ne $false)
{
$CurrentPage = Invoke-WebRequest -Headers $headerParams -Uri $pagedReq -UseBasicParsing
$pageArray += $CurrentPage
if ($CurrentPage.Headers.NextPageUri)
{
$pageTracker = $true
}
Else
{
$pageTracker = $false
}
$pagedReq = $CurrentPage.Headers.NextPageUri
}
}
$pageArray += $rawreq
foreach ($page in $pageArray)
{
$request = $page.content | ConvertFrom-Json
foreach ( $content in $request)
{
$uri = $content.contentUri + "?PublisherIdentifier=" + $TenantGUID
Invoke-WebRequest -UseBasicParsing -Headers $headerParams -Uri $uri -PassThru -OutFile $outputdocument
}
}
}
Out-File -Encoding Ascii -FilePath $res -inputObject "200 OK"