SOLVED

Help with parameter for Search-UnifiedAuditLog

%3CLINGO-SUB%20id%3D%22lingo-sub-65437%22%20slang%3D%22en-US%22%3EHelp%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-65437%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDisclaimer%3A%20I%20am%20new%20to%20PowerShell%2C%20hence%20why%20I%20turn%20here%20for%20your%20input.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EBackground%3A%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EI'm%20creating%20a%20Power%20BI%20dashboard%20based%20on%20data%20exported%20from%20the%20O365%20Audit%20Log.%20For%20the%20moment%2C%20I'm%20not%20using%20the%20recently%20launched%20API%2C%20but%20a%20daily%20scheduled%20PowerShell%20script%20to%20export%20the%20data.%20Everything%20works%20fine%20with%20one%20exception%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EIssue%3A%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EThe%20maximum%20number%20of%20objects%20that%20can%20be%20returned%20in%20one%20query%20is%205000%20(see%20article%20for%20details).%20We%20have%20a%20fairly%20small%20tenant%2C%20so%20I%20have%20divided%20the%20cmdlet%20to%20run%20twice%20to%20cover%2024h%20(AM%2BPM)%2C%20that%20gives%20us%2010k%20rows%20which%20usually%20is%20enough.%20Now%20the%20exception%20is%20when%20we%20run%20SharePoint%20backups%20via%20Metalogix.%20The%20operation%20is%20of%20course%20walking%20through%20every%20single%20file%20which%20results%20in%20logs%20well%20above%2010k%20rows.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3ESolution%3F%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EWhat%20I%20want%20to%20achieve%20is%20to%20exclude%20the%20user%20account%20associated%20with%20the%20backup%20operation%2C%20to%20only%20return%20%22relevant%22%20objects%2Fevents.%20I%20was%20hoping%20to%20archive%20this%20by%20using%20the%20parameters%20provided%20in%20the%20cmdlet%2C%20but%20with%20my%20limited%20knowledge%20I%20can't%20figure%20it%20out.%20I%20can%20pass%20user%20ID's%20to%20include%2C%20but%20would%20this%20also%20allow%20me%20to%20exclude%20by%20user%20ID%3F%20If%20so%2C%20how%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20can%20always%20divide%20the%20cmdlet%20to%20run%20more%20than%20twice%20per%2024h%2C%20allowing%20more%20objects%20to%20be%20returned%2C%20but%20I%20hope%20there%20is%20a%20better%20solution%20to%20this.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechnet.microsoft.com%2Fen-us%2Flibrary%2Fmt238501%2528v%3Dexchg.160%2529.aspx%3Ff%3D255%26amp%3BMSPPError%3D-2147217396%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Ftechnet.microsoft.com%2Fen-us%2Flibrary%2Fmt238501%2528v%3Dexchg.160%2529.aspx%3Ff%3D255%26amp%3BMSPPError%3D-2147217396%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMany%20thanks!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-65437%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESearch-UnifiedAuditLog%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-184209%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-184209%22%20slang%3D%22en-US%22%3E%3CP%3ESiteIds%20absolutely%20does%20work%2C%20but%20as%20you%20indicate%2C%20the%20problem%20is%20to%20find%20the%20GUID%20for%20the%20site.%20If%20you%20have%20an%20audit%20record%20for%20an%20action%20performed%20in%20the%20site%2C%20you%20can%20find%20it%20in%20the%20%22more%20information%22%20details%20for%20the%20record.%20You%20can%20then%20do%20something%20like%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EPS%20C%3A%5C%26gt%3B%20Search-UnifiedAuditLog%20-SiteIds%20acfe74d8-edfb-436d-924b-e018666605ee%20-StartDate%201-dec-2017%20-EndDate%2018-apr-2018%20%7C%20ft%20creat%0Aiondate%2C%20userids%2C%20operations%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-184189%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-184189%22%20slang%3D%22en-US%22%3E%3CP%3EI%20believe%20the%20SiteIds%20parameter%20does%20work%2C%20you%20just%20need%20to%20use%20the%20Site%20GUID%20(which%20you%20can%20obtain%20from%20the%20Audit%20Log%20Search%20in%20the%20Security%20and%20Compliance%20Center).%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-140209%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-140209%22%20slang%3D%22en-US%22%3EThe%20SiteIds%20parameter%20doesn't%20work%2C%20i%20tried%20passing%20in%20WebId%2C%20Site%20but%20i%20get%20error%3CBR%20%2F%3E%3CBR%20%2F%3EThe%20Site%20Id%20search%20is%20not%20yet%20supported.%3CBR%20%2F%3E%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20InvalidArgument%3A%20(%3A)%20%5BSearch-UnifiedAuditLog%5D%2C%20NotSupportedException%3CBR%20%2F%3E%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20%5BServer%3DVI1P191MB0240%2CRequestId%3D800fa919-711b-47ee-9915-699b8684a804%2CTimeStamp%3D28%2F12%2F2017%2011%3A30%3A37%5D%20%5BFailureCategory%3DCmdlet-NotSupportedException%5D%20518C5EC4%2CMicrosoft.Exchange.Management.SystemConfigurationTasks.Searc%20%3CBR%20%2F%3E%20%20%20hUnifiedAuditLog%3CBR%20%2F%3E%20%20%20%20%2B%20PSComputerName%20%20%20%20%20%20%20%20%3A%20outlook.office365.com%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-89006%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-89006%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%2C%20I%20will%20try%20again%20using%20your%20tips.%20Appreciate%20your%20help.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-88963%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-88963%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F47873%22%20target%3D%22_blank%22%3E%40Rajiv%20Chokshi%3C%2FA%3E%26nbsp%3Brefering%20to%20the%20Technet%20article%20that%20I%20linked%20in%20the%20end%20of%20my%20answer%3B%20You%20can%20use%20the%20%22%3CEM%3EOperation%3C%2FEM%3E%22%20parameter%20to%20define%20the%20specific%20activities%20that%20you%20are%20looking%20for%2C%20but%20I%20would%20probably%20recommend%20you%20try%20querying%26nbsp%3Ball%20of%20them%20first%20and%20then%20review%20and%20select%20the%20ones%20you%20care%20about.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUsing%20the%20%22%3CEM%3ERecordType%3C%2FEM%3E%22%20parameter%2C%20I%20see%20that%20you%20can%20also%20filter%20the%20search%20to%20for%20example%20include%20things%20like%3A%3C%2FP%3E%3CUL%3E%3CLI%3ESharePointFileOperation%3C%2FLI%3E%3CLI%3E%3CP%3ESharePointSharingOperation%3C%2FP%3E%3C%2FLI%3E%3C%2FUL%3E%3CP%3EThe%20same%20with%20the%20site%20collection%20filter.%20I%20do%20not%20know%20directly%20which%20parameter%20that%20works%20best%20for%20this.%20As%20you%20can%20see%20you%20have%20one%20called%20%22%3CEM%3ESiteIds%3C%2FEM%3E%22%20which%20could%20work%2C%20but%20I'm%20not%20sure%20that%20the%20ID%20stays%20the%20same%20for%20all%20sites%20or%20sub%20sites%20of%20a%20site%20collection.%20Maybe%20it%20would%20be%20better%20to%20use%20%22%3CEM%3EObjectIds%3C%2FEM%3E%22%20which%20is%20queried%20as%20string%20and%20returns%20the%20object%20as%20a%20URL.%20So%20defining%20the%20site%20collection%20url%20%2B%20a%20wildcard%20characters%20should%20hopefully%20work.%20Something%20like%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3ESiteIds%20https%3A%2F%2Fcontoso.sharepoint.com%2Fsites%2Fsitecollectionname*%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20this%20helps!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-88934%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-88934%22%20slang%3D%22en-US%22%3E%3CP%3EMy%20needs%20are%20specifc%20to%20SharePoint%20online.%20Using%20the%20GUI%2C%20I%20am%20able%20to%20get%20below%205%20different%20reports%20and%20I%20am%20working%20on%20figuring%20out%20the%20correct%20PowerShell%20parameters%20to%20use%20and%20combine%20all%205%20using%20the%20append%20option%20to%20get%20EVERYTHING%20that%20happened%20on%20a%20SINGLE%20site%20collection%20into%20one%20report.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFile%20and%20page%20activities%3CBR%20%2F%3EFolder%20activities%3CBR%20%2F%3ESharing%20and%20access%20request%20activities%3CBR%20%2F%3ESynchronization%20activities%3CBR%20%2F%3ESite%20administration%20activities%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-88860%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-88860%22%20slang%3D%22en-US%22%3E%3CP%3EGiven%20what%20I%20see%20for%20Exchange%20Online%20events%2C%20if%20you%20added%20them%20into%20the%20mix%20and%20add%20some%20compliance%20like%20Office%20365%20retention%20policies%2C%20you%20might%20double%20the%20data%20you%20gather%20daily...%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-88858%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-88858%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F64%22%20target%3D%22_blank%22%3E%40Tony%20Redmond%3C%2FA%3E%26nbsp%3Bfully%20agree%2C%20especially%20if%20you%20have%20a%20large%20tenant%20%2F%20many%20users.%20I%20set%20my%20search%20date%20interval%20to%2024%20hours%20and%20have%20about%20200%20licensed%20users%2C%20and%20I%20am%20getting%20around%203000-4000%20rows%20per%20day%20(or%20about%201.5%20mb%20data).%20However%2C%20a%20majority%20of%20the%20records%20are%20associated%20with%20a%20services%20account%20crawling%20the%20tenant%20for%20daily%20backup.%20Additionally%2C%20we%20are%20only%20using%20Office%20365%20for%20SharePoint%2C%20so%20no%20Exchange%2C%20OneDrive%20or%20other%20service%20that%20would%26nbsp%3Bpush%20the%20number%20of%20returned%20records%20to%20a%20whole%20new%20level.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-88839%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-88839%22%20slang%3D%22en-US%22%3E%3CP%3EOne%20thing%20that%20strikes%20me%20about%20searching%20the%20audit%20log%20with%20PowerShell%20is%20that%20unless%20you%20are%20very%20careful%20with%20the%20search%20criteria%2C%20you're%20going%20to%20see%20a%20heap%20of%20data%20that%20you%20might%20not%20want%20to%20process.%20There%20are%20now%20so%20many%20workloads%20providing%20audit%20records%20to%20the%20data%20mart%20that%20you%20might%20be%20flooded%20with%20records%20you%20don't%20expect...%20So%20make%20sure%20that%20your%20search%20parameters%20are%20tuned%20to%20request%20data%20for%20the%20operations%20you%20want%20to%20examine%20in%20the%20period%20you%20need%20to%20see...%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-88794%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-88794%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F47873%22%20target%3D%22_blank%22%3E%40Rajiv%20Chokshi%3C%2FA%3E%20Sorry%20for%20the%20delayed%20reply.%20Yes%2C%20this%20should%20be%20possible.%20Anything%20you%20can%20query%20in%20the%20Audit%20log%20in%20the%20Security%20%26amp%3B%20Compliance%20Center%2C%20can%20be%20pulled%20from%20the%20Audit%20log%20using%20Search-UnifiedAuditLog%2C%20and%20exported%20to%20CSV.%20So%20start%20with%20going%20to%20the%20Office%20365%20Audit%20log%20and%20confirm%20that%20you%20can%20find%20the%20actions%20you%20are%20looking%20for.%3CBR%20%2F%3E%3CBR%20%2F%3EThen%20you%20can%20start%20on%20designing%20the%20script.%20To%20get%20you%20started%2C%20below%20is%20a%20copy%20of%20the%20scripts%20that%20I%20am%20using.%20It%20is%20looping%20the%20cmdlet%20using%20the%20session%20parameter%20until%20all%20the%20records%20are%20exported%20to%20the%20CSV%20file.%20When%20I'm%20converting%20from%20JSON%2C%20and%20when%20exporting%20to%20CSV%2C%20I%20am%20selecting%20certain%20objects%20that%20are%20of%20value%20for%20my%20use%20case%2C%20but%20there%20are%20more%20object%20holding%20data%20that%20might%20be%20valuable%20for%20you.%20Taking%20away%20the%20selection%20should%20bring%20it%20all%20in.%20Also%2C%20I%20am%20searching%203%20days%20back%20in%20time%20as%20I%20have%20noticed%20that%20some%20records%20take%20that%20long%20before%20they%20are%20saved%20to%20the%20audit%20log.%20You%20can%20experiment%20with%20this%20and%20compare%20the%20number%20of%20rows%20you%20get%20depending%20on%20how%20far%20back%20you%20set%20the%20interval.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20far%20away%20from%20an%20expert%20on%20PowerShell%2C%20but%20with%20patience%20and%20help%20from%20our%20database%20engineer%20at%20work%2C%20we%20have%20created%20a%20Power%20BI%20dashboard%20that%20can%20display%20all%20audit%20log%20actions%20across%20the%20whole%20tenant%20in%20a%20user%20friendly%20and%20graphical%20way.%20It%20gets%20new%20data%20from%20the%20script%20on%20a%20daily%20basis%2C%20and%20as%20it%20is%20exported%20you%20will%20have%20no%20limits%20in%20searching%20back%20in%20time%2C%20compared%20to%20the%20(newly%20announced)%2090%20days%20that%20MSFT%20saves%20for%20the%20Audit%20log.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20note%20that%20you%20should%20start%20by%20using%20a%20script%20to%20securely%20save%20(encrypt)%20your%20credentials%2C%20which%20in%20turn%20are%20called%20in%20the%20beginning%20of%20the%20second%20script%20(you%20only%20need%20to%20use%20this%20once).%20%26nbsp%3BYou%20probably%20create%20a%20service%20account%20for%20this%20and%20set%20it%20up%20with%20minimal%20permissions%20needed%20(more%20under%20the%20%22Before%20you%20begin%22%20tab%20under%20%3CA%20href%3D%22https%3A%2F%2Fsupport.office.com%2Fen-us%2Farticle%2FSearch-the-audit-log-in-the-Office-365-Security-Compliance-Center-0d4d0f35-390b-4518-800e-0c7ec95e946c%3Fui%3Den-US%26amp%3Brs%3Den-US%26amp%3Bad%3DUS%26amp%3BfromAR%3D1%23PickTab%3DBYB%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethis%20link%3C%2FA%3E).%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%20script%20to%20store%20your%20credentials%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Eparam(%0A%20%20%20%20%5BParameter(Mandatory%3D%24true)%5D%0A%20%20%20%20%5Bstring%5D%24Path%2C%0A%20%20%20%20%5BParameter(Mandatory%3D%24true)%5D%0A%20%20%20%20%5Bstring%5D%24password%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Convert%20the%20password%20to%20a%20secure%20string%0A%20%20%20%20%24SecurePassword%20%3D%20%24password%20%7C%20ConvertTo-SecureString%20-AsPlainText%20-Force%0A%20%20%20%20%0A%20%20%20%20%23%20Store%20the%20credential%20in%20the%20path%0A%20%20%20%20%24SecurePassword%20%7C%20ConvertFrom-SecureString%20%7C%20Out-File%20%24Path%0A%20%20%20%20%0A%20%20%20%20%23%20Write%20What%20we%20did%0A%20%20%20%20Write-Host%20%22Wrote%20password%20to%20%24path%22%20%20%20%20%0A%20%20%20%20%0A%26lt%3B%23%20%0A.SYNOPSIS%0AStores%20a%20password%20in%20a%20file%20on%20the%20local%20computer%20for%20retrevial%20by%20scripts.%0A%0A.DESCRIPTION%0AUsed%20for%20securely%20storing%20a%20password%20on%20a%20machine%20for%20use%20with%20automated%20scripts.%0A%0ATakes%20a%20password%20and%20encrypts%20it%20using%20the%20local%20account%2C%20then%20stores%20that%20password%20in%20a%20file%20you%20specify.%0AOnly%20the%20account%20that%20creates%20the%20output%20file%20can%20decrypt%20the%20password%20stored%20in%20the%20file.%0A%0A.PARAMETER%20Path%0APath%20and%20file%20name%20for%20the%20password%20file%20that%20will%20be%20created.%0A%0A.PARAMETER%20Password%0APlain%20text%20version%20of%20password.%0A%0A.OUTPUTS%0AFile%20Specified%20in%20Path%20variable%20will%20contain%20an%20encrypted%20version%20of%20the%20password.%0A%0A.EXAMPLE%0A.%5CNew-StoredCredential.ps1%20-Path%20c%3A%5Cscripts%5CO365Account.txt%20-Password%20%22Password123%22%0A%0APuts%20the%20encrypted%20version%20of%20Password123%20into%20the%20c%3A%5Cscripts%5CO365Account.txt%20file%0A%23%26gt%3B%3C%2FPRE%3E%3CP%3ESecond%20script%20to%20pull%20data%20from%20the%20audit%20log%20(you%20need%20to%20define%20your%20own%20variables%20in%20some%20places%20as%20I%20have%20removed%20mine%20from%20this%20copy)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%23%20Set%20constants%0A%24Username%20%3D%20%22%5BYOUR%20ACCOUNT%2C%20RECOMMENDED%20TO%20USE%20THE%20PRINCIPLE%20OF%20LEAST%20PRIVILEGE%5D%22%0A%24PasswordPath%20%3D%20%22%5BYOUR%20PATH%20DEFINED%20IN%20SCRIPT%20ABOVE%5D%22%0A%0A%23%20Read%20the%20password%20from%20the%20file%20and%20convert%20to%20SecureString%0AWrite-Host%20%22Getting%20password%20from%20%24passwordpath%22%0A%24SecurePassword%20%3D%20Get-Content%20%24PasswordPath%20%7C%20ConvertTo-SecureString%0A%0A%23%20Build%20a%20Credential%20Object%20from%20the%20password%20file%20and%20the%20%24username%20constant%0A%24Credential%20%3D%20New-Object%20System.Management.Automation.PSCredential%20-ArgumentList%20%24Username%2C%20%24SecurePassword%0A%0A%23%20Open%20a%20session%20to%20Exchange%20Online%20were%20the%20audit%20log%20cmdlet%20is%20located%0A%24O365Session%20%3D%20New-PSSession%20-ConfigurationName%20Microsoft.Exchange%20-ConnectionUri%20https%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%20-Credential%20%24Credential%20-Authentication%20Basic%20-AllowRedirection%0AImport-PSsession%20%24O365session%0A%0A%23%20Define%20dates%20for%20search.%20MSFT%20claims%20Windows%20short%20date%20format%20on%20the%20machine%20running%20the%20script%20should%20be%20used%2C%20but%20test%20proved%20%22MM%2Fdd%2Fyyyy%22%20must%20be%20used%0A%24StartDate%20%3D%20(get-date).AddDays(-3).ToString(%22MM%2Fdd%2Fyyyy%22)%0A%24EndDate%20%3D%20(get-date).AddDays(-2).ToString(%22MM%2Fdd%2Fyyyy%22)%0A%0A%23%20Define%20file%20name%20and%20path%20for%20csv%20export%0A%24FileAppend%20%3D%20(get-date).AddDays(-3).ToString(%22yyyyMMdd%22)%0A%23%20%5Bstring%5D%24FileAppend%20%3D%20(Get-Date%20-Format%20yyyyMMdd)%0A%24OutputFile%20%3D%20%22%5BDEFINE%20OUTPUT%20FILE%20PATH%5D%22%20%2B%20%24FileAppend%20%2B%20%22.csv%22%0A%0A%23%20Set%20variable%20value%20to%20trigger%20loop%20below%20(can%20be%20anything)%0A%24AuditOutput%20%3D%201%0A%0A%23%20Loop%20will%20run%20until%20%24AuditOutput%20returns%20null%20which%20equals%20that%20no%20more%20event%20objects%20exists%20from%20the%20specified%20date%0Awhile%20(%24AuditOutput)%20%7B%0A%0A%20%20%20%20%23%20Search%20the%20defined%20date(s)%2C%20SessionId%20%2B%20SessionCommand%20in%20combination%20with%20the%20loop%20will%20return%20and%20append%20100%20object%20per%20iteration%20until%20all%20objects%20are%20returned%20(minimum%20limit%20is%2050k%20objects)%0A%20%20%20%20%24AuditOutput%20%3D%20Search-UnifiedAuditLog%20-StartDate%20%24StartDate%20-EndDate%20%24EndDate%20-SessionId%20%221%22%20-SessionCommand%20ReturnLargeSet%0A%0A%20%20%20%20%23%20Select%20and%20expand%20the%20nested%20object%20(AuditData)%20as%20it%20holds%20relevant%20reporting%20data.%20Convert%20output%20format%20from%20default%20JSON%20to%20enable%20export%20to%20csv%0A%20%20%20%20%24ConvertedOutput%20%3D%20%24AuditOutput%20%7C%20Select-Object%20-ExpandProperty%20AuditData%20%7C%20ConvertFrom-Json%0A%0A%20%20%20%20%23%20Export%20results%20exluding%20type%20information.%20Append%20rather%20than%20overwrite%20if%20the%20file%20exist%20in%20destination%20folder%0A%20%20%20%20%24ConvertedOutput%20%7C%20Select-Object%20CreationTime%2CUserId%2COperation%2CWorkload%2CObjectID%2CSiteUrl%2CSourceFileName%2CClientIP%2CUserAgent%20%20%7C%20Export-Csv%20%24OutputFile%20-NoTypeInformation%20-Append%0A%7D%0A%0A%23%20Close%20the%20sesssion%20to%20Exhange%20Online%0ARemove-PSSession%20%24O365Session%3C%2FPRE%3E%3CP%3EI%20hope%20this%20helps%20to%20get%20you%20started.%20You%20can%20read%20more%20about%20the%20audit%20log%20and%20how%20you%20can%20limit%20it's%20scope%20if%20needed%20%3CA%20href%3D%22https%3A%2F%2Ftechnet.microsoft.com%2Fen-us%2Flibrary%2Fmt238501%2528v%3Dexchg.160%2529.aspx%3Ff%3D255%26amp%3BMSPPError%3D-2147217396%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-85404%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-85404%22%20slang%3D%22en-US%22%3E%3CP%3EIs%20this%20possible%20with%20the%20native%20%3CSPAN%3EPowerShell%20command%20search-unifiedauditlog%3F%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EI%20am%20trying%20to%20figure%20out%20if%20it%20is%20possible%20to%20get%20a%20%3CU%3ESINGLE%3C%2FU%3E%20report%20for%20%3CU%3Eall%20activities%3C%2FU%3E%20on%20a%20%3CU%3Eparticular%26nbsp%3BSPO%20site%20collection%3C%2FU%3E%20using%20the%20PowerShell%20command%20%3CU%3Esearch-unifiedauditlog.%3C%2FU%3E%3C%2FP%3E%0A%3CP%3EFor%20example%20I%20want%20to%20know%20everything%20that%20happened%20on%20below%20site%20collection%20in%20the%20last%20%26nbsp%3B30%20days%20%3CA%20href%3D%22https%3A%2F%2Fcontoso.sharepoint.com%2Fsites%2Fprojectabc%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fcontoso.sharepoint.com%2Fsites%2Fprojectabc%3C%2FA%3E%20and%20this%20should%20include%20all%20of%20these%20%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFile%20and%20page%20activities%3C%2FP%3E%0A%3CP%3EFolder%20activities%3C%2FP%3E%0A%3CP%3ESharing%20and%20access%20request%20activities%3C%2FP%3E%0A%3CP%3ESynchronization%20activities%3C%2FP%3E%0A%3CP%3ESite%20administration%20activities%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%20in%20advance.%20I%20was%20able%20to%20do%20this%20with%20AdminDroid%20but%20looking%20to%20see%20if%20the%20native%20command%20can%20accomplish%20this.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-65885%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-65885%22%20slang%3D%22en-US%22%3E%3CP%3ERemember%20that%20if%20you%20build%20a%20solution%2C%20you%20have%20to%20maintain%20the%20code...%20and%20things%20break%2C%20like%20APIs.%20That's%20one%20of%20the%20reasons%20why%20it%20is%20sometimes%20better%20to%20go%20with%20a%20commercial%20solution%20and%20let%20the%20ISV%20have%20the%20headache%20of%20maintaining%20the%20code%20and%20making%20sure%20that%20it%20works%2C%20even%20when%20the%20APIs%20are%20in%20flux%20(as%20does%20happen%20inside%20Office%20365).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETR%3C%2FP%3E%3CBLOCKQUOTE%3E%3CHR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F8584%22%20target%3D%22_blank%22%3E%40Pontus%20T%3C%2FA%3E%20wrote%3A%3CBR%20%2F%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F64%22%20target%3D%22_blank%22%3E%40Tony%20Redmond%3C%2FA%3E%26nbsp%3BThank%20you%20for%20the%20tip%20about%26nbsp%3BCogmotive.%20I%20will%20have%20a%20look%20at%20the%20product%20for%20sure.%20However%2C%20I%20like%20the%20customizability%20that%20I%20get%20in%20Power%20BI%2C%20building%20interactive%20reports%20for%20different%20stakeholders.%20And%20I%20guess%20it's%20something%20about%20the%20challenge%20of%20building%20it%20yourself%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3CHR%20%2F%3E%3C%2FBLOCKQUOTE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-65827%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-65827%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F3694%22%20target%3D%22_blank%22%3E%40NarasimaPerumal%20Chandramohan%3C%2FA%3E%26nbsp%3Bthanks%20for%20pointing%20me%20in%20the%20right%20direction.%20I%20managed%20to%20solve%20it%20by%20using%26nbsp%3B%3CEM%3ESessionID%26nbsp%3B%3C%2FEM%3Eand%26nbsp%3B%3CEM%3ESessionCommand%3C%2FEM%3E.%20All%20I%20needed%20was%20a%20while%20loop%20that%20kept%20running%20until%20the%20variable%20taking%20the%20audit%20data%20returned%20null%2C%20and%20keep%20appending%20the%20export%20file%20in%20every%20loop%20run.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-65776%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-65776%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F64%22%20target%3D%22_blank%22%3E%40Tony%20Redmond%3C%2FA%3E%26nbsp%3BThank%20you%20for%20the%20tip%20about%26nbsp%3BCogmotive.%20I%20will%20have%20a%20look%20at%20the%20product%20for%20sure.%20However%2C%20I%20like%20the%20customizability%20that%20I%20get%20in%20Power%20BI%2C%20building%20interactive%20reports%20for%20different%20stakeholders.%20And%20I%20guess%20it's%20something%20about%20the%20challenge%20of%20building%20it%20yourself%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-65762%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-65762%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20unaware%20of%20a%20way%20to%20exclude%20accounts%20from%20a%20cmdlet%20pull%20for%20audit%20records.%20There%20might%20be%20an%20easier%20way%20for%20you%20to%20do%20this...%20Cogmotive%20Reports%20has%20a%20Discover%20and%20Audit%20module%20that%20works%20against%20the%20audit%20log%20records%20(in%20other%20words%2C%20the%20same%20data)%20and%20it%20comes%20with%20a%20pivot%20table%20capability%20that%20you%20might%20be%20able%20to%20do%20what%20you%20want.%20They%20also%20are%20able%20to%20provide%20more%20data%20than%205%2C000%20records%20at%20a%20time%20because%20they%20store%20the%20audit%20log%20data%20in%20their%20own%20stores.%20You%20could%20try%20that...%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-65750%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-65750%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F3694%22%20target%3D%22_blank%22%3E%40NarasimaPerumal%20Chandramohan%3C%2FA%3E%26nbsp%3BI%20have%20been%20fiddling%20around%20with%20that%20a%20bit%20but%20did%20not%20manage%20to%20get%20it%20to%20work.%20To%20me%20the%20instructions%20on%20how%20to%20use%20it%20are%20a%20bit%20vague.%20I%20don't%20know%20what%20they%20mean%20with%20%3CEM%3EPaging%2C%20%3C%2FEM%3Eand%20a%20full%20tenant%20backup%20is%20likely%20to%20require%20more%20than%2050k%20objects%20to%20be%20returned.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHowever%2C%20it%20seems%20like%20I%20can%20use%20multiple%20session%20ID's%20and%20that%20each%20one%20will%20return%2050k%20if%20I%20use%20the%20ReturnLastSet%20parameter.%26nbsp%3BI%20think%20it's%20worth%20trying%20out%20again.%20I%20need%20to%20make%20sure%20that%20the%20rest%20of%20the%20script%20can%20handle%20the%20way%20objects%20are%20returned%20through%20paging%2C%20as%20it%20needs%20to%20convert%20from%20JSON%20to%20allow%20proper%20export.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20a%20bit%20of%20the%20script%20I%20use%20today%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24AuditOutputPM%20%3D%20Search-UnifiedAuditLog%20-StartDate%20%22%24StartDate%2012%3A00%20PM%22%20-EndDate%20%22%24EndDate%2023%3A59%20PM%22%20-ResultSize%205000%0A%24AuditOutputAM%20%3D%20Search-UnifiedAuditLog%20-StartDate%20%22%24StartDate%2000%3A01%20AM%22%20-EndDate%20%22%24EndDate%2011%3A59%20AM%22%20-ResultSize%205000%0A%0A%24ConvertedOutputPM%20%3D%20%24AuditOutputPM%20%7C%20Select-Object%20-ExpandProperty%20AuditData%20%7C%20ConvertFrom-Json%0A%24ConvertedOutputAM%20%3D%20%24AuditOutputAM%20%7C%20Select-Object%20-ExpandProperty%20AuditData%20%7C%20ConvertFrom-Json%0A%0A%24ConvertedOutputPM%20%7C%20Select-Object%20CreationTime%2CUserId%2COperation%2CWorkload%2CObjectID%2CSiteUrl%2CSourceFileName%2CClientIP%2CUserAgent%20%20%7C%20Export-Csv%20%24OutputFile%20-NoTypeInformation%20-Append%0A%24ConvertedOutputAM%20%7C%20Select-Object%20CreationTime%2CUserId%2COperation%2CWorkload%2CObjectID%2CSiteUrl%2CSourceFileName%2CClientIP%2CUserAgent%20%20%7C%20Export-Csv%20%24OutputFile%20-NoTypeInformation%20-Append%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-65746%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-65746%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3BYes%2C%20I%20NOT%20EQUAL%20operator%20would%20be%20the%20optimal%20thing%2C%20but%20I%20have%20not%20got%20that%20to%20work.%20I%20guess%20it's%20not%20supported%20as%20no%20similar%20functionality%20exists%20for%20the%20Audit%20Log%20interface%20in%20the%20Security%20%26amp%3B%20Complience%20Center.%20And%20to%20pass%20all%20users%20except%20one%20is%20not%20a%20great%20option%20unfortunately.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-65710%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-65710%22%20slang%3D%22en-US%22%3E%3CP%3EHave%20you%20checked%20the%20parameter%20%22%3CEM%3ESessionCommand%3C%2FEM%3E%22%20in%20the%20Search-UnifiedAuditLog%20cmdlet%3F.%20By%20using%20this%20you%20can%20get%20all%20the%20records.%20But%20you%20need%20to%20do%20the%20filters%20in%20the%20DB%20where%20you%20have%20stored%20the%20audit%20logs.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-65628%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-65628%22%20slang%3D%22en-US%22%3E%3CP%3EWhat%20you%20are%20looking%20for%20I%20guess%20is%20something%20like%20a%20%22NOT%22%20operator%2C%20which%20will%20allow%20you%20to%20exclude%20all%20actions%20from%20particular%20user%2Faccount.%20Unfortunately%2C%20I'm%20not%20aware%20of%20such%20for%20the%20UserIds%20parameter%20of%20the%20Search-UnifiedAuditLog%20cmdlet.%20I%20guess%20you%20can%20do%20it%20the%20other%20way%20around%2C%20use%20the%20UserIds%20parameter%20to%20explicitly%20list%20all%20users%2C%20apart%20for%20the%20service%20account%20used%20for%20backups.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFlagging%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F64%22%20target%3D%22_blank%22%3E%40Tony%20Redmond%3C%2FA%3E%26nbsp%3Bto%20double-check.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-668804%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-668804%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F8584%22%20target%3D%22_blank%22%3E%40Pontus%20T%3C%2FA%3E%26nbsp%3B%26nbsp%3B%20Hello%2C%20I%20love%20your%20script%20its%20exactly%20what%20we%20have%20been%20trying%20to%20accomplish%3B%20however%2C%20I%20have%20found%20a%20problem.%26nbsp%3B%20It%20seems%20that%20many%20records%20are%20duplicated%20and%20even%20though%20i%20am%20sorting%20the%20records%20in%20the%20command%20by%20creation%20date%2C%20they%20seem%20to%20be%20out%20of%20order.%26nbsp%3B%20Its%20almost%20as%20if%20the%20file%20is%20appended%20about%20100%20records%20at%20a%20time%20and%20i%20notice%20the%20creation%20dates%20jumbled.%20for%20example%20the%20creation%20dates%20will%20be%3A%3C%2FP%3E%3CTABLE%3E%3CTBODY%3E%3CTR%3E%3CTD%3E5%2F20%2F2019%200%3A19%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E5%2F19%2F2019%2021%3A23%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E5%2F16%2F2019%2022%3A40%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E5%2F16%2F2019%2020%3A34%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E5%2F16%2F2019%2020%3A30%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E5%2F16%2F2019%2020%3A30%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E5%2F16%2F2019%2020%3A30%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E5%2F16%2F2019%2012%3A28%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E5%2F16%2F2019%2012%3A26%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E5%2F24%2F2019%2021%3A00%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E5%2F24%2F2019%2020%3A59%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E5%2F24%2F2019%2020%3A59%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E5%2F24%2F2019%2020%3A58%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E5%2F24%2F2019%2020%3A58%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E5%2F24%2F2019%2020%3A57%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CP%3EThis%20wouldn't%20be%20so%20bad%20as%20I%20can%20sort%20the%20data%20in%20Excel%2C%20but%20because%20there%20are%20duplicate%20records%20it%20seems%20there%20may%20be%20some%20overlap%20during%20the%20append.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-671182%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-671182%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F354676%22%20target%3D%22_blank%22%3E%40benjoyner%3C%2FA%3E%20Would%20this%20help%3F%20%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Ftehnoonr%2F2018%2F01%2F26%2Fretrieving-office-365-audit-data-using-powershell%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fblogs.msdn.microsoft.com%2Ftehnoonr%2F2018%2F01%2F26%2Fretrieving-office-365-audit-data-using-powershell%2F%3C%2FA%3E%3C%2FFONT%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-671403%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-671403%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F47873%22%20target%3D%22_blank%22%3E%40Rajiv%20Chokshi%3C%2FA%3EThanks%20Rajiv%2C%20that%20script%20functions%20much%20as%20the%20one%20in%20this%20post%20functions%2C%20however%20it%20has%20some%20more%20functionality%20with%20the%20logging%20that%20I%20like.%26nbsp%3B%20However%2C%20there%20is%20still%20an%20issue%20with%20duplicate%20records%20and%20I%20see%20that%20the%20append%20function%20still%20seems%20to%20write%20the%20data%20out%20of%20order.%26nbsp%3B%20When%20I%20look%20at%20creation%20times%20they%20are%20broken%20up%3B%20the%20numbers%20below%20show%20how%20the%20appending%20is%20broken.%26nbsp%3B%20The%20creation%20times%20should%20all%20be%20written%20sequentially.%26nbsp%3B%20Any%20idea%20why%20this%20is%20not%20happening%3F%20does%20it%20need%20a%20separate%20flag%20or%20pipe%20to%20fix%20this%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E10%3C%2FP%3E%3CP%3E9%3C%2FP%3E%3CP%3E8%3C%2FP%3E%3CP%3E7%3C%2FP%3E%3CP%3E6%3C%2FP%3E%3CP%3E5%3C%2FP%3E%3CP%3E4%3C%2FP%3E%3CP%3E3%3C%2FP%3E%3CP%3E2%3C%2FP%3E%3CP%3E1%3C%2FP%3E%3CP%3E18%3C%2FP%3E%3CP%3E17%3C%2FP%3E%3CP%3E16%3C%2FP%3E%3CP%3E15%3C%2FP%3E%3CP%3E14%3C%2FP%3E%3CP%3E13%3C%2FP%3E%3CP%3E12%3C%2FP%3E%3CP%3E11%3C%2FP%3E%3CP%3E25%3C%2FP%3E%3CP%3E24%3C%2FP%3E%3CP%3E23%3C%2FP%3E%3CP%3E22%3C%2FP%3E%3CP%3E21%3C%2FP%3E%3CP%3E20%3C%2FP%3E%3CP%3E19%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-671464%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20parameter%20for%20Search-UnifiedAuditLog%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-671464%22%20slang%3D%22en-US%22%3EI%20was%20able%20to%20get%20this%20working.%20I%20had%20to%20add%20%22%20%7C%20Sort-object%20CreationDate%20%7C%20%22%20as%20a%20pipe%20reght%20before%20the%20export%20csv%20command.%20The%20default%20order%20is%20Ascending%20so%20that%20fixed%20the%20problem%20of%20the%20records%20out%20of%20order.%20As%20for%20the%20duplicate%20records%2C%20Looking%20in%20the%20office%20365%20service%20center%20it%20seems%20these%20records%20are%20already%20duplicated%20so%20its%20not%20a%20problem%20with%20overlap%20in%20the%20command.%20Furthermore%20does%20anybody%20know%20if%20its%20possible%20to%20download%20the%20audit%20logs%20and%20correct%20for%20time%20zone%3F%20it%20seems%20all%20the%20creation%20dates%20are%20listed%20in%20UTC%20rather%20than%20my%20local%20time%20zone%20as%20it%20is%20in%20the%20online%20interface.%3C%2FLINGO-BODY%3E
Highlighted
Contributor

