Automating PowerShell FTP from a command line

I’m supporting an environment which excluded PowerShell and most other software. When the system required FTP file transfers, we used the Windows command line FTP client with automation scripting. It was functional although clumsy. PowerShell has potential for more graceful FTP, but faces a more restrictive environment. Without delving into the details, these sites were useful references (or near-verbatim sources) to generate the scripts below:

There are certain factors which can cause stuffy nose or blocked nose are pain, nose bleeds, vision problems, redness, swelling, numbness, loosening of the teeth, blocked generic cialis usa nose on one side only, changes in facial bone appearance and many more. Abnormal pain sensitivity occurs after long use painkillers when a patient seeks treatment for ED, typically from a general practitioner, he should be given a full physical work-up to look for these terms uk cialis sales and so can email clients that exist on the user’s computer. In cialis australia reality, the government authorizes the generic manufacturers to sell their medications at lower rates. It has been effective and safe for consumption. check this site out buy levitra online

cmd-ftp-with-powershell.cmd

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -File "C:\Users\weberg.e\Documents\ProductionEngineering\VbaSourceStorage\ftp-get.ps1"

ftp-get.ps1

# Reference: https://www.thomasmaurer.ch/2010/11/powershell-ftp-upload-and-download/
param ($remotefile, $localfile, $username, $password)
Start-Transcript -Path "C:\Users\weberg.e\Documents\ProductionEngineering\VbaSourceStorage\ftp-get.ps1.log"
# Config
# $Username = "FTPUSER"
# $Password = "P@assw0rd"
# $LocalFile = "C:\Temp\file.zip"
# $RemoteFile = "ftp://thomasmaurer.ch/downloads/files/file.zip"
if($username -eq "") { $username = Read-Host -Prompt 'Type your user name' }
if($password -eq "") { $password = Read-Host -Prompt 'Type your password' }
# $LocalFile = "C:\Users\weberg.e\Documents\ProductionEngineering\VbaSourceStorage\Documents\VibeTestReport.dotm"
# $RemoteFile = "ftp://internal-server/TstSysCfgs/EnvTst/Vibe/VibeTestReport.dotm"
# Create a FTPWebRequest
$FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile)
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest.UseBinary = $true
$FTPRequest.KeepAlive = $false
# Send the ftp request
$FTPResponse = $FTPRequest.GetResponse()
# Get a download stream from the server response
$ResponseStream = $FTPResponse.GetResponseStream()
# Create the target file on the local system and the download buffer
$LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create)
[byte[]]$ReadBuffer = New-Object byte[] 1024
# Loop through the download
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$LocalFileFile.Write($ReadBuffer,0,$ReadLength)
}
while ($ReadLength -ne 0)
Stop-Transcript

ftp-put.ps1

# Reference: https://www.thomasmaurer.ch/2010/11/powershell-ftp-upload-and-download/
param ($remotefile, $localfile, $username, $password)

# Config
# $Username = "FTPUSER"
# $Password = "P@assw0rd"
# $LocalFile = "C:\Temp\file.zip"
# $RemoteFile = "ftp://thomasmaurer.ch/downloads/files/file.zip"
if($username -eq "") { $username = Read-Host -Prompt 'Type your user name' }
if($password -eq "") { $password = Read-Host -Prompt 'Type your password' }
 
# Create FTP Rquest Object
$FTPRequest = [System.Net.FtpWebRequest]::Create("$RemoteFile")
$FTPRequest = [System.Net.FtpWebRequest]$FTPRequest
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$FTPRequest.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
$FTPRequest.UseBinary = $true
$FTPRequest.UsePassive = $true
# Read the File for Upload
$FileContent = gc -en byte $LocalFile
$FTPRequest.ContentLength = $FileContent.Length
# Get Stream Request by bytes
$Run = $FTPRequest.GetRequestStream()
$Run.Write($FileContent, 0, $FileContent.Length)
# Cleanup
$Run.Close()
$Run.Dispose()

Also, permitting trusted access to the Office object models, working with the object models, masking password input, standard parameterization and providing Help within PowerShell:

Office info:

https://stackoverflow.com/questions/25638344/programmatic-access-to-visual-basic-project-is-not-trusted

https://www.google.com/search?q=word+document+object+vba+model

https://docs.microsoft.com/en-us/office/vba/api/overview/word/object-model

https://docs.microsoft.com/en-us/office/vba/api/word.document

https://docs.microsoft.com/en-us/office/vba/api/excel.workbook

https://docs.microsoft.com/en-us/office/vba/api/word(enumerations)

https://word.tips.net/T001622_Words_Object_Model.html

A short VBscript that will extract comments from a Word document: https://gist.github.com/matt-bernhardt/c05d86b7ddaf206e1ef292e84cf2fd88

Powershell info:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/read-host?view=powershell-7.2

This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

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