<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Splunk on Ash Tewari</title><link>https://www.tewari.info/tag/splunk/</link><description>Recent content in Splunk on Ash Tewari</description><generator>Hugo -- gohugo.io</generator><language>en-US</language><lastBuildDate>Tue, 23 Feb 2016 03:20:14 +0000</lastBuildDate><atom:link href="https://www.tewari.info/tag/splunk/feed.xml" rel="self" type="application/rss+xml"/><item><title>Using Powershell with Splunk</title><link>https://www.tewari.info/2016/02/22/using-powershell-with-splunk/</link><pubDate>Tue, 23 Feb 2016 03:20:14 +0000</pubDate><guid>https://www.tewari.info/2016/02/22/using-powershell-with-splunk/</guid><description>&lt;p&gt;&lt;a class="link" href="http://dev.splunk.com/view/splunk-powershell-resource-kit/SP-CAAADRU" title="Splunk Powershell Resource Kit"&#10; target="_blank" rel="noopener"&#10; &gt;Splunk Powershell Resource Kit&lt;/a&gt; is a convenient and very capable wrapper over &lt;a class="link" href="http://www.splunk.com/" target="_blank" rel="noopener"&#10; &gt;Splunk&lt;/a&gt; &lt;a class="link" href="http://dev.splunk.com/restapi" target="_blank" rel="noopener"&#10; &gt;REST API&lt;/a&gt;. You can use the &lt;a class="link" href="https://msdn.microsoft.com/en-us/powershell" target="_blank" rel="noopener"&#10; &gt;Powershell&lt;/a&gt; commandlets exposed by this resource kit to deploy, check and manage splunk services as well as execute splunk searches. In this post, you will be introduced to the Splunk Powershell Resource Kit, you will learn how to use powershell commandlets to connect to a splunk instance and execute searches.&lt;/p&gt;&#10;&lt;p&gt;1. First, you will need to download the resource kit from &lt;a class="link" href="https://github.com/splunk/splunk-reskit-powershell" target="_blank" rel="noopener"&#10; &gt;github&lt;/a&gt;.&#10;2. Installation is very simple. All you have to do is download and extract the files from the zip archive and double click on install.bat to install the splunk powershell module.&#10;3. Open Windows Powershell console from Windows Start menu.&#10;4. Verify that the Splunk module is installed by executing Get-Module commandlet.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Get-Module SplunkSearch&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;5. Import splunk resource kit commandlets using Import-Module command.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Import-Module -Name Splunk&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;6. Next, you need to use Get-Credentials and then Connect-Splunk commandlets to connect to splunk. You need to do this once per session or if you need to switch to a different splunk instance.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$credential = Get-Credential&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Connect-Splunk -Credential $credential -ComputerName localhost&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I have a local Splunk Enterprise instance running on my machine, so I am using localhost as the ComputerName to connect to it. If you have a SplunkCloud subscription you can use YourSubscriptionId.splunkcloud.com as the ComputerName to connect to your subscription. Like so -&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Connect-Splunk -Credential $credential -ComputerName MySubscription.splunkcloud.com&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;7. Next, use Search-Splunk commandlet to execute searches -&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Search-Splunk -Search &amp;#34;Error&amp;#34;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here is a sample script:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$lastDay = ( get-date ).addDays( -1 ).toString( ‘s’ )&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$searches = @(&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;#34;ERROR&amp;#34;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; , &amp;#34;source=&amp;#34;&amp;#34;tutorialdata.zip:*&amp;#34;&amp;#34; ERROR&amp;#34;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; , &amp;#34;CreditDoesNotMatch&amp;#34;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ,&amp;#34;source=&amp;#34;&amp;#34;tutorialdata.zip:.\\www3/access.log&amp;#34;&amp;#34; productId=WC-SH-G04&amp;#34;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Write-Output $lastDay&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;foreach($search in $searches)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; $qry = $search + &amp;#34; | stats count&amp;#34;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Write-Output $qry&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Search-Splunk -Search $qry -StartTime $lastDay | Select-Object -ExpandProperty Count&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The sample script executes multiple Splunk searches and outputs the count of results matching these search queries. Note that I am &amp;ldquo;-StartTime&amp;rdquo; parameter to scope the search to a narrower time window and &amp;ldquo;stats count&amp;rdquo; command to get the count of results. You can get this sample script as a &lt;a class="link" href="https://gist.github.com/ashtewari/c29a8ac98587d42c91bd" target="_blank" rel="noopener"&#10; &gt;github gist&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;You can also use -EndTime and –MaxReturnCount to further constrain the query results and Format-List, Format-Table, Format-Wide commands to format the results. You can learn more about other Search parameters as well as other capabilities exposed in the resource kit &lt;a class="link" href="https://github.com/splunk/splunk-reskit-powershell/raw/master/Docs/Splunk%20PowerShell%20Resource%20Kit.docx" target="_blank" rel="noopener"&#10; &gt;documentation&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;Resources :&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;a class="link" href="http://dev.splunk.com/view/splunk-powershell-resource-kit/SP-CAAADRU" target="_blank" rel="noopener"&#10; &gt;Splunk Powershell ResourceKit&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a class="link" href="https://github.com/splunk/splunk-reskit-powershell" target="_blank" rel="noopener"&#10; &gt;Splunk Powershell ResourceKit on github&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a class="link" href="https://github.com/splunk/splunk-reskit-powershell/raw/master/Docs/Splunk%20PowerShell%20Resource%20Kit.docx" target="_blank" rel="noopener"&#10; &gt;Splunk Powershell ResourceKit Documentation&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Image credit : &lt;a class="link" href="https://thenounproject.com/search/?q=shell&amp;amp;i=209178" target="_blank" rel="noopener"&#10; &gt;terminal by Andrea Mazzini&lt;/a&gt; from the Noun Project&lt;/p&gt;&#10;</description></item></channel></rss>