Tweeting with PowerShell

PowerShell and Twitter. Would seem like an odd pair. However, you might find that there are some practical uses to the combination of these two tools. Some may use it for scheduled social media events, or just general announcements. It could be used as a marketing device to help broadcast information about a product as well. Twitter and PowerShell combined provides a useful tool.

Twitter API’s

In order to make the connection to Twitter from PowerShell successful, you will need a PowerShell module to connect to Twitter’s API’s. These modules will help send the correct token information in order to authenticate with Twitter and enable a connection. Once the connection is established, these modules have various potential uses.

Twitter API – Example 1

For this first example Twitter API, is one that was written some time ago by Trevor Sullivan who was a PowerShell MVP at one time and is now working with AWS. His work can be found HERE.

Twitter API – Example 2

This second example provides some slightly greater flexibility as well as the ability to preconfigure secrets and other items needed for API authentication with Twitter. This one was written by Adam Bertram and it can be found HERE. He is also a Microsoft PowerShell MVP.

Where to schedule these to run?

Depending on your needs, availability and resources there are many places where to schedule your automatic tweeting. Below are two examples of where we can set up PowerShell :

OnPremises Server

Using a server in your environment may be one of the easiest ways to automate your tweets. See the code sample below for a working example of a PowerShell script used in automating tweets. Refer to this blog post for how to schedule your tweeting PowerShell script on a server – Schedule It!

Azure Run Books

Another possible place to run these is in an Azure RunBook. There are some caveats. It does take resources and thus it will cost something to run this in the service. If you have an Azure account and have a way to pay for this, then setting up a run book in Azure is an option. Make sure to read this article on how to set this up:

Azure RunBook Setup

The code sample at the bottom can also be used for an Azure RunBook as long as the API is loaded and the appropriate keys and are prepped.

Sample Code Automating Tweets

Now, depending on which API you are using you may need to do some extra work. The code sample below use’s Adam’s Twitter
[sourcecode language=”powershell”]
# Store date for later
$Date = Get-Date -UFormat "%m/%d/%Y"
# Try to import the 'MyTwitter' PowerShell Module we need to tweet with later
Try {
Import-Module MyTwitter -ErrorAction STOP
} Catch {
Exit
}
# Array of Tweets
$TweetList = @(
'This is a sample tweet – https://www.practicalpowershell.com'
'Tweeting a message out – https://www.PowerShellGeek.com'
'All my tweets are here – https://www.twitter.com'
)
# Send Dates for the next 2 months
If ($Date -eq "7/29/2019") {$TweetId = 0} # Week 1
If ($Date -eq "8/7/2019") {$TweetId = 1} # Week 2
If ($Date -eq "8/14/2019") {$TweetId = 2} # Week 3

# Provision the tweet of the week
$Tweet = $TweetList[$TweetId]

# Publish the tweet
Try {
Send-Tweet -Message $Tweet -ErrorAction STOP
} Catch {
Write-Verbose 'Failed to send tweet!'
}
[/sourcecode]
There you have it. A quick tip on how to use PowerShell to send out your tweets.

Related Post