12Knocksinna / Office365itpros

Office 365 for IT Pros PowerShell examples
MIT License
1.33k stars 577 forks source link

TeamsGroupsActivityReportV5.12 SPOUsage 401 unauthorized #106

Closed tommykr2 closed 6 months ago

tommykr2 commented 6 months ago

I'm getting the error below despite creating an app registration with the permissions requested.

Specifically, it seems to be the call to getSharePointSiteUsageDetail that throws 401 unauthorized, despite having Reports.Read.All permission with consent granted on the app registration. https://learn.microsoft.com/en-us/graph/api/reportroot-getsharepointsiteusagedetail?view=graph-rest-1.0&tabs=http

I'm using Microsoft.Graph module version 2.15.0

The script proceeds and starts processing groups but SPO Activity, SPO Storage Used, Number SPO Files are blank, N/A, 0.

Invoke-RestMethod : The remote server returned an error: (401) Unauthorized. At D:\OneDrive\TeamsGroupsActivityReportV5.12.ps1:272 char:17

12Knocksinna commented 6 months ago

There are some problems at present accessing SharePoint data. See https://office365itpros.com/2024/02/19/sharepoint-usage-data-issue/. This is probably the reason. We have to wait for Microsoft to fix the issue.

Dindin-Rules commented 6 months ago

I believe the code still has a issue prior to the one you mentioned.

I ran through that code and manually fixed all the issues and got it to pull data for all the sites but because there was no identifying information I could not get it to output into the report/grid view.

The very first issue I ran into was the variable headers had data inside the function but called outside the function, down where the SPO part is asking for it in the open script, headers output was null.

I moved headers out of the current function and into the getaccesstoken just for testing and had it return the value Return $Token, $Headers then I did the same to the global and another function and after that I could call headers from the open script with no issue and then found the same issue you mentioned about graph API returning no expected identifying information.

it looked like this. (This is from a test tenant, not private data as its deleted now) Report Refresh Date : 2024-03-12 Site Id : 00000000-0000-0000-0000-000000000000 Site URL : Owner Display Name : 82D28824CBDAF3EA9A---------------- Is Deleted : False Last Activity Date : File Count : 0 Active File Count : 0 Page View Count : 0 Visited Page Count : 0 Storage Used (Byte) : 1733493 Storage Allocated (Byte) : 27487790694400 Root Web Template : My Site Host Owner Principal Name : Report Period : 180

Report Refresh Date : 2024-03-12 Site Id : 00000000-0000-0000-0000-000000000000 Site URL : Owner Display Name : 82D28824CBDAF3EA9AD---------------- Is Deleted : False Last Activity Date : File Count : 0 Active File Count : 0 Page View Count : 0 Visited Page Count : 0 Storage Used (Byte) : 1585758 Storage Allocated (Byte) : 27487790694400 Root Web Template : Team Site Owner Principal Name : Report Period : 180

I found you could get the site ID from a error code {0} but could not use it in any usable fashion and I was able to output the site data/storage/other with the displayname (Write-Host ("SingleSiteData DisplayName = {0}" -f $SingleSiteData.DisplayName) -ForegroundColor Red) which seemed to be the site ID but could not figure out how to parse the data into the report with that data vs the expected ObjectID so I stopped.

unless I'm mistaken, variables defined in a function are local to that function and the original script does not output headers nor does the SPO part of the script call the function so it cant work that way.

let me know if I'm off base but that's my thoughts on it. :)

12Knocksinna commented 6 months ago

I will have a look later on. I recall cleaning up some code recently. Maybe I did too good a job.

Tony Redmond (from a mobile device)


