PowerShell Archives - Chinh Do https://www.chinhdo.com/category/programming/powershell/ Chinh's semi-random thoughts on software development, gadgets, and other things. Fri, 28 Oct 2022 19:14:18 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.1 Monitor & Visualize Your SmartThings Smart Home with Splunk https://www.chinhdo.com/20200330/monitor-visualize-your-smart-home-with-splunk/ https://www.chinhdo.com/20200330/monitor-visualize-your-smart-home-with-splunk/#view_comments Tue, 31 Mar 2020 00:54:00 +0000 https://www.chinhdo.com/?p=701 The smart home has gone through quite a convergence in the last few years. Modern protocols like Z-Wave & ZigBee, along with mart hubs, and smart assistants like Amazon Alexa, Google Home & Apple Siri are finally bringing everything together to make the smart home a practical and reliable reality. What had been still missing…

The post Monitor & Visualize Your SmartThings Smart Home with Splunk appeared first on Chinh Do.

]]>
The smart home has gone through quite a convergence in the last few years. Modern protocols like Z-Wave & ZigBee, along with mart hubs, and smart assistants like Amazon Alexa, Google Home & Apple Siri are finally bringing everything together to make the smart home a practical and reliable reality.

What had been still missing from the picture for me, is the ability to log, analyze, and visualize all the data that my smart home generated. I use Splunk (data capture and visualization tool) at work so I decided to give it a try at home and it’s worked out great.

Here’s a Splunk dashboard I created for my home, showing current and historical data from multiple data sources: energy meter, contact sensors, switches, weather data feed, Windows event logs, and some custom PowerShell scripts.

My SmartThings-based smart home setup:

  • Samsung SmartThings Hub 2nd Gen
  • Amazon Echo Devices
  • Various ZigBee/Z-Wave devices
    • Samsung SmartThings GP-U999SJVLAAA Door & Window Multipurpose Sensors
    • Samsung SmartThings GP-U999SJVLBAA Motion Sensors
    • Samsung F-OUT-US-2 SmartThings Outlets
    • Other ZigBee/Z-Wave switches, dimmers, and plugs
    • Samsung ST-CEN-MOIS-1/FTR-US-2 Water Leak Sensors
    • Aeotec HEM G2 whole house energy monitor
    • First Alert ZCOMBO 2-in-1 Smoke Detector & Carbon Monoxide Alarm, Z-Wave
  • PowerShell scripts to pull data from openweathermap.org & run/log periodic Internet speed tests.
  • Splunk Free

Installing Splunk Free Edition

Download and install Splunk. You will start with the Enterprise version which comes with a 60-Day Trial. After that you can switch to the Free edition. Splunk Free allows indexing up to 500 MB of data per day which has been sufficient for my home logging needs. For my setup I installed Splunk on a 14-year old Windows box with a Intel Core2 Quad CPU Q6600 @2.40GHz – Splunk indexing/query performance has been pretty acceptable.

If your install was successful, you should be able to log into Splunk web by navigating to http://localhost:8000 (or replace localhost with your Splunk server hostname).

If you want to monitor other computers, install Splunk Universal Forwarder on each of those computers. I’ll go through how to configure the Universal Forwarders in a future post.

Enable the Splunk HTTP Event Collector (HEC)

The Splunk HTTP Event Collector is how we are going to send SmartThings events to Splunk. Follow the directions here to set it up. For my setup, I unchecked “Enable SSL”. Make sure you create an Authentication Token and note it down – you will need it later.

Send a test event so you can confirm that it’s working.

curl -k  https://localhost:8088/services/collector/event -H "Authorization: Splunk B5A79AAD-D822-46CC-80D1-819F80D7BFB0" -d '{"event": "hello world"}'

Install/Publish the Splunk HTTP Event Logger SmartThings SmartApp

