Tweet from PowerShell… Customized for a TS

Ok.. another one that’s been in my drafts for the past 3 months, Seems to work fine, however if you run it in a TS over and over and over again because you’re testing constantly, it seems to skip some, like Twitter blocks duplicate Tweets. Anyway, here ya go.  If you have any improvements, please get ahold of me.

—–

Hey Folks, so I am at MMS right now, and I saw a lot of demos were people were tweeting from at Task Sequence using Orchestrator. I was like, um.. that seems like a lot of work to just tweet, I don’t want to support another server, hopefully it’s actually doing more than just tweeting. I set this up a while back for fun, and forgot to blog it, so here it goes. The hardest part… was already done by Adam! I stole his work, and built on top of it. You’ve probably noticed that is a theme on my blog, I like to borrow the hard work of other and twist it for my own purposes, of course giving credit where credit is due. I hope you are doing the same to my blog, and please give me a shout out if you take something I’ve done and add to it, and then blog it for everyone else to use!
So I started here, Adam’s  Blog.  This provided the connections to Twitter.  I created a new account specifically for my blog (@garytownblog) and set it up in his script, which he explains in great detail.  So Part 1 … Go there and do that..

Part 2… the half that makes it work nicely in a Task  Sequence.

#Used this Blog Post to get PS Tweet Module: https://www.adamtheautomator.com/twitter-module-powershell/
# - Create the .MyTwitter.psm1 based on that blog poast and place the file in same folder as this Script to run in your TS
#CMTraceLogging from Ryan  Ephgrave: https://www.ephingadmin.com/powershell-cmtrace-log-function/
#This Script just imports that module, then grabs basic info to Tweet
#Used at the end of TS to Tweet completion. 
#@gwblok - https://garytown.com
#usage: TSSendTweet.ps1 -Users'@gwblok, @garytownblog' -HashTags '#ThisRocks'
#usage: TSSendTweet.ps1 -TestMode   (This will spit out diagnostic info only to Twitter (Computer Name, IPU or OSD, Model, RunTime, Addtional CommandLine)

[CmdletBinding()]
Param(
    [Parameter(Mandatory=$false,Position=1,HelpMessage="Users")]
    [ValidateNotNullOrEmpty()]
    [string]$Users,

    [Parameter(Mandatory=$false,Position=1,HelpMessage="HashTags")]
    [ValidateNotNullOrEmpty()]
    [string]$HashTags,

    [Parameter(Mandatory=$false,Position=1,HelpMessage="Tweet Message Content")]
    [ValidateNotNullOrEmpty()]
    [string]$TweetContent
    ,

    [Parameter(Mandatory=$false,Position=1,HelpMessage="Tweet Message Content")]
    [ValidateNotNullOrEmpty()]
    [ValidateSet("Yes","No")]
    [string]$TestMode 

)

Import-Module .\MyTwitter.psm1

#Connect to Task Sequence Environment
$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$SetOSDInfoType = $tsenv.Value("SetOSDInfoType")
$TSname = $tsenv.Value("_SMSTSPackageName") 
$OSBuild = $tsenv.Value("SMSTS_BUILD") 
$LogFilePath = $tsenv.Value("_SMSTSLogPath")
$LogFile = "$LogFilePath\TSTweet.log"
$model = (Get-CimInstance -ClassName Win32_ComputerSystem).Model
$OSDSetupAdditionalUpgradeOptions = $tsenv.Value("OSDSetupAdditionalUpgradeOptions")
if ($OSDSetupAdditionalUpgradeOptions -ne "$null")
    {
    $SetupArguments = "with Arguments $OSDSetupAdditionalUpgradeOptions"
    }
else
    {
    $SetupArguments = "with Default  Arguments"
    }    

