<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>log analytics query powershell Archives - the Sysadmin Channel</title>
	<atom:link href="https://thesysadminchannel.com/tag/log-analytics-query-powershell/feed/" rel="self" type="application/rss+xml" />
	<link>https://thesysadminchannel.com/tag/log-analytics-query-powershell/</link>
	<description>Documenting My Life as a System Administrator</description>
	<lastBuildDate>Sat, 19 Mar 2022 03:56:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
<site xmlns="com-wordpress:feed-additions:1">144174110</site>	<item>
		<title>Query Log Analytics via Powershell</title>
		<link>https://thesysadminchannel.com/query-log-analytics-via-powershell/</link>
					<comments>https://thesysadminchannel.com/query-log-analytics-via-powershell/#respond</comments>
		
		<dc:creator><![CDATA[Paul Contreras]]></dc:creator>
		<pubDate>Sat, 19 Mar 2022 03:56:37 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Invoke-AzOperationalInsightsQuery example]]></category>
		<category><![CDATA[log analytics query powershell]]></category>
		<category><![CDATA[powershell query log analytics]]></category>
		<category><![CDATA[query log analytics powershell]]></category>
		<category><![CDATA[Query Log Analytics via Powershell]]></category>
		<guid isPermaLink="false">https://thesysadminchannel.com/?p=3934</guid>

					<description><![CDATA[<p>Over the past several months, I&#8217;ve been delving more and more into Azure Log Analytics and I must say that I absolutely love it. It&#8217;s incredibly fast and seeing the results come in right away is an instant gratification. And&#8230; <a href="https://thesysadminchannel.com/query-log-analytics-via-powershell/" class="more-link">Continue Reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://thesysadminchannel.com/query-log-analytics-via-powershell/">Query Log Analytics via Powershell</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Over the past several months, I&#8217;ve been delving more and more into Azure Log Analytics and I must say that I absolutely love it.  It&#8217;s incredibly fast and seeing the results come in right away is an instant gratification.  And while this article is not going to be geared around KQL queries and how to use Log Analytics, it is going to focus on how to query Log Analytics via Powershell and the setup that&#8217;s involved with making it happen.<br />
&nbsp;</p>
<p>Allowing us to use Powershell to pull this information gives us the ability to automate and streamline events in a single pane of glass&#8230; and spoiler alert, this uses the Invoke-AzOperationalInsightsQuery cmdlet to query the workspace.</p>
<div id="tableofcontents">
<h2>Table Of Contents</h2>
<ul>
<li><a href="#loganalytics">What is Log Analytics and what language does it use?</a></li>
<li><a href="#setupprerequisites">Setup Prerequisites</a></li>
<ul>
<li><a href="#createworkspace">Create a Log Analytics Workspace</a></li>
<li><a href="#diagnosticsettings">Send logs to workspace via diagnostic settings</a></li>
<li><a href="#compilequery">Use KQL to compile a query</a></li>
</ul>
<li><a href="#powershell">How to query Log Analytics via Powershell</a></li>
<li><a href="#conclusion">Conclusion</a></li>
</ul>
</div>
<div id="loganalytics" style="scroll-margin-top: 15px;"></div>
<h2>What is Log Analytics and what language does it use?</h2>
<p>Log Analytics is Azure&#8217;s own Security Event and Incident Management (SEIM) tool and it gives administrators the ability to view log details within their tenant. There are several categories to query from such as AuditLogs, SignInLogs and RiskyUsers to name a few, and having those details on hand gives me the upper edge whenever I&#8217;m trying to figure out a problem.<br />
&nbsp;</p>
<p>Furthermore, Log Analytics uses Kusto Query Languange (KQL) in the backend to drive this functionality and it&#8217;s relatively easy to get started once you get the hang of formulating queries.  What I like the most about it, is that you can set it up using tabular expressions which makes the overall query much easier to read.<br />
&nbsp;</p>
<p>However, one important thing to note is that everything is case-sensitive so just make sure you keep that in mind if you&#8217;re not seeing the results you&#8217;re expecting to see.</p>
<div id="setupprerequisites" style="scroll-margin-top: 15px;"></div>
<h2>Setup Prerequisites</h2>
<p>In order to get started, there are several requirements and prerequisites that need to be met to have a successful outcome.  Let&#8217;s take a minute to list the requirements that are needed.<br />
&nbsp;</p>
<ul>
<li>Azure AD P1/P2 license to send to Log Analytics</li>
<li>An active subscription</li>
<ul>
<li>The user querying the data will also need read permissions to the subscription</li>
</ul>
<li>An active Log Analytics workspace</li>
<li>PowerShell Az Module (specifically Az.OperationalInsights)</li>
<li>Global Administrator or Security Administrator Azure AD roles</li>
</ul>
<p>&nbsp;</p>
<p>I&#8217;m running Azure AD P2 license in my lab and my test account, Buzz Lightyear, is granted the Security Administrator role using PIM. This account also has read access to the subscription.<br />
<a href="https://thesysadminchannel.com/wp-content/uploads/2022/03/Security-Administrator.png" target="_blank" rel="noopener"><img fetchpriority="high" decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/03/Security-Administrator.png" alt="Security Administrator" width="1006" height="468" class="aligncenter size-full wp-image-3940" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/03/Security-Administrator.png?v=1647647121 1006w, https://thesysadminchannel.com/wp-content/uploads/2022/03/Security-Administrator-768x357.png?v=1647647121 768w" sizes="(max-width: 1006px) 100vw, 1006px" /></a></p>
<div id="createworkspace" style="scroll-margin-top: 15px;"></div>
<h2>Create a Log Analytics Workspace</h2>
<p>In addition to <a href="https://docs.microsoft.com/en-us/azure/cost-management-billing/manage/create-subscription" rel="noopener" target="_blank">creating an Azure AD subscription</a>, you&#8217;ll need to create a Log Analytics workspace to be able to specify that workspace when sending the logs.<br />
&nbsp;</p>
<p>To get there, I usually search for Log Analytics workspaces in top search bar but if you want to save yourself an extra click, here is the <a href="https://portal.azure.com/#blade/HubsExtension/BrowseResource/resourceType/Microsoft.OperationalInsights%2Fworkspaces" rel="noopener" target="_blank">direct link</a>. If you haven&#8217;t created a workspace yet, be sure to click &#8220;Create&#8221; to create one.  </p>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2022/03/Create-Log-Analytics-workspaces.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/03/Create-Log-Analytics-workspaces.png" alt="Create Log Analytics workspaces" width="1138" height="378" class="aligncenter size-full wp-image-3943" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/03/Create-Log-Analytics-workspaces.png?v=1647647806 1138w, https://thesysadminchannel.com/wp-content/uploads/2022/03/Create-Log-Analytics-workspaces-1024x340.png?v=1647647806 1024w, https://thesysadminchannel.com/wp-content/uploads/2022/03/Create-Log-Analytics-workspaces-768x255.png?v=1647647806 768w" sizes="(max-width: 1138px) 100vw, 1138px" /></a><br />
&nbsp;</p>
<p>If you already have one created like I do, click on it and copy the Workspace ID.  We&#8217;ll need this later.<br />
<a href="https://thesysadminchannel.com/wp-content/uploads/2022/03/Azure-AD-Workspace-ID.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/03/Azure-AD-Workspace-ID.png" alt="Azure AD Workspace ID" width="1270" height="454" class="aligncenter size-full wp-image-3944" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/03/Azure-AD-Workspace-ID.png?v=1647648082 1270w, https://thesysadminchannel.com/wp-content/uploads/2022/03/Azure-AD-Workspace-ID-1024x366.png?v=1647648082 1024w, https://thesysadminchannel.com/wp-content/uploads/2022/03/Azure-AD-Workspace-ID-768x275.png?v=1647648082 768w" sizes="(max-width: 1270px) 100vw, 1270px" /></a></p>
<div id="diagnosticsettings" style="scroll-margin-top: 15px;"></div>
<h2>Send logs to workspace via diagnostic settings</h2>
<p>As mentioned, one of the requirements is to have a workspace created so we can send the data there. Since we already have a workspace created, let&#8217;s take the next step to ensure the logs we want to send to the workspace are enabled.<br />
&nbsp;</p>
<p>Back in the Azure Portal:</p>
<ul>
<li>Navigate to Azure Active Directory -> Diagnostic settings</li>
<ul>
<li><a href="https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/DiagnosticSettings" rel="noopener" target="_blank">Diagnostic Settings Direct Link</a></li>
</ul>
<li>Create or Edit the setting</li>
<li>Select the categories you would like to enable</li>
<li>Ensure &#8220;Send to Log Analytics workspace&#8221; is checked</li>
<li>Specify the subscription and Log Analytics workspace dropdown details accordingly</li>
<li>Click Save</li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2022/03/Configure-Azure-Diagnostic-Settings.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/03/Configure-Azure-Diagnostic-Settings.png" alt="Configure Azure Diagnostic Settings" width="1041" height="626" class="aligncenter size-full wp-image-3949" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/03/Configure-Azure-Diagnostic-Settings.png?v=1647649990 1041w, https://thesysadminchannel.com/wp-content/uploads/2022/03/Configure-Azure-Diagnostic-Settings-1024x616.png?v=1647649990 1024w, https://thesysadminchannel.com/wp-content/uploads/2022/03/Configure-Azure-Diagnostic-Settings-125x75.png?v=1647649990 125w, https://thesysadminchannel.com/wp-content/uploads/2022/03/Configure-Azure-Diagnostic-Settings-768x462.png?v=1647649990 768w" sizes="(max-width: 1041px) 100vw, 1041px" /></a><br />
&nbsp;</p>
<div id="compilequery" style="scroll-margin-top: 15px;"></div>
<h2>Use KQL to compile a query</h2>
<p>At this point, you have now successfully configured your Log Analytics to capture events from the categories that you specified.  Next is to actually use the product to retrieve data that you&#8217;re interested in.<br />
&nbsp;</p>
<p>The possibilities of exactly what you want to query are pretty much unlimited as far as I&#8217;m concerned. However, some of the most common queries I use on a regular basis are related to sign-in details, risk events and certain audit log details.<br />
&nbsp;</p>
<p>I&#8217;m going to demo a simple query to see how many times the user Buzz Lightyear has signed in over the past 7 days, but I would highly recommend you familiarize yourself with the <a href="https://docs.microsoft.com/en-us/azure/data-explorer/kql-quick-reference" rel="noopener" target="_blank">KQL Quick Reference Microsoft guide</a> for further learning.</p>
<pre class="brush: powershell; title: ; notranslate">
//Since we are only interested in sign-in logs, we'll specify SignInLogs.
//The TimeGenerated will specify how long you want to search back. 
SigninLogs
| where TimeGenerated &gt; ago(7d)
| where Identity contains &quot;Buzz Lightyear&quot; or UserPrincipalName contains &quot;buzz@thesysadminchannel.com&quot;
| distinct UserPrincipalName, UserDisplayName, ClientAppUsed, AppDisplayName, AppId
| limit 5
</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2022/03/Log-Analytics-query-example.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/03/Log-Analytics-query-example.png" alt="Log Analytics query example" width="1228" height="487" class="aligncenter size-full wp-image-3952" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/03/Log-Analytics-query-example.png?v=1647653148 1228w, https://thesysadminchannel.com/wp-content/uploads/2022/03/Log-Analytics-query-example-1024x406.png?v=1647653148 1024w, https://thesysadminchannel.com/wp-content/uploads/2022/03/Log-Analytics-query-example-768x305.png?v=1647653148 768w" sizes="(max-width: 1228px) 100vw, 1228px" /></a></p>
<div id="powershell" style="scroll-margin-top: 15px;"></div>
<h2>How to query log analytics via Powershell</h2>
<p>Whenever you want to query Log Analytics via Powershell I would always recommend testing the query in the Azure Portal first to make sure you&#8217;re not spinning your wheels if something doesn&#8217;t work the way it&#8217;s intended.<br />
&nbsp;</p>
<p>Second, since we&#8217;re going to be passing in a relatively long string, we need to make sure that our quotes are properly handled.  By that I mean if we&#8217;re using joins that require the &#8216;$&#8217; character or properties that contain quotes like the sample above, we need to make sure those characters are either escaped or properly set in the overall query (using single and double quotes accordingly).</p>
<pre class="brush: powershell; title: ; notranslate">
PS C:\&gt; $Query = 'SigninLogs
 | where TimeGenerated &gt; ago(7d)
 | where Identity contains &quot;Buzz Lightyear&quot; or UserPrincipalName contains &quot;buzz@thesysadminchannel.com&quot;
 | distinct UserPrincipalName, UserDisplayName, ClientAppUsed, AppDisplayName, AppId
 | limit 5'

PS C:\&gt; $WorkspaceId = 'b0xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

PS C:\&gt; $ResultList = Invoke-AzOperationalInsightsQuery -WorkspaceId $WorkspaceID -Query $Query -ErrorAction Stop | `
   select -ExpandProperty Results

PS C:\&gt; $ResultList | ft

UserPrincipalName           UserDisplayName ClientAppUsed AppDisplayName              AppId
-----------------           --------------- ------------- --------------              -----
buzz@thesysadminchannel.com Buzz Lightyear  Browser       OfficeHome                  4765445b-32c6-49b0-83e6-1d937652…
buzz@thesysadminchannel.com Buzz Lightyear  Browser       Office365 Shell WCSS-Client 89bee1f7-5e6e-4d8a-9f3d-ecd60125…
buzz@thesysadminchannel.com Buzz Lightyear  Browser       Azure Portal                c44b4083-3bb0-49c1-b47d-974e53cb…

PS C:\&gt;

</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2022/03/Query-Log-Analytics-via-Powershell-example.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2022/03/Query-Log-Analytics-via-Powershell-example.png" alt="Query Log Analytics via Powershell example" width="1099" height="473" class="aligncenter size-full wp-image-3957" srcset="https://thesysadminchannel.com/wp-content/uploads/2022/03/Query-Log-Analytics-via-Powershell-example.png 1099w, https://thesysadminchannel.com/wp-content/uploads/2022/03/Query-Log-Analytics-via-Powershell-example-1024x441.png 1024w, https://thesysadminchannel.com/wp-content/uploads/2022/03/Query-Log-Analytics-via-Powershell-example-768x331.png 768w" sizes="(max-width: 1099px) 100vw, 1099px" /></a></p>
<div id="conclusion" style="scroll-margin-top: 15px;"></div>
<h2>Conclusion</h2>
<p>That&#8217;s it, we now know how to query Log Analytics via Powershell. The best part is, you can use this technique to automate reports or simply use it in conjunction with other automation tools since it&#8217;s available to you through a command line interface.  </p>
<p>This is something I use in the real world and it has helped me out tremendously, but I&#8217;m curious to know how this can apply to you and your environment.</p>
<p>The post <a href="https://thesysadminchannel.com/query-log-analytics-via-powershell/">Query Log Analytics via Powershell</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thesysadminchannel.com/query-log-analytics-via-powershell/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3934</post-id>	</item>
	</channel>
</rss>
