Zeaun.com
  • Support
Zeaun.com
  • Support
Information Technology Mastery,

Browsing NASA’s Image of The Day Archive using PowerShell

by zupreme7 months agono comment
0
Shares
Share on FacebookShare on Twitter
image-3

Welcome back to this series on Microsoft Windows PowerShell mastery, utilizing real-world examples you can make use of today.

Assign the URL to the $zurl variable

Next, you may need to tell PowerShell to respect TLS 1.2, as it does not by default, even though ALOT of websites use this as their SSL (Secure Sockets Layer) method. Your PC or server may already have TLS 1.2 enabled, so proceed on this item as needed. If you get an error that says an SSL connection could not be established, this is likely what is required to move past it.

Note: This change applies only to the current session, and must be reset each time – so include it in your script if you do not have TLS 1.2 enabled across the board.

Enable TLS 1.2

Now we will download data from the URL provided for storage in the $z variable. If you have been following my tutorials on PowerShell lately, this part should feel quite familiar to you.

Now things get more complex. We need to do a few things here.

  1. We will assign the number One to a variable called $n.
  2. We will construct a string appending the value of $n as a numerical prefix to the given element title.
  3. We will loop through the elements within the $z variable to produce a list of titles, displayed in Yellow.
  4. We will incrementally increase the value of $n by One.

Some looping occurs now. Do not move past this point in the script until you understand it.

The titles are displayed now

Great. We have a formatted list of titles now. But what about the content? Surely we do not want to stop at just titles.

Let’s go deeper.

Ask the user which item is of interest to them

Above we have added the “Read-Host” command which allows us to capture user input within the PowerShell console for further use. In this case we will take a number and then go back to our data for next steps.

The beginning of an If statement block

Don’t be discouraged. If statements are a key way to manage the flow of your automation scripting based on the data returned and/or the input given. In this case we will proceed only if a numerical value above Zero was provided.

In the above segment, we are taking the $zchoice value and are reducing it by One. We do this because, to your computer, the numbering for entries starts at Zero, as opposed to One. If we applied the exact number shown and given by the user, we would not receive the correct article info.

Note that we do not close the If statement block out here (with a “}”) because we need a bit more logic baked within it to get the results we want.

From there we have told PowerShell what data to show and in what colors. Here we have cast the title in Yellow and the Description in Green.

Next we ask the user if they want to read more, and then we refuse input which is incorrect for our purposes here (Y vs N, indicating “Yes” vs. “No”.).

If the user submits any value not matching “Y”, “y”, “N”, or “n”, we decline to proceed – programatically.

Proceeding from the above, we then process the “Y” responses which fall within spec (“Y” or “y”).

Note: In a production script I would suggest also accepting all case-versions of the word “Yes”, which would include “yes”, “Yes”, “YEs”, “YES”, and others. – but this is just a tutorial.

Above we:

  1. launched an instance of Internet Explorer
  2. Navigated to the link which corresponds to the article number chosen
  3. Made IE visible to the end-user

In the above script segment we wrap up our automation script by giving a fond farewell to the user who decides NOT to visit the link associated with their chosen article.

Now, we simply close our our script block with a “}” and the script should run as described.

Note: I chose #55 in the list

And entering a “Y” in the above Read-host prompt initiates my visit to this page, in Internet Explorer.

Imagine what else you could automate using this method….

Now, go forth and use your newfound Automation Powers for Good instead of Evil.

Note: If you found this article, and/or others of mine, valuable – donate here: https://cash.app/$Zeaun

Previous

Getting Latitude and Longitude for a given Zip Code with PowerShell

7 months ago
Next

Tutorial: Public IP Address Identification with PowerShell

7 months ago

Leave a Reply Cancel reply

Related posts

pop1
Information Technology Mastery,

PowerShell Tutorial: Programmatically Retrieving the Population of each US State

by zupreme7 months agono comment
coin10
Information Technology Mastery,

Tutorial: Getting the current price of Bitcoin using Powershell (USD, EUR, & GBP)

by zupreme7 months agono comment
image-12
Information Technology Mastery,

Tutorial: Public IP Address Identification with PowerShell

by zupreme7 months agono comment
ps9
Information Technology Mastery,

Getting Latitude and Longitude for a given Zip Code with PowerShell

by zupreme7 months agono comment

© 2020 Zeaun Zarrieff. All rights reserved.