Hi,

 

Disclaimer: I am new to PowerShell, hence why I turn here for your input.

 

Background:

I'm creating a Power BI dashboard based on data exported from the O365 Audit Log. For the moment, I'm not using the recently launched API, but a daily scheduled PowerShell script to export the data. Everything works fine with one exception:

 

Issue:

The maximum number of objects that can be returned in one query is 5000 (see article for details). We have a fairly small tenant, so I have divided the cmdlet to run twice to cover 24h (AM+PM), that gives us 10k rows which usually is enough. Now the exception is when we run SharePoint backups via Metalogix. The operation is of course walking through every single file which results in logs well above 10k rows.

 

Solution?

What I want to achieve is to exclude the user account associated with the backup operation, to only return "relevant" objects/events. I was hoping to archive this by using the parameters provided in the cmdlet, but with my limited knowledge I can't figure it out. I can pass user ID's to include, but would this also allow me to exclude by user ID? If so, how?

 

I can always divide the cmdlet to run more than twice per 24h, allowing more objects to be returned, but I hope there is a better solution to this. 

 

https://technet.microsoft.com/en-us/library/mt238501%28v=exchg.160%29.aspx?f=255&MSPPError=-21472173...

 

Many thanks!

23 Replies
Highlighted

What you are looking for I guess is something like a "NOT" operator, which will allow you to exclude all actions from particular user/account. Unfortunately, I'm not aware of such for the UserIds parameter of the Search-UnifiedAuditLog cmdlet. I guess you can do it the other way around, use the UserIds parameter to explicitly list all users, apart for the service account used for backups.

 