function LogCMTrace {
    Param (
		[Parameter(Mandatory=$false)]
		$Message,
 
		[Parameter(Mandatory=$false)]
		$ErrorMessage,
 
		[Parameter(Mandatory=$false)]
		$Component,
 
		[Parameter(Mandatory=$false)]
		[int]$Type,
		
		[Parameter(Mandatory=$true)]
		$LogFile
	)
<#
Type: 1 = Normal, 2 = Warning (yellow), 3 = Error (red)
#>
	$Time = Get-Date -Format "HH:mm:ss.ffffff"
	$Date = Get-Date -Format "MM-dd-yyyy"
 
	if ($ErrorMessage -ne $null) {$Type = 3}
	if ($Component -eq $null) {$Component = " "}
	if ($Type -eq $null) {$Type = 1}
 
	$LogMessage = "<![LOG[$Message $ErrorMessage" + "]LOG]!><time=`"$Time`" date=`"$Date`" component=`"$Component`" context=`"`" type=`"$Type`" thread=`"`" file=`"`">"
	$LogMessage | Out-File -Append -Encoding UTF8 -FilePath $LogFile
}

LogCMTrace -Message "TSTweet Process Started in $SetOSDInfoType Mode" -Type 1 -LogFile $LogFile
LogCMTrace -Message "TSTweet Process Started in TestMode: $TestMode" -Type 1 -LogFile $LogFile

if ($TestMode -eq "Yes")
    {
    $DifferenceUpgrade = ([datetime]$TSEnv.Value('SMSTS_FinishUpgradeTime')) - ([datetime]$TSEnv.Value('SMSTS_StartUpgradeTime')) 
    $DifferenceUpgrade = [math]::Round($DifferenceUpgrade.TotalMinutes)
   
    LogCMTrace -Message "Deployed $OSBuild $SetOSDInfoType $SetupArguments on $env:computername Model: $model - Setup Time: $DifferenceUpgrade" -Type 1 -LogFile $LogFile
    Send-Tweet -Message "Deployed $OSBuild $SetOSDInfoType $SetupArguments on $env:computername Model: $model - Setup Time: $DifferenceUpgrade"
    }

else
    {
    if ($TweetContent -ne "$null")
        {    
        LogCMTrace -Message "Message specified, using user's message" -Type 1 -LogFile $LogFile
        LogCMTrace -Message "TSTweet - $Users, $TweetContent $HashTags" -Type 1 -LogFile $LogFile
        Send-Tweet -Message "$Users, $TweetContent $HashTags"
        }

    else
        {
        LogCMTrace -Message "Null Message, using Script to determine message" -Type 1 -LogFile $LogFile
            if ($SetOSDInfoType -eq 'IPU' -or $SetOSDInfoType -eq 'CS')
                    {
        
                    [int64] $decimalreturncode = $tsenv.Value("_SMSTSOSUpgradeActionReturnCode")
                    #[int64] $hexreturncode = 0xC1900210
                    $hexreturncode = "{0:X0}" -f [int64]$decimalreturncode

                            $WinIPURet = @(
                        @{ Err = "C1900210"; Msg = 'No compatibility issues.'}
                        @{ Err = "C1900208"; Msg = 'Incompatible apps or drivers.' }
                        @{ Err = "C1900204"; Msg = 'Selected migration choice is not available.' }
                        @{ Err = "C1900200"; Msg = 'Not eligible for Windows 10.' }
                        @{ Err = "C190020E"; Msg = 'Not enough free disk space.' }
                        @{ Err = "C1900107"; Msg = 'Unsupported Operating System.' }
                        @{ Err = "0"; Msg = 'Windows Setup completed successfully.' }
                        )
                    $ErrorMsg = $winipuret | ? err -eq $hexreturncode  | % Msg
                    if ($ErrorMsg -ne  $null)
                        {
                        LogCMTrace -Message "TSTweet - Hey $Users, Deployed Windows 10 $OSBuild on $env:computername is complete with result: $ErrorMsg $HashTags" -Type 1 -LogFile $LogFile
                        Send-Tweet -Message "Hey $Users, Deploy Windows 10 $OSBuild on $env:computername is complete with result: $ErrorMsg  $HashTags"
                        }
                    else
                        {
                        LogCMTrace -Message "TSTweet - Hey $Users, $TSname deploying Windows 10 $OSBuild on $env:computername is complete.  $HashTags" -Type 1 -LogFile $LogFile
                        Send-Tweet -Message "Hey $Users, $TSname deploying Windows 10 $OSBuild on $env:computername is complete.  $HashTags"
                        }
            }
            if  ($SetOSDInfoType -eq 'OSD')
                {
                LogCMTrace -Message "TSTweet - Hey $Users, $TSname deploying Windows 10 $OSBuild on $env:computername is complete.  $HashTags" -Type 1 -LogFile $LogFile
                Send-Tweet -Message "Hey $Users, $TSname deploying Windows 10 $OSBuild on $env:computername is complete. $HashTags"
                }
        }

    }

It requires several variables, but if you use my  Task  Sequence that you can download from HERE, (MMS2018) then you can just plop it in.
Otherwise, you’ll need to make sure your TS has  (talked about more here)

“SetOSDInfoOType”, (Either IPU or  CS or  OSD)
“osbuildversion”, which will be the build number you set in your  TS.  It uses these to create a hand crafted tweet with additional info.  You can always remove these and have a generic tweet message for each time you run it, or add to it.   Pretty flexible.

Once you have your script, and you add it to your content, you can then add it to your  TS.
Run  PowerShell Script Step -> point at your script, add users you want to tag  (If you want to), and you’re set.

 

1 thought on “Tweet from PowerShell… Customized for a TS”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.