A very cool benefit of using Samsung SmartThings is the ability to create your own SmartApps, or re-use SmartApp code written by others in the community. To send SmartThings events to Splunk, we will use SmartThingsSplunkLogger SmartApp code by Jason Hamilton/Brian Keifer:

  • Go to https://graph.api.smartthings.com and log in
  • Choose Locations and select your home location
  • Click SmartApps, then “+ New SmartApp”
  • Switch to the “From Code” tab
  • Copy the source code from here and paste into the “From Code” box in your new SmartApp.

Enable the Splunk Logger SmartApp

  • In your SmartThings mobile app (instructions here are for Android but should be similar for iOS), switch to the Automation tab.
  • Choose “+Add a SmartApp”, scroll to the bottom and select My Apps, then Splunk HTTP Event Logger.
    • 10/28/2022: The “Add a SmartApp link has been moved in the latest app. It should now be in the Automations tab > + in the upper right > add routine > Discover tab and scroll to the bottom.
  • Tap on each “Log these…” section and select all the devices you want to monitor.
  • In the Splunk Server section:
    • Select Local Server
    • Fill in your Splunk Server hostname/IP
    • Leave Use SSL unchecked
    • Leave Splunk Port as the default 8088 (or change if you want)
    • Fill in the Splunk Authentication Token from earlier.
    • For “Assign a name”, type “Splunk Logger”
    • For “Set for specific mode(s), leave everything unchecked (default)
Galaxy Note Smartthings Splunk
  • Click Save at the top right corner.

At this point, if everything is working correctly, you should start seeing some SmartThings events in Splunk. You may need to wait 15 minutes for events to start coming in.

Build Your Splunk Dashboard

Splunk is fairly easy to use/learn so you should be able to learn the basics pretty quickly. If you are new to Splunk, try going through the Splunk Search Tutorial.

To get you started here are a few of the Splunk queries I use for my dashboard panels.

For the Current Power Consumption Radial Gauge, I use “stats latest(value)” to display the latest value from the Aeotec HEM G2 whole house energy monitor. I also limit the Time Range to the last 60 minutes since I am only interested in very recent data.

sourcetype=httpevent name=power deviceId="118c2de8-3256-44a6-96bd-d1547715fc92"
| stats latest(value)

For the Power Draw History panel, I use the timechart command to show a historical chart:

sourcetype=httpevent name=power deviceId="118c2de8-3256-44a6-96bd-d1547715fc92"
| timechart p50(value)

For the “Indoor Temperature” panel, the query uses a “by” cause to chart multiple series, one from each device. All of these devices are either contact sensors or moisture sensors that also have a temperature sensor built in.

sourcetype=httpevent name=temperature 
|  timechart avg(value) by device

Weather panels such as “Outdoor Temperature”, “Outdoor Humidity”, “Wind Speed” and Rain use data from openweathermap.org’s weather REST APIs. I wrote a PowerShell script that runs hourly to pull the data and appends to a log file, which is forwarded to Splunk. I will cover this in a future blog post.

For the “Contact Sensors & Switches” panel:

sourcetype=httpevent (name=contact OR name=switch)
| eval valueInt = case(value = "closed","1", value="open","2", value = "on","2", value="off","1")
|  stats latest(valueInt) by device

“CPU Load” and “Network Bytes per Sec” panels use data from Windows Event Logs.

CPU Load:
sourcetype="Perfmon:CPU Load" | timechart avg(Value) by host
Network Bytes:
sourcetype="Perfmon:Network Interface" (counter="Bytes Sent/sec" OR counter="Bytes Received/sec") | timechart sum(Value) by host

“WAN Speed” panel shows hourly Internet speed test results from a custom PowerShell script.

Hope this helps you to get started and move your smart home to the next level. In future posts, I will write more about installing and configuring Splunk Universal Forwarders, sending other types of data to Splunk, including Windows Events Log, weather data from openweathermap.org, and custom PowerShell scripts to log other data as well as querying Splunk data and send out alerts if certain conditions are met.

Troubleshooting Tips

If SmartThings events are not showing up in Splunk try looking at SmartThings Live Logging logs. Log into SmartThings Groovy IDE then choose Live Logging menu. When a SmartThing event occurs (such as when a door contact sensor is tripped), you should see a log entry written:

See Also

The post Monitor & Visualize Your SmartThings Smart Home with Splunk appeared first on Chinh Do.

]]>
https://www.chinhdo.com/20200330/monitor-visualize-your-smart-home-with-splunk/feed/ 9
Posting a feed to Google Search Appliance (GSA) from PowerShell https://www.chinhdo.com/20141114/posting-a-feed-to-google-search-appliance-gsa-from-powershell/ https://www.chinhdo.com/20141114/posting-a-feed-to-google-search-appliance-gsa-from-powershell/#comments_reply Fri, 14 Nov 2014 12:59:40 +0000 https://www.chinhdo.com/?p=580 PowerShell code to submit a feed to Google Search Appliance: See also Feeds Protocol Developer’s Guide

The post Posting a feed to Google Search Appliance (GSA) from PowerShell appeared first on Chinh Do.

]]>
PowerShell code to submit a feed to Google Search Appliance:

$dataSource = "MyFeed" # replace this with your datasource name
$gsaUrl = "http://testsearch.example.com:19900/xmlfeed"; # replace this with your GSA URL

$xml = @"
$dataSource incremental
"@
   
Write-Output "Submitting feed to $gsaUrl."

$body = "feedtype=metadata-and-url&datasource=$dataSource&data=$xml"
$contentType = "multipart/form-data"
$length = $body.Length

Write-Output "Posting feed to GSA:"
Write-Output $xml

$r=Invoke-WebRequest -uri $gsaUrl -method POST -body $body -ContentType $contentType -UserAgent "Ps-GsaFeedSubmitter"

See also Feeds Protocol Developer’s Guide

The post Posting a feed to Google Search Appliance (GSA) from PowerShell appeared first on Chinh Do.

]]>
https://www.chinhdo.com/20141114/posting-a-feed-to-google-search-appliance-gsa-from-powershell/feed/ 0
Finding the Last Friday (or any weekday) of the Month in PowerShell https://www.chinhdo.com/20140913/finding-the-last-friday-or-any-weekday-of-the-month/ https://www.chinhdo.com/20140913/finding-the-last-friday-or-any-weekday-of-the-month/#view_comments Sat, 13 Sep 2014 04:09:01 +0000 https://www.chinhdo.com/?p=555 If there are less than 7 days left in the month, then it’s the last weekday of this type in this month. Similarly, finding the last specific weekday of the month involves finding the last weekday, and the difference between that weekday and the target weekday:

The post Finding the Last Friday (or any weekday) of the Month in PowerShell appeared first on Chinh Do.

]]>
If there are less than 7 days left in the month, then it’s the last weekday of this type in this month.

function Is-LastWeekDayOfMonth([DateTime] $d) {
    return [DateTime]::DaysInMonth($d.Year, $d.Month) - $d.Day -lt 7
}

Similarly, finding the last specific weekday of the month involves finding the last weekday, and the difference between that weekday and the target weekday:

function Get-LastFridayOfMonth([DateTime] $d) {
    $lastDay = new-object DateTime($d.Year, $d.Month, [DateTime]::DaysInMonth($d.Year, $d.Month))
    $diff = ([int] [DayOfWeek]::Friday) - ([int]
    $lastDay.DayOfWeek)
    if ($diff -ge 0) {
        return $lastDay.AddDays(- (7-$diff))
    }
    else
    {
        return $lastDay.AddDays($diff)
    }
}

The post Finding the Last Friday (or any weekday) of the Month in PowerShell appeared first on Chinh Do.

]]>
https://www.chinhdo.com/20140913/finding-the-last-friday-or-any-weekday-of-the-month/feed/ 1
Running MSDEPLOY from PowerShell https://www.chinhdo.com/20140312/running-msdeploy-from-powershell/ https://www.chinhdo.com/20140312/running-msdeploy-from-powershell/#comments_reply Wed, 12 Mar 2014 18:39:55 +0000 https://www.chinhdo.com/?p=531 I had to spend quite a bit of time figuring this out. So hopefully this will help someone out there. To launch MSDEPLOY from PowerShell, make sure you escape any quote or comma characters. The “2>&1” at the end allows Powershell to detect errors and bail out if you have $ErrorActionPreference = “Stop” If you…