Flagging @Tony Redmond to double-check.

Highlighted

Have you checked the parameter "SessionCommand" in the Search-UnifiedAuditLog cmdlet?. By using this you can get all the records. But you need to do the filters in the DB where you have stored the audit logs.

 

 

Highlighted

@Vasil Michev Yes, I NOT EQUAL operator would be the optimal thing, but I have not got that to work. I guess it's not supported as no similar functionality exists for the Audit Log interface in the Security & Complience Center. And to pass all users except one is not a great option unfortunately.

Highlighted

@NarasimaPerumal Chandramohan I have been fiddling around with that a bit but did not manage to get it to work. To me the instructions on how to use it are a bit vague. I don't know what they mean with Paging, and a full tenant backup is likely to require more than 50k objects to be returned.

 

However, it seems like I can use multiple session ID's and that each one will return 50k if I use the ReturnLastSet parameter. I think it's worth trying out again. I need to make sure that the rest of the script can handle the way objects are returned through paging, as it needs to convert from JSON to allow proper export.

 

Here is a bit of the script I use today:

 

$AuditOutputPM = Search-UnifiedAuditLog -StartDate "$StartDate 12:00 PM" -EndDate "$EndDate 23:59 PM" -ResultSize 5000
$AuditOutputAM = Search-UnifiedAuditLog -StartDate "$StartDate 00:01 AM" -EndDate "$EndDate 11:59 AM" -ResultSize 5000