From: Dindin-Rules @.> Sent: Monday, March 18, 2024 2:26:19 PM To: 12Knocksinna/Office365itpros @.> Cc: 12 Knocksinna (Gmail) @.>; State change @.> Subject: Re: [12Knocksinna/Office365itpros] TeamsGroupsActivityReportV5.12 SPOUsage 401 unauthorized (Issue #106)

I believe the code still has a issue prior to the one you mentioned.

I ran through that code and manually fixed all the issues and got it to pull data for all the sites but because there was no identifying information I could not get it to output into the report/grid view.

The very first issue I ran into was the variable headers had data inside the function but called outside the function, down where the SPO part is asking for it in the open script, headers output was null.

I moved headers out of the current function and into the getaccesstoken just for testing and had it return the value Return $Token, $Headers then I did the same to the global and another function and after that I could call headers from the open script with no issue and then found the same issue you mentioned about graph API returning no expected identifying information.

it looked like this. (This is from a test tenant, not private data as its deleted now) Report Refresh Date : 2024-03-12 Site Id : 00000000-0000-0000-0000-000000000000 Site URL : Owner Display Name : 82D28824CBDAF3EA9A---------------- Is Deleted : False Last Activity Date : File Count : 0 Active File Count : 0 Page View Count : 0 Visited Page Count : 0 Storage Used (Byte) : 1733493 Storage Allocated (Byte) : 27487790694400 Root Web Template : My Site Host Owner Principal Name : Report Period : 180

Report Refresh Date : 2024-03-12 Site Id : 00000000-0000-0000-0000-000000000000 Site URL : Owner Display Name : 82D28824CBDAF3EA9AD---------------- Is Deleted : False Last Activity Date : File Count : 0 Active File Count : 0 Page View Count : 0 Visited Page Count : 0 Storage Used (Byte) : 1585758 Storage Allocated (Byte) : 27487790694400 Root Web Template : Team Site Owner Principal Name : Report Period : 180

I found you could get the site ID from a error code {0} but could not use it in any usable fashion and I was able to output the site data/storage/other with the displayname which seemed to be the site ID but could not figure out how to parse the data into the report with that data vs the expected ObjectID so I stopped.

unless I'm mistaken, variables defined in a function are local to that function and the original script does not output headers nor does the SPO part of the script call the function so it cant work that way.

let me know if I'm off base but that's my thoughts on it. :)

— Reply to this email directly, view it on GitHubhttps://github.com/12Knocksinna/Office365itpros/issues/106#issuecomment-2004063509, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AK45CIXFMC55YLYK6X3NQOLYY32QXAVCNFSM6AAAAABEU5UGIGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMBUGA3DGNJQHE. You are receiving this because you modified the open/close state.Message ID: @.***>

12Knocksinna commented 6 months ago

OK. I fixed some issues, mostly relating to the problem Microsoft has with returning SharePoint site URLs with usage data. I have released version 5.13. Perhaps you can try and see if it works for you.

tommykr2 commented 6 months ago

@12Knocksinna thank you for the quick update Tony! I ran the script yesterday and it ran and provided a pretty good report. Pretty quickly it said "processing details for 2381 sites" and kicked back "couldn't add details for site XYZ" for just a handful. I looked at script and the cause wasn't obvious to me. No big deal, it was only a few.

Then I did get a bunch of unhandled exceptions when processing groups; "Cannot index into a null array" from line 362 char: 9. It seems to be groups that have no owners. I spot checked several that I got the error for and indeed there were no group owners

I'm happy with the output, thank you.

12Knocksinna commented 6 months ago

I just fixed the no owner bug. I noticed that no group had an owner and that this was due to a change in the date returned by the Graph. I updated the code but didn’t accommodate ownerless groups like I should. Fixed now.

Tony Redmond (from a mobile device)


From: tommykr2 @.> Sent: Wednesday, March 20, 2024 3:06:23 PM To: 12Knocksinna/Office365itpros @.> Cc: 12 Knocksinna (Gmail) @.>; Mention @.> Subject: Re: [12Knocksinna/Office365itpros] TeamsGroupsActivityReportV5.12 SPOUsage 401 unauthorized (Issue #106)

@12Knocksinnahttps://github.com/12Knocksinna thank you for the quick update Tony! I ran the script yesterday and it ran and provided a pretty good report. Pretty quickly it said "processing details for 2381 sites" and kicked back "couldn't add details for site XYZ" for just a handful. I looked at script and the cause wasn't obvious to me. No big deal, it was only a few.

Then I did get a bunch of unhandled exceptions when processing groups; "Cannot index into a null array" from line 362 char: 9. It seems to be groups that have no owners. I spot checked several that I got the error for and indeed there were no group owners

I'm happy with the output, thank you.

— Reply to this email directly, view it on GitHubhttps://github.com/12Knocksinna/Office365itpros/issues/106#issuecomment-2009657580, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AK45CIVRBNAYTSZTJ3W334TYZGJV7AVCNFSM6AAAAABEU5UGIGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMBZGY2TONJYGA. You are receiving this because you were mentioned.Message ID: @.***>