The post Running MSDEPLOY from PowerShell appeared first on Chinh Do.

]]>
I had to spend quite a bit of time figuring this out. So hopefully this will help someone out there. To launch MSDEPLOY from PowerShell, make sure you escape any quote or comma characters.

The “2>&1” at the end allows Powershell to detect errors and bail out if you have $ErrorActionPreference = “Stop”

If you still have problems, download and use EchoArgs.exe to see exactly what PowerShell sees.

Example

function Get-MSWebDeployInstallPath(){
  $path = (get-childitem "HKLM:\SOFTWARE\Microsoft\IIS Extensions\MSDeploy" | Select -last 1).GetValue("InstallPath")
  $path = "${path}msdeploy.exe"

  if (!(Test-Path "$path")) {
    throw "MSDEPLOY.EXE is not installed. See http://go.microsoft.com/?linkid=9278654"
  }

  return $path
}

$msdeploy = Get-MSWebDeployInstallPath

& $msDeploy -verb:sync -source:recycleApp `
  -dest:recycleApp=`"${webSiteName}`"`,recycleMode=StopAppPool`,wmsvc=${server}`,userName="${userName}"`,password="${password}" `
  -allowuntrusted -debug 2>&1

The post Running MSDEPLOY from PowerShell appeared first on Chinh Do.

]]>
https://www.chinhdo.com/20140312/running-msdeploy-from-powershell/feed/ 0
Give The Power of Speech and Sound to Your PowerShell Scripts https://www.chinhdo.com/20100116/give-the-power-of-speech-and-sound-to-your-powershell-scripts/ https://www.chinhdo.com/20100116/give-the-power-of-speech-and-sound-to-your-powershell-scripts/#view_comments Sat, 16 Jan 2010 05:34:00 +0000 https://www.chinhdo.com/20100120/give-the-power-of-speech-and-sound-to-your-powershell-scripts/ Do you ever have the problem where you start a long running script (such as running a code build), multi-task on something else on another monitor while waiting for the script to finish, and then totally forget about the script until half an hour later? Well, here’s a solution your problem: have your script give…

The post Give The Power of Speech and Sound to Your PowerShell Scripts appeared first on Chinh Do.

]]>
Do you ever have the problem where you start a long running script (such as running a code build), multi-task on something else on another monitor while waiting for the script to finish, and then totally forget about the script until half an hour later? Well, here’s a solution your problem: have your script give you holler at you when it’s done.

Hello sir! I am done running your command!

In my library script file, I have the following functions to play sound files and to speak any text:

function PlayMp3($path) {
    # Use the default player to play. Hide the window.
    $si = new-object System.Diagnostics.ProcessStartInfo
    $si.fileName = $path
    $si.windowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
    $process = New-Object System.Diagnostics.Process
    $process.startInfo=$si
    $process.start() 
}

function PlayWav($path) {
    $sound = new-Object System.Media.SoundPlayer;
    $sound.SoundLocation="$path";
    $sound.Play();
}

function Say($msg) {
    $Voice = new-object -com SAPI.SpVoice
    $Voice.Speak($msg, 1 )
}

If you like the text-to-speech feature but find Windows’ speech engine lacking, check out Ivona. It’s a commercial text-to-speech engine but you are allow to generate and download short speech files for free personal use. Now, my script can nicely interrupt me to tell me when it’s done. Other online text-to-speech engines: vozMe, SpokenText.

If Making Noise Is Not Your Thing

If making noise is not your thing, consider displaying a message in the Notification Area. Here’s the code (courtesy Microsoft TechNet):

Build complete!
function Get-ScriptName {
    $MyInvocation.ScriptName
} 

function DisplayNotificationInfo($msg, $title, $type) {
    # $type - "info" or "error"
    if ($type -eq $null) {$type = "info"}
    if ($title -eq $null) {$title = Get-ScriptName}

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon 
    # Specify your own icon below
    $objNotifyIcon.Icon = "C:CdoScriptsFolder.ico"
    $objNotifyIcon.BalloonTipIcon = "Info"  
    $objNotifyIcon.BalloonTipTitle = $title
    $objNotifyIcon.BalloonTipText = $msg

    $objNotifyIcon.Visible = $True 
    $objNotifyIcon.ShowBalloonTip(10000)
}

The post Give The Power of Speech and Sound to Your PowerShell Scripts appeared first on Chinh Do.

]]>
https://www.chinhdo.com/20100116/give-the-power-of-speech-and-sound-to-your-powershell-scripts/feed/ 2
Automating Maven with PowerShell https://www.chinhdo.com/20100107/powershell-mvn-integration/ https://www.chinhdo.com/20100107/powershell-mvn-integration/#view_comments Fri, 08 Jan 2010 02:10:00 +0000 https://www.chinhdo.com/20100107/powershell-mvn-integration/ I found out the hard way that mvn (Maven) on Windows always return a success code of true, which means you cannot use the return code ($?) to check whether the mvn command succeeded or failed. Why they decided to break this seemingly basic program contract is a mystery. A work-around is to scan the…

The post Automating Maven with PowerShell appeared first on Chinh Do.

]]>
I found out the hard way that mvn (Maven) on Windows always return a success code of true, which means you cannot use the return code ($?) to check whether the mvn command succeeded or failed. Why they decided to break this seemingly basic program contract is a mystery. A work-around is to scan the mvn output and look for specific strings such as “BUILD SUCCESSFUL”.

Here’s how:

function InvokeAndCheckStdOut($cmd, $successString, $failString) {
    Write-Host "====> InvokeAndCheckStdOut"
    $fullCmd = "$cmd|Tee-Object -variable result" 

    Invoke-Expression $fullCmd
    $found = $false
    $success = $false
    foreach ($line in $result) {
      if ($line -match $failString) {
       $found = $true
       $success = $false
       break
      }
      else {
       if ($line -match $successString) {
        $found = $true
        $success = $true
        #"[InvokeAndCheckStdOut] FOUND MATCH: $line"
        break
       }
       else {
        #"[InvokeAndCheckStdOut] $line"
       }
      }
    }

    if (! $success) {
      PlayWav "${env:windir}\Media\ding.wav"
      throw "Mvn command failed."
    }

    Write-Host "InvokeAndCheckStdOut <===="
}

function InvokeMvn($cmd) {
    InvokeAndCheckStdOut $cmd "BUILD SUCCESSFUL" "BUILD FAILED"
}


InvokeMvn "mvn clean install"

See Also

The post Automating Maven with PowerShell appeared first on Chinh Do.

]]>
https://www.chinhdo.com/20100107/powershell-mvn-integration/feed/ 1
Tee-Object and Invoke-Expression in PowerShell https://www.chinhdo.com/20100105/powershell-invoke-expression-tee-object/ https://www.chinhdo.com/20100105/powershell-invoke-expression-tee-object/#view_comments Wed, 06 Jan 2010 02:07:00 +0000 https://www.chinhdo.com/20100105/powershell-invoke-expression-tee-object/ The PowerShell Tee-Object Cmdlet allows you to send command output to a file or a variable, and display it in the console at the same time. This is very useful for those instances where you need to parse the text output of a command. I had a hard time getting it to work with Invoke-Expression.…

The post Tee-Object and Invoke-Expression in PowerShell appeared first on Chinh Do.

]]>
The PowerShell Tee-Object Cmdlet allows you to send command output to a file or a variable, and display it in the console at the same time. This is very useful for those instances where you need to parse the text output of a command. I had a hard time getting it to work with Invoke-Expression. After trying different things, I finally found the solution. To get Tee-Object to work with Invoke-Expression in PowerShell 1, include the Tee command in the Invoke-Expression command like this:

Invoke-Expression "mvn clean install | Tee –variable result”

The following, which I guess is what most people try first, doesn’t work (at least in PowerShell V1). I guess because you are storing the result of the “Invoke-Expression” command itself into the variable instead of “mvn clean install”.

    Invoke-Expression "mvn clean install” | Tee -variable result

Wrapping Invoke-Expression in parenthesis (see below) works, but has a drawback: the output is not written to Standard Out until the whole command finishes.

    (Invoke-Expression "mvn clean install”) | Tee -variable result

The post Tee-Object and Invoke-Expression in PowerShell appeared first on Chinh Do.

]]>
https://www.chinhdo.com/20100105/powershell-invoke-expression-tee-object/feed/ 6
Create a Temporary File in PowerShell https://www.chinhdo.com/20091210/create-a-temporary-file-in-powershell/ https://www.chinhdo.com/20091210/create-a-temporary-file-in-powershell/#comments_reply Fri, 11 Dec 2009 00:52:00 +0000 https://www.chinhdo.com/20091210/create-a-temporary-file-in-powershell/ $tempFile = [IO.Path]::GetTempFileName() An empty file created immediately when you call this method. Remember to clean it up when are you done!

The post Create a Temporary File in PowerShell appeared first on Chinh Do.

]]>
$tempFile = [IO.Path]::GetTempFileName()

An empty file created immediately when you call this method. Remember to clean it up when are you done!

image

The post Create a Temporary File in PowerShell appeared first on Chinh Do.

]]>
https://www.chinhdo.com/20091210/create-a-temporary-file-in-powershell/feed/ 0
Bag of Links #2 https://www.chinhdo.com/20090212/bag-of-links-2/ https://www.chinhdo.com/20090212/bag-of-links-2/#comments_reply Fri, 13 Feb 2009 00:36:31 +0000 https://www.chinhdo.com/20090212/bag-of-links-2/ .NET/C# Programming Thread synchronization: Wait and Pulse demystified. By Nick Butler. Use a shortened GUID for file names and request strings. ShortGuid – A shorter and url friendly GUID class in C#. By Dave Transom. Top Ten Tips to Using XPath and XPointer. By John E. Simpson. Client-side caching for script methods access in ASP.NET…

The post Bag of Links #2 appeared first on Chinh Do.

]]>
.NET/C# Programming

PowerShell

Apps and Tools

Other Stuff

The post Bag of Links #2 appeared first on Chinh Do.

]]>
https://www.chinhdo.com/20090212/bag-of-links-2/feed/ 0
Bag of Links #1 https://www.chinhdo.com/20090123/bags-of-links-1/ https://www.chinhdo.com/20090123/bags-of-links-1/#view_comments Sat, 24 Jan 2009 03:52:21 +0000 https://www.chinhdo.com/?p=269 A while ago I had been posting my Finds of the Weeks series and this is the continuation of that. Instead of weekly though, this series will be more of a “whenever possible” kind of thing. General Programming Some code optimizations can actually slow down your app. Beautiful Code: False Optimizations by sernaferna. Quad core…

The post Bag of Links #1 appeared first on Chinh Do.

]]>
A while ago I had been posting my Finds of the Weeks series and this is the continuation of that. Instead of weekly though, this series will be more of a “whenever possible” kind of thing.

General Programming

.NET/C# Stuff

Database

Windows

Software, Tools, etc.

  • If you have a Linksys WRT54* router, I highly recommend loading Tomato firmware. I have been using it for about 6 months now and it’s so much better than the built-in Linksys firmware. Tomato’s QOS works great to make sure my Vonage phone line remains usable at all times.Tomato firmware

PowerShell

  • Ben Pierce posted a series of very useful PowerShell command-line demos: Demo1 (Administering Windows), Demo 2 (Administering Servers in bulk), Demo 3 (How do I Know Which Class to Use), Demo 4 (Administering Hyper-V).

Something Different

The post Bag of Links #1 appeared first on Chinh Do.

]]>
https://www.chinhdo.com/20090123/bags-of-links-1/feed/ 1