$ConvertedOutputPM = $AuditOutputPM | Select-Object -ExpandProperty AuditData | ConvertFrom-Json
$ConvertedOutputAM = $AuditOutputAM | Select-Object -ExpandProperty AuditData | ConvertFrom-Json

$ConvertedOutputPM | Select-Object CreationTime,UserId,Operation,Workload,ObjectID,SiteUrl,SourceFileName,ClientIP,UserAgent  | Export-Csv $OutputFile -NoTypeInformation -Append
$ConvertedOutputAM | Select-Object CreationTime,UserId,Operation,Workload,ObjectID,SiteUrl,SourceFileName,ClientIP,UserAgent  | Export-Csv $OutputFile -NoTypeInformation -Append
Highlighted

I am unaware of a way to exclude accounts from a cmdlet pull for audit records. There might be an easier way for you to do this... Cogmotive Reports has a Discover and Audit module that works against the audit log records (in other words, the same data) and it comes with a pivot table capability that you might be able to do what you want. They also are able to provide more data than 5,000 records at a time because they store the audit log data in their own stores. You could try that...

Highlighted

@Tony Redmond Thank you for the tip about Cogmotive. I will have a look at the product for sure. However, I like the customizability that I get in Power BI, building interactive reports for different stakeholders. And I guess it's something about the challenge of building it yourself :)

