Drop Folder PowerShell

Drop folders can be used for a lot of things. Files placed in these folders can be processed by PowerShell for whatever purpose they were defined for. Sample usages of a Drop Folder:

(1) CSV file containing a list of users – Used to verify AD information
(2) Txt file containing tweets to be sent out via PowerShell and a
(3) CSV file with a list of mailboxes to migrate to another server or to Office 365

The author has used a drop folder for each of the above sample tasks in order to service a client request. Some of these will require an automated process, where others might need to be more manual. Some keys to these examples:

(a) Check for files – before proceeding, see if there are any files. No files, exit script. Files exist, move to the next step.
(b) Put in a cleanup option at the end to remove any files that are no longer needed.
(c) Log Log Log – log changes, cmdlets, etc to keep track of the progress of the script.

The below example will look for text files in a $Path directory. Then if files are found, content will be read and tweeted out. Files that are found will also be deleted. If no files are found, no tweet is sent.

Example Tweet Txt File
[sourcecode language=”powershell”]
# Twitter File Drop Path
$Path = 'C:\Twitter\TwitterDrop'

# Import twitter module
Import-Module MyTwitter

# Check for files:
$FileCheck = Get-ChildItem $Path

# If files are present, proceed
If ($Null -ne $FileCheck) {
$Date = Get-Date -Format "MM.dd.yyyy-hh.mm-tt"
$Line = "$Date,Files found – proceeding to tweet each file found." | Out-File $LogDestination -Append
$FileNames = $FileCheck.Name
Foreach ($FileName in $FileNames) {
$FullName = $Path+'\'+$FileName
$Date = Get-Date -Format "MM.dd.yyyy-hh.mm-tt"
$Line = "$Date,Processing the $FileName file to be tweeted." | Out-File $LogDestination -Append
$Content = Get-Content $FullName
Try {
# Write-Host "Send-Tweet -Message $Content -ErrorAction STOP"
Send-Tweet -Message $Content -ErrorAction STOP
$Date = Get-Date -Format "MM.dd.yyyy-hh.mm-tt"
$Line = "$Date,Success – Tweet in file – $FileName – with tweet – $Content – sent via twitter!" | Out-File $LogDestination -Append
} Catch {
$Date = Get-Date -Format "MM.dd.yyyy-hh.mm-tt"
$Line = "$Date,Failed – Tweet in file – $FileName – with tweet – $Content – failed to send via twitter!" | Out-File $LogDestination -Append
}
Try {
Del $FullName -ErrorAction STOP
$Date = Get-Date -Format "MM.dd.yyyy-hh.mm-tt"
$Line = "$Date,Success – removed the $FileName file." | Out-File $LogDestination -Append
} Catch {
$Date = Get-Date -Format "MM.dd.yyyy-hh.mm-tt"
$Line = "$Date,Failed to remove the $FileName file." | Out-File $LogDestination -Append
}
}
} Else {
$Date = Get-Date -Format "MM.dd.yyyy-hh.mm-tt"
$Line = "$Date,No files found in Drop folder to process for tweets." | Out-File $LogDestination -Append
}
[/sourcecode]
The PowerShell module I am using for the above tweeting is MyTitter by Adam Bertram and more information on the module can be found here – https://adamtheautomator.com/twitter-module-powershell/. ** The above code sample is provided as-is for you to learn from.

Related Post

CIM to WMICIM to WMI

Years ago WMI was de-emphasized for CIM. Wait. Do you know what those are? CIM – “The Common Information Model (CIM) is an extensible, object-oriented data model that contains information