Written by 3:26 pm Microsoft, PowerShell • 4 Comments

Powershell: Send Tweet

Powershell Header

For some reason your Powershell script should send a Tweet. With this function Powershell will send a message to Twitter.

function Send-Tweet($Message)
{
#Twitter Login
$Twitter_UserName = "yourname"
$Twitter_Password = "yourpassword"
$Twitter_URL = "https://twitter.com/statuses/update.xml?status="

$Twitter_Request = [System.Net.WebRequest]::Create($Twitter_URL + $Message)
$Twitter_Request.credentials= New-Object System.Net.NetworkCredential($Twitter_UserName,$Twitter_Password)
$Twitter_Request.method= "POST"
$Twitter_Request.contentType = "application/x-www-form-urlencoded"
$Twitter_Request.GetResponse().statusCode # return the status code of the request
}

You can now call this function and add a message

Send-Tweet "This is my first Tweet from Powershell"

Twitter

If you need a interactive script you can simply use this (seen by Bhargav’s IT Playground)

function Send-Tweet($Message,$UserName){
if($Message-eq$null){$Message=Read-Host"Enter your tweet"}
if($Username-eq$null){$Username=read-host"Enter your twitter username"}
if($Password-eq$null)
{
$Password=read-host-assecurestring"Enter your twitter password"
$marshal=[Runtime.InteropServices.Marshal]
$Password=$marshal::PtrToStringAuto($marshal::SecureStringToBSTR($Password))
}
$url="https://twitter.com/statuses/update.xml?status=$Message"
$request=[System.Net.WebRequest]::Create($url)
$request.credentials=New-Object System.Net.NetworkCredential($UserName,$Password)
$request.method="POST"
$request.contentType ="application/x-www-form-urlencoded"
$request.GetResponse().statusCode # return the status code of the request}

Tags: , , , , , , , , , Last modified: September 2, 2019
Close Search Window
Close