Highlighted
Best Response confirmed by Pontus T (Contributor)
Solution

@NarasimaPerumal Chandramohan thanks for pointing me in the right direction. I managed to solve it by using SessionID and SessionCommand. All I needed was a while loop that kept running until the variable taking the audit data returned null, and keep appending the export file in every loop run.

Highlighted

Remember that if you build a solution, you have to maintain the code... and things break, like APIs. That's one of the reasons why it is sometimes better to go with a commercial solution and let the ISV have the headache of maintaining the code and making sure that it works, even when the APIs are in flux (as does happen inside Office 365).

 

TR


@Pontus T wrote:

@Tony Redmond Thank you for the tip about Cogmotive. I will have a look at the product for sure. However, I like the customizability that I get in Power BI, building interactive reports for different stakeholders. And I guess it's something about the challenge of building it yourself :)


 

Highlighted

Is this possible with the native PowerShell command search-unifiedauditlog?

I am trying to figure out if it is possible to get a SINGLE report for all activities on a particular SPO site collection using the PowerShell command search-unifiedauditlog.

For example I want to know everything that happened on below site collection in the last  30 days https://contoso.sharepoint.com/sites/projectabc and this should include all of these : 

File and page activities

Folder activities

Sharing and access request activities

Synchronization activities

Site administration activities

 

