Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get-MgAuditLogDirectoryAudit : The stream was already consumed. It cannot be read again. #7

Open
chris-dnv opened this issue Mar 29, 2024 · 8 comments

Comments

@chris-dnv
Copy link

Hi! I'm trying to run your pipeline but get the following error, the pipeline ran for over 8 hours could it be as per this issue that the access token needs refreshed? : https://github.com/microsoftgraph/msgraph-sdk-powershell/issues/1911

##[error]Get-MgAuditLogDirectoryAudit : The stream was already consumed. It cannot be read again.
At C:\a\_temp\561e9809-62b8-4fbf-845c-c93ab44c31a1.ps1:171 char:3
+   $modificationEvent = Get-MgAuditLogDirectoryAudit -All -Filter $eve ...
    + CategoryInfo          : NotSpecified: (:) [Get-MgAuditLogDirectoryAudit_List], InvalidOperationException
    + FullyQualifiedErrorId : Microsoft.Graph.PowerShell.Cmdlets.GetMgAuditLogDirectoryAudit_List
##[debug]Processed: ##vso[task.logissue source=CustomerScript;type=error]Get-MgAuditLogDirectoryAudit : The stream was already consumed. It cannot be read again.
At C:\a\_temp\561e9809-62b8-4fbf-845c-c93ab44c31a1.ps1:171 char:3
+   $modificationEvent = Get-MgAuditLogDirectoryAudit -All -Filter $eve ...
    + CategoryInfo          : NotSpecified: (:) [Get-MgAuditLogDirectoryAudit_List], InvalidOperationException
    + FullyQualifiedErrorId : Microsoft.Graph.PowerShell.Cmdlets.GetMgAuditLogDirectoryAudit_List
##[error]PowerShell exited with code '1'.
@ztrhgf
Copy link
Owner

ztrhgf commented Apr 2, 2024

Hi,
this is the initial backup run (a.k.a. the first one)? 8 hours seem like bug, or do you have huge Azure environment?

@chris-dnv
Copy link
Author

Yes we have a large environment so the run does take around 8-9 hours (i have another export workflow but I was trying yours for the logging capability)

@ztrhgf
Copy link
Owner

ztrhgf commented Apr 2, 2024

I see. I haven't seen this error, but google tells me that you are right and it is because token has to be renewed. You are on your own I am afraid. I don't know how to fix this right now.

@chris-dnv
Copy link
Author

No problem, thanks for taking a look

@ztrhgf
Copy link
Owner

ztrhgf commented Apr 3, 2024

I was thinking about it and there is one obvious workaround. Modify the code to split audit log searches by 24 hours and before each search make new authentication. And just join the results afterwards.

@MendelD
Copy link

MendelD commented Oct 9, 2024

Getting the same error in my environment since september 10th.
Pretty large environment as well, the pipeline runs for maximum of 6 hours (timeoutInMinutes: 360), but seems to fail with this error just around 1 hour and 15 minutes.
Might this be related to token lifetime?
https://learn.microsoft.com/en-us/entra/identity-platform/configurable-token-lifetimes

@ztrhgf
Copy link
Owner

ztrhgf commented Oct 10, 2024

Just to be sure, what pipeline are we talking about? Azure or Intune? I assume Azure.

In the pipeline output there should be something like:

"`n$(Get-Date -format 'HH:mm') (UTC) Getting Azure Audit events"
"`t- from: '$lastCommitDate' (UTC) to: '$backupStart' (UTC)"
"`t- filter: $eventFilter"

before Get-MgAuditLogDirectoryAudit is being called.

What does it say in your case?

Have you tried to split the searches by 24 hours as I suggested in the previous comment?

@ztrhgf ztrhgf reopened this Oct 10, 2024
@chris-dnv
Copy link
Author

Hi, from what I remember using certificate based auth for the service principal used to run the script fixed this issue in my testing (I never tested using a workload identity), hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants