Microsoft Power Automate Desktop to Download Authenticated Files from the Web

I enjoy a good python script, and at one point I had a fully automated authentication and download for a particular datafeed file that I used on a regular basis. At some point, the protection on that site got in the way of my python script and after much trial and error, I gave up and it became a manual process for me again. I have tried automation programs in the past to get around this issue, but they always came up short.

That is until Microsoft Power Automate Desktop arrived. PAD emulates a user, so it can essentially bypass browser bot checks, allowing me to once again automate my datafeed retrieval.

The Goal

In this example, I'll walk you through creating an automation to:

  1. Open a browser
  2. Go to a website
  3. Login with username/password
  4. Download a file
  5. Run a script against that file

You'll need a Windows 10 machine to do this.

First, download and install Microsoft Power Automate Desktop

Follow the steps to install the program and get to the screen to create your first flow.

You will also need the browser extension. I chose Microsoft Edge for the extension because it's the browser I use the least and I figured it would work best, being a Microsoft product and all.

Click "New flow", add a Flow name and click Create. This will drop you into the screen to create the automation.

image.png

There are two ways to accomplish our 5 step goal. The first way is what I call the 'dirty' way. Click on "Desktop Recorder" and carefully follow every step until you have completed the task, then click stop. The problem is, very rarely do things behave exactly the same every time, so this is not a reliable solution.

Here is what a portion of the output of a "Desktop Recorder" session looks likes to achieve our 5 step process. There are actually 28 steps total:

image.png

Every action you take, including unintentional actions, gets recorded. Now it's just as easy to go in and remove any action you want to ignore, such as moving a window so you can see the contents. Just click the action and click or press Delete.

image.png

So it is accurate as far as actions go, but hardly efficient. Compare this script with one that is properly developed:

image.png

We've reduced the number of steps from 28 to 8 and achieved the same thing. Let me break down each step.

Step 1) Open Edge and navigate to a website. This step combines several of the recorder steps into one. By specifying which browser to open and where to go, we've basically compacted the "complex" task of finding the web browser, opening it, clicking the address bar, typing in the address, and pressing enter. You'll start to see why the desktop recorder is not very efficient.

Step 2-4) Populate the username field, populate the password field and click the "Login" button. The way to do this can get a bit tricky, but with the "Populate text Field on a web page" action, you can piggy-back off of the browser session created in Step 1 and select the UI elements needed to complete the form.

Step 5) Create a new tab. In this case, there are a few ways to direct this. I could have opted to navigate through the site to get to the file I need, but a more direct approach is to borrow the session I've already established and open a direct link to the file I need using a new browser tab. This way I don't have to worry about navigation, and as long as that file URL doesn't change, the script will work fine. There is an action to "download from web", but I couldn't figure out how to tie that back to my current browser session, which I needed to do for authentication.

Step 6) Wait 6 seconds This number is arbitrary, but I want the file every chance it needs to complete the download.

Step 7) Close the browser. You don't want to just leave that running.

Step 8) Run PowerShell script This was easy enough. You can call an existing script, or, in my case, simply copy the contents of a script to the Action itself. That way my automation isn't dependent on an external file.

True Automation

Now, how does one run these unattended? Unfortunately, I have not found that out yet. There is an option for an enterprise enabled gateway that can access a remote Windows server to execute the commands, however there are some stipulations:

  • UI flow creates, manages, and then releases the Windows user session on the target devices.
  • Unattended UI flows run on devices with the screen locked so that no one can see the flow while it runs.
  • Windows 10 devices cannot run unattended if there are any active Windows user sessions present (even a locked one). You will receive an error
  • On Windows Server, if you have a locked Windows user session open with the same user as the UI flow is supposed to run as, you will receive an error

That's a lot of things that are potentially unavoidable, unless you setup your own system specifically for automation.

I'm going to run a VM using Vagrant / VirtualBox to see if this can be done.

I launched a VM using this image

The Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "Microsoft/EdgeOnWindows10"
  config.vm.box_version = "1.0"
end

I will update when I have an answer.

-AJ

No Comments Yet