Thanks in advance. I was able to do this with AdminDroid but looking to see if the native command can accomplish this.

Highlighted

@Rajiv Chokshi Sorry for the delayed reply. Yes, this should be possible. Anything you can query in the Audit log in the Security & Compliance Center, can be pulled from the Audit log using Search-UnifiedAuditLog, and exported to CSV. So start with going to the Office 365 Audit log and confirm that you can find the actions you are looking for.

Then you can start on designing the script. To get you started, below is a copy of the scripts that I am using. It is looping the cmdlet using the session parameter until all the records are exported to the CSV file. When I'm converting from JSON, and when exporting to CSV, I am selecting certain objects that are of value for my use case, but there are more object holding data that might be valuable for you. Taking away the selection should bring it all in. Also, I am searching 3 days back in time as I have noticed that some records take that long before they are saved to the audit log. You can experiment with this and compare the number of rows you get depending on how far back you set the interval.

 

I am far away from an expert on PowerShell, but with patience and help from our database engineer at work, we have created a Power BI dashboard that can display all audit log actions across the whole tenant in a user friendly and graphical way. It gets new data from the script on a daily basis, and as it is exported you will have no limits in searching back in time, compared to the (newly announced) 90 days that MSFT saves for the Audit log.

 

Please note that you should start by using a script to securely save (encrypt) your credentials, which in turn are called in the beginning of the second script (you only need to use this once).  You probably create a service account for this and set it up with minimal permissions needed (more under the "Before you begin" tab under this link). 

 

