Skip to content

Commit 295e04f

Browse files
authored
Merge pull request #319 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents a5bdb7a + 000ac0e commit 295e04f

12 files changed

+114
-40
lines changed

Modules/CIPPCore/Public/Alerts/Get-CIPPAlertSharepointQuota.ps1

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
function Get-CIPPAlertSharepointQuota {
32
<#
43
.FUNCTIONALITY
@@ -12,10 +11,8 @@ function Get-CIPPAlertSharepointQuota {
1211
$TenantFilter
1312
)
1413
Try {
15-
$tenantName = (New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/sites/root' -asApp $true -tenantid $TenantFilter).id.Split('.')[0]
16-
$sharepointToken = (Get-GraphToken -scope "https://$($tenantName)-admin.sharepoint.com/.default" -tenantid $TenantFilter)
17-
$sharepointToken.Add('accept', 'application/json')
18-
$sharepointQuota = (Invoke-RestMethod -Method 'GET' -Headers $sharepointToken -Uri "https://$($tenantName)-admin.sharepoint.com/_api/StorageQuotas()?api-version=1.3.2" -ErrorAction Stop).value
14+
$SharePointInfo = Get-SharePointAdminLink -Public $false
15+
$sharepointQuota = (New-GraphGetRequest -scope "$($SharePointInfo.AdminUrl)/.default" -tenantid $TenantFilter -uri "$($SharePointInfo.AdminUrl)/_api/StorageQuotas()?api-version=1.3.2").value
1916
} catch {
2017
return
2118
}
@@ -31,4 +28,4 @@ function Get-CIPPAlertSharepointQuota {
3128
Write-AlertTrace -cmdletName $MyInvocation.MyCommand -tenantFilter $TenantFilter -data $AlertData
3229
}
3330
}
34-
}
31+
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Teams-Sharepoint/Invoke-ListSharepointAdminUrl.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ function Invoke-ListSharepointAdminUrl {
1919
if ($Tenant.SharepointAdminUrl) {
2020
$AdminUrl = $Tenant.SharepointAdminUrl
2121
} else {
22-
$tenantName = (New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/sites/root' -asApp $true -tenantid $TenantFilter).id.Split('.')[0]
23-
$AdminUrl = "https://$($tenantName)-admin.sharepoint.com"
24-
$Tenant | Add-Member -MemberType NoteProperty -Name SharepointAdminUrl -Value $AdminUrl
22+
$SharePointInfo = Get-SharePointAdminLink -Public $false
23+
$Tenant | Add-Member -MemberType NoteProperty -Name SharepointAdminUrl -Value $SharePointInfo.AdminUrl
2524
$Table = Get-CIPPTable -TableName 'Tenants'
2625
Add-CIPPAzDataTableEntity @Table -Entity $Tenant -Force
2726
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Teams-Sharepoint/Invoke-ListSharepointQuota.ps1

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ Function Invoke-ListSharepointQuota {
2121
$UsedStoragePercentage = 'Not Supported'
2222
} else {
2323
try {
24-
$TenantName = (New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/sites/root' -asApp $true -tenantid $TenantFilter).id.Split('.')[0]
25-
26-
$SharePointToken = (Get-GraphToken -scope "https://$($TenantName)-admin.sharepoint.com/.default" -tenantid $TenantFilter)
27-
$SharePointToken.Add('accept', 'application/json')
28-
# Implement a try catch later to deal with SharePoint guest user settings
29-
$SharePointQuota = (Invoke-RestMethod -Method 'GET' -Headers $SharePointToken -Uri "https://$($TenantName)-admin.sharepoint.com/_api/StorageQuotas()?api-version=1.3.2" -ErrorAction Stop).value | Sort-Object -Property GeoUsedStorageMB -Descending | Select-Object -First 1
24+
$SharePointInfo = Get-SharePointAdminLink -Public $false
25+
$SharePointQuota = (New-GraphGetRequest -scope "$($SharePointInfo.AdminUrl)/.default" -tenantid $TenantFilter -uri "$($SharePointInfo.AdminUrl)/_api/StorageQuotas()?api-version=1.3.2").value | Sort-Object -Property GeoUsedStorageMB -Descending | Select-Object -First 1
3026

3127
if ($SharePointQuota) {
3228
$UsedStoragePercentage = [int](($SharePointQuota.GeoUsedStorageMB / $SharePointQuota.TenantStorageMB) * 100)

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Standards/Invoke-ListStandardsCompare.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ function Invoke-ListStandardsCompare {
5050
$FieldName = $Standard.RowKey
5151
$FieldValue = $Standard.Value
5252
$Tenant = $Standard.PartitionKey
53+
54+
# decode field names that are hex encoded (e.g. QuarantineTemplates)
55+
if ($FieldName -match '^(standards\.QuarantineTemplate\.)(.+)$') {
56+
$Prefix = $Matches[1]
57+
$HexEncodedName = $Matches[2]
58+
$Chars = [System.Collections.Generic.List[char]]::new()
59+
for ($i = 0; $i -lt $HexEncodedName.Length; $i += 2) {
60+
$Chars.Add([char][Convert]::ToInt32($HexEncodedName.Substring($i,2),16))
61+
}
62+
$FieldName = "$Prefix$(-join $Chars)"
63+
}
64+
5365
if ($FieldValue -is [System.Boolean]) {
5466
$FieldValue = [bool]$FieldValue
5567
} elseif ($FieldValue -like '*{*') {

Modules/CIPPCore/Public/Get-CIPPSPOTenant.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ function Get-CIPPSPOTenant {
88

99
if (!$SharepointPrefix) {
1010
# get sharepoint admin site
11-
$tenantName = (New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/sites/root' -asApp $true -tenantid $TenantFilter).id.Split('.')[0]
11+
$SharePointInfo = Get-SharePointAdminLink -Public $false
12+
$tenantName = $SharePointInfo.TenantName
13+
$AdminUrl = $SharePointInfo.AdminUrl
1214
} else {
1315
$tenantName = $SharepointPrefix
16+
$AdminUrl = "https://$($tenantName)-admin.sharepoint.com"
1417
}
15-
$AdminUrl = "https://$($tenantName)-admin.sharepoint.com"
1618

1719
# Query tenant settings
1820
$XML = @'
@@ -21,7 +23,7 @@ function Get-CIPPSPOTenant {
2123
$AdditionalHeaders = @{
2224
'Accept' = 'application/json;odata=verbose'
2325
}
24-
$Results = New-GraphPostRequest -scope "$AdminURL/.default" -tenantid $TenantFilter -Uri "$AdminURL/_vti_bin/client.svc/ProcessQuery" -Type POST -Body $XML -ContentType 'text/xml' -AddedHeaders $AdditionalHeaders
26+
$Results = New-GraphPostRequest -scope "$($AdminUrl)/.default" -tenantid $TenantFilter -Uri "$($SharePointInfo.AdminUrl)/_vti_bin/client.svc/ProcessQuery" -Type POST -Body $XML -ContentType 'text/xml' -AddedHeaders $AdditionalHeaders
2527

2628
$Results | Select-Object -Last 1 *, @{n = 'SharepointPrefix'; e = { $tenantName } }, @{n = 'TenantFilter'; e = { $TenantFilter } }
2729
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
function Get-SharePointAdminLink {
2+
<#
3+
.FUNCTIONALITY
4+
Internal
5+
#>
6+
[CmdletBinding()]
7+
param ($Public)
8+
9+
if ($Public) {
10+
# Do it through domain discovery, unreliable
11+
try {
12+
# Get tenant information using autodiscover
13+
$body = @"
14+
<?xml version="1.0" encoding="utf-8"?>
15+
<soap:Envelope xmlns:exm="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:ext="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
16+
<soap:Header>
17+
<a:Action soap:mustUnderstand="1">http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetFederationInformation</a:Action>
18+
<a:To soap:mustUnderstand="1">https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc</a:To>
19+
<a:ReplyTo>
20+
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
21+
</a:ReplyTo>
22+
</soap:Header>
23+
<soap:Body>
24+
<GetFederationInformationRequestMessage xmlns="http://schemas.microsoft.com/exchange/2010/Autodiscover">
25+
<Request>
26+
<Domain>$TenantFilter</Domain>
27+
</Request>
28+
</GetFederationInformationRequestMessage>
29+
</soap:Body>
30+
</soap:Envelope>
31+
"@
32+
33+
# Create the headers
34+
$AutoDiscoverHeaders = @{
35+
'Content-Type' = 'text/xml; charset=utf-8'
36+
'SOAPAction' = '"http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetFederationInformation"'
37+
'User-Agent' = 'AutodiscoverClient'
38+
}
39+
40+
# Invoke autodiscover
41+
$Response = Invoke-RestMethod -UseBasicParsing -Method Post -Uri 'https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc' -Body $body -Headers $AutoDiscoverHeaders
42+
43+
# Get the onmicrosoft.com domain from the response
44+
$TenantDomains = $Response.Envelope.body.GetFederationInformationResponseMessage.response.Domains.Domain | Sort-Object
45+
$OnMicrosoftDomains = $TenantDomains | Where-Object { $_ -like "*.onmicrosoft.com" }
46+
47+
if ($OnMicrosoftDomains.Count -eq 0) {
48+
throw "Could not find onmicrosoft.com domain through autodiscover"
49+
} elseif ($OnMicrosoftDomains.Count -gt 1) {
50+
throw "Multiple onmicrosoft.com domains found through autodiscover. Cannot determine the correct one: $($OnMicrosoftDomains -join ', ')"
51+
} else {
52+
$OnMicrosoftDomain = $OnMicrosoftDomains[0]
53+
$tenantName = $OnMicrosoftDomain.Split('.')[0]
54+
}
55+
} catch {
56+
throw "Failed to get SharePoint admin URL through autodiscover: $($_.Exception.Message)"
57+
}
58+
} else {
59+
$tenantName = (New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/sites/root' -asApp $true -tenantid $TenantFilter).id.Split('.')[0]
60+
}
61+
62+
# Return object with all needed properties
63+
return [PSCustomObject]@{
64+
AdminUrl = "https://$tenantName-admin.sharepoint.com"
65+
TenantName = $tenantName
66+
SharePointUrl = "https://$tenantName.sharepoint.com"
67+
}
68+
}

Modules/CIPPCore/Public/New-CIPPSharepointSite.ps1

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,10 @@ function New-CIPPSharepointSite {
6565
$APIName = 'Create SharePoint Site',
6666
$Headers
6767
)
68-
$tenantName = (New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/sites/root' -asApp $true -tenantid $TenantFilter).id.Split('.')[0]
69-
$AdminUrl = "https://$($tenantName)-admin.sharepoint.com"
70-
$SitePath = $SiteName -replace ' ' -replace '[^A-Za-z0-9-]'
71-
$SiteUrl = "https://$tenantName.sharepoint.com/sites/$SitePath"
72-
73-
7468

69+
$SharePointInfo = Get-SharePointAdminLink -Public $false
70+
$SitePath = $SiteName -replace ' ' -replace '[^A-Za-z0-9-]'
71+
$SiteUrl = "https://$($SharePointInfo.TenantName).sharepoint.com/sites/$SitePath"
7572

7673
switch ($TemplateName) {
7774
'Communication' {
@@ -142,7 +139,7 @@ function New-CIPPSharepointSite {
142139
'accept' = 'application/json;odata.metadata=none'
143140
'odata-version' = '4.0'
144141
}
145-
$Results = New-GraphPostRequest -scope "$AdminUrl/.default" -uri "$AdminUrl/_api/SPSiteManager/create" -Body ($body | ConvertTo-Json -Compress -Depth 10) -tenantid $TenantFilter -ContentType 'application/json' -AddedHeaders $AddedHeaders
142+
$Results = New-GraphPostRequest -scope "$($SharePointInfo.AdminUrl)/.default" -uri "$($SharePointInfo.AdminUrl)/_api/SPSiteManager/create" -Body ($body | ConvertTo-Json -Compress -Depth 10) -tenantid $TenantFilter -ContentType 'application/json' -AddedHeaders $AddedHeaders
146143
}
147144

148145
# Check the results. This response is weird. https://learn.microsoft.com/en-us/sharepoint/dev/apis/site-creation-rest

Modules/CIPPCore/Public/Request-CIPPSPOPersonalSite.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ function Request-CIPPSPOPersonalSite {
3636
</ObjectPaths>
3737
</Request>
3838
"@
39-
$tenantName = (New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/sites/root' -asApp $true -tenantid $TenantFilter).id.Split('.')[0]
40-
$AdminUrl = "https://$($tenantName)-admin.sharepoint.com"
39+
40+
$SharePointInfo = Get-SharePointAdminLink -Public $false
4141

4242
try {
43-
$Request = New-GraphPostRequest -scope "$AdminURL/.default" -tenantid $TenantFilter -Uri "$AdminURL/_vti_bin/client.svc/ProcessQuery" -Type POST -Body $XML -ContentType 'text/xml'
43+
$Request = New-GraphPostRequest -scope "$($SharePointInfo.AdminUrl)/.default" -tenantid $TenantFilter -Uri "$($SharePointInfo.AdminUrl)/_vti_bin/client.svc/ProcessQuery" -Type POST -Body $XML -ContentType 'text/xml'
4444
if (!$Request.IsComplete) { throw }
4545
Write-LogMessage -headers $Headers -API $APIName -message "Requested personal site for $($UserEmails -join ', ')" -Sev 'Info' -tenant $TenantFilter
4646
return "Successfully requested personal site for $($UserEmails -join ', ')"

Modules/CIPPCore/Public/Set-CIPPSPOTenant.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ function Set-CIPPSPOTenant {
4444
process {
4545
if (!$SharepointPrefix) {
4646
# get sharepoint admin site
47-
$tenantName = (New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/sites/root' -asApp $true -tenantid $TenantFilter).id.Split('.')[0]
47+
$SharePointInfo = Get-SharePointAdminLink -Public $false
48+
$AdminUrl = $SharePointInfo.AdminUrl
4849
} else {
4950
$tenantName = $SharepointPrefix
51+
$AdminUrl = "https://$($tenantName)-admin.sharepoint.com"
5052
}
5153
$Identity = $Identity -replace "`n", '&#xA;'
52-
$AdminUrl = "https://$($tenantName)-admin.sharepoint.com"
5354
$AllowedTypes = @('Boolean', 'String', 'Int32')
5455
$SetProperty = [System.Collections.Generic.List[string]]::new()
5556
$x = 114

Modules/CIPPCore/Public/Set-CIPPSharePointPerms.ps1

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ function Set-CIPPSharePointPerms {
2020
Write-Information 'No URL provided, getting URL from Graph'
2121
$URL = (New-GraphGetRequest -uri "https://graph.microsoft.com/v1.0/users/$($UserId)/Drives" -asapp $true -tenantid $TenantFilter).WebUrl
2222
}
23-
$tenantName = (New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/sites/root' -asApp $true -tenantid $TenantFilter).id.Split('.')[0]
24-
$AdminUrl = "https://$($tenantName)-admin.sharepoint.com"
23+
24+
$SharePointInfo = Get-SharePointAdminLink -Public $false
25+
2526
$XML = @"
2627
<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName=".NET Library">
2728
<Actions>
@@ -39,7 +40,7 @@ function Set-CIPPSharePointPerms {
3940
</ObjectPaths>
4041
</Request>
4142
"@
42-
$request = New-GraphPostRequest -scope "$AdminURL/.default" -tenantid $TenantFilter -Uri "$AdminURL/_vti_bin/client.svc/ProcessQuery" -Type POST -Body $XML -ContentType 'text/xml'
43+
$request = New-GraphPostRequest -scope "$($SharePointInfo.AdminUrl)/.default" -tenantid $TenantFilter -Uri "$($SharePointInfo.AdminUrl)/_vti_bin/client.svc/ProcessQuery" -Type POST -Body $XML -ContentType 'text/xml'
4344
# Write-Host $($request)
4445
if (!$request.ErrorInfo.ErrorMessage) {
4546
$Message = "$($OnedriveAccessUser) has been $($RemovePermission ? 'removed from' : 'given') access to $URL"

0 commit comments

Comments
 (0)