Flow of the Week: Local code execution
This Flow of the Week was written by Ashwin Sathya Raghunathan. Thanks for contributing!
———————————
Today, I want to talk about something that opens up the flow beyond the scope of connecting cloud services and being able to help running tasks on your local computer. Very often I forget to lock my computer before leaving the desk only to remember it later in a meeting, but by this time the damage would already be done, such as a prank e-mail to the team from my mailbox. So, we are going to attempt a solution to this problem using the following capabilities:
- Gateways
- File System connector
- Basic PowerShell scripting
Approach
Gateways provide a way for on-premise services to connect to the cloud. Use cases involve connecting a on-premise SQL database to a flow for use in internal systems where customers don’t want their SQL data in the cloud.
In addition, flow buttons are an elegant way to trigger a flow with a single click. (We could also integrate a hardware button from home that could do the same with a HTTP request.)
With the above said, the solution involves the following:
A PowerShell script that is running in your local computer. The script is watching a directory for a file event and locks the computer on it:
$watcher = New-Object System.IO.FileSystemWatcher $watcher.Path = "C:\Lock" $watcher.Filter = "*.*" $watcher.IncludeSubdirectories = $true $watcher.EnableRaisingEvents = $true $action = { . rundll32.exe user32.dll,LockWorkStation } ### DECIDE WHICH EVENTS SHOULD BE WATCHED Register-ObjectEvent $watcher "Created" -Action $action Register-ObjectEvent $watcher "Changed" -Action $action Register-ObjectEvent $watcher "Deleted" -Action $action Register-ObjectEvent $watcher "Renamed" -Action $action while ($true) {sleep 5}
Install the Data Gateway configure it with Flow by following the instructions.
Once the installation is complete and the Data gateway is configured, add a “File System” connection by navigating to the “Gear” icon -> Connections -> Create Connection -> File System and select the “Connect via on-premise data gateway”. You should be able to see your data gateway in the drop down list. Complete the connection creation.
The flow in itself is very simple as it just needs to create a file on the click of a button. Most of the heavy lifting is done by the PowerShell script.
Since I added a button trigger, I can use it from my Flow app on my phone to lock my computer on demand.
Now at the click of a button I can lock my workstation and no more prank e-mails. We are also automating our internal deployment with a combination of this technique and approvals to allow a smooth automated deployment workflow using Flow.
Summary
Once completing the seemingly innocent flow, we can extend this to do a variety of tasks by simply defining a protocol over the FS and the PowerShell script and automate many workflows that would require an on-premise workstation to be in picture.
As part of walking through the Flow, we learn the following capabilities from Flow:
- Data gateways
- File System connector
- Flow buttons for mobile
Go forth and automate!