First script to store your credentials

 

param(
    [Parameter(Mandatory=$true)]
    [string]$Path,
    [Parameter(Mandatory=$true)]
    [string]$password
    )

    # Convert the password to a secure string
    $SecurePassword = $password | ConvertTo-SecureString -AsPlainText -Force
    
    # Store the credential in the path
    $SecurePassword | ConvertFrom-SecureString | Out-File $Path
    
    # Write What we did
    Write-Host "Wrote password to $path"    
    
<# 
.SYNOPSIS
Stores a password in a file on the local computer for retrevial by scripts.

.DESCRIPTION
Used for securely storing a password on a machine for use with automated scripts.

Takes a password and encrypts it using the local account, then stores that password in a file you specify.
Only the account that creates the output file can decrypt the password stored in the file.

.PARAMETER Path
Path and file name for the password file that will be created.

.PARAMETER Password
Plain text version of password.

.OUTPUTS
File Specified in Path variable will contain an encrypted version of the password.

.EXAMPLE
.\New-StoredCredential.ps1 -Path c:\scripts\O365Account.txt -Password "Password123"

Puts the encrypted version of Password123 into the c:\scripts\O365Account.txt file
#>

Second script to pull data from the audit log (you need to define your own variables in some places as I have removed mine from this copy)

 

# Set constants
$Username = "[YOUR ACCOUNT, RECOMMENDED TO USE THE PRINCIPLE OF LEAST PRIVILEGE]"
$PasswordPath = "[YOUR PATH DEFINED IN SCRIPT ABOVE]"

# Read the password from the file and convert to SecureString
Write-Host "Getting password from $passwordpath"
$SecurePassword = Get-Content $PasswordPath | ConvertTo-SecureString

# Build a Credential Object from the password file and the $username constant
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword

# Open a session to Exchange Online were the audit log cmdlet is located
$O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
Import-PSsession $O365session

# Define dates for search. MSFT claims Windows short date format on the machine running the script should be used, but test proved "MM/dd/yyyy" must be used
$StartDate = (get-date).AddDays(-3).ToString("MM/dd/yyyy")
$EndDate = (get-date).AddDays(-2).ToString("MM/dd/yyyy")

# Define file name and path for csv export
$FileAppend = (get-date).AddDays(-3).ToString("yyyyMMdd")
# [string]$FileAppend = (Get-Date -Format yyyyMMdd)
$OutputFile = "[DEFINE OUTPUT FILE PATH]" + $FileAppend + ".csv"

# Set variable value to trigger loop below (can be anything)
$AuditOutput = 1

# Loop will run until $AuditOutput returns null which equals that no more event objects exists from the specified date
while ($AuditOutput) {

    # Search the defined date(s), SessionId + SessionCommand in combination with the loop will return and append 100 object per iteration until all objects are returned (minimum limit is 50k objects)
    $AuditOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -SessionId "1" -SessionCommand ReturnLargeSet

    # Select and expand the nested object (AuditData) as it holds relevant reporting data. Convert output format from default JSON to enable export to csv
    $ConvertedOutput = $AuditOutput | Select-Object -ExpandProperty AuditData | ConvertFrom-Json

    # Export results exluding type information. Append rather than overwrite if the file exist in destination folder
    $ConvertedOutput | Select-Object CreationTime,UserId,Operation,Workload,ObjectID,SiteUrl,SourceFileName,ClientIP,UserAgent  | Export-Csv $OutputFile -NoTypeInformation -Append
}

# Close the sesssion to Exhange Online
Remove-PSSession $O365Session

I hope this helps to get you started. You can read more about the audit log and how you can limit it's scope if needed here.

 

Highlighted

One thing that strikes me about searching the audit log with PowerShell is that unless you are very careful with the search criteria, you're going to see a heap of data that you might not want to process. There are now so many workloads providing audit records to the data mart that you might be flooded with records you don't expect... So make sure that your search parameters are tuned to request data for the operations you want to examine in the period you need to see...

Highlighted

@Tony Redmond fully agree, especially if you have a large tenant / many users. I set my search date interval to 24 hours and have about 200 licensed users, and I am getting around 3000-4000 rows per day (or about 1.5 mb data). However, a majority of the records are associated with a services account crawling the tenant for daily backup. Additionally, we are only using Office 365 for SharePoint, so no Exchange, OneDrive or other service that would push the number of returned records to a whole new level.

Highlighted

Given what I see for Exchange Online events, if you added them into the mix and add some compliance like Office 365 retention policies, you might double the data you gather daily...

Highlighted

My needs are specifc to SharePoint online. Using the GUI, I am able to get below 5 different reports and I am working on figuring out the correct PowerShell parameters to use and combine all 5 using the append option to get EVERYTHING that happened on a SINGLE site collection into one report.

 

File and page activities
Folder activities
Sharing and access request activities
Synchronization activities
Site administration activities

Highlighted

@Rajiv Chokshi refering to the Technet article that I linked in the end of my answer; You can use the "Operation" parameter to define the specific activities that you are looking for, but I would probably recommend you try querying all of them first and then review and select the ones you care about. 

 

Using the "RecordType" parameter, I see that you can also filter the search to for example include things like:

  • SharePointFileOperation
  • SharePointSharingOperation

The same with the site collection filter. I do not know directly which parameter that works best for this. As you can see you have one called "SiteIds" which could work, but I'm not sure that the ID stays the same for all sites or sub sites of a site collection. Maybe it would be better to use "ObjectIds" which is queried as string and returns the object as a URL. So defining the site collection url + a wildcard characters should hopefully work. Something like:

 

 

SiteIds https://contoso.sharepoint.com/sites/sitecollectionname*

 

Hope this helps!

 

 

Highlighted

Thanks, I will try again using your tips. Appreciate your help.

Highlighted
The SiteIds parameter doesn't work, i tried passing in WebId, Site but i get error

The Site Id search is not yet supported.
+ CategoryInfo : InvalidArgument: (:) [Search-UnifiedAuditLog], NotSupportedException
+ FullyQualifiedErrorId : [Server=VI1P191MB0240,RequestId=800fa919-711b-47ee-9915-699b8684a804,TimeStamp=28/12/2017 11:30:37] [FailureCategory=Cmdlet-NotSupportedException] 518C5EC4,Microsoft.Exchange.Management.SystemConfigurationTasks.Searc
hUnifiedAuditLog
+ PSComputerName : outlook.office365.com
Highlighted

I believe the SiteIds parameter does work, you just need to use the Site GUID (which you can obtain from the Audit Log Search in the Security and Compliance Center).

Highlighted

SiteIds absolutely does work, but as you indicate, the problem is to find the GUID for the site. If you have an audit record for an action performed in the site, you can find it in the "more information" details for the record. You can then do something like:

 

PS C:\> Search-UnifiedAuditLog -SiteIds acfe74d8-edfb-436d-924b-e018666605ee -StartDate 1-dec-2017 -EndDate 18-apr-2018 | ft creat
iondate, userids, operations