Skip to content

Instantly share code, notes, and snippets.

@Techlogist
Last active October 3, 2021 12:30
Show Gist options
  • Save Techlogist/1ea43196b14d4b86d882f422dbb3db24 to your computer and use it in GitHub Desktop.
Save Techlogist/1ea43196b14d4b86d882f422dbb3db24 to your computer and use it in GitHub Desktop.
Uploading files to a FTP with Powershell
# License: GNU General Public License v2.0
# Author: Miguel
# Website: www.techlogist.net
# Post: https://techlogist.net/powershell/uploading-files-to-a-ftp-with-powershell/
# Description: Uploading files to a FTP with Powershell
# OS/Language/Region: Windows/EN-US
# Example of powershell report output to a report in HTML and upload to FTP
# Set the output and FTP file folder
$path = "C:\Dir"
# Credentials for FTP server
$credentials = Get-Credential
# Set the ports which will not be checked
$NARemotePort="0"
$NARemoteAddress="::","127.0.0.1"
$Array=""
# Optain the properties to process
ForEach ($OutIPPort in (Get-NetTCPConnection | Sort-Object -Property RemotePort)){
if ($OutIPPort.RemotePort -in $NARemotePort -or $OutIPPort.RemoteAddress -in $NARemoteAddress) { }
else{
$IPName=Get-Process -id $OutIPPort.OwningProcess | Select-Object Name;
$REMP=Resolve-DnsName $OutIPPort.RemoteAddress -ErrorAction SilentlyContinue | Select-Object NameHost
if($REMP.NameHost -eq $null) {$NameHost= "No record"} else {$NameHost=$REMP.NameHost}
# Define the output variables
$Detail1=$IPName.Name
$Detail2=$OutIPPort.OwningProcess
$Detail3=$OutIPPort.RemoteAddress
$Detail4=$NameHost
$Detail5=$OutIPPort.RemotePort
# Create the table code
$Collected="<tr><td>$Detail1</td><td>$Detail2</td><td>$Detail3</td><td>$Detail4</td><td>$Detail5</td></tr>"
$Array += $Collected
}
}
# Create the HTML body
$report="<table>
<tr><th>Local Process</th><th>Local Port</th><th>Remote IP</th><th>Remote Name</th><th>Remote Port</th></tr>
$Array
</table>"
# Define the CSS style
$style="<style>
table {
border: 1px solid black;
border-collapse: collapse;
}
th{
color:white;
background:black;
}
td {
text-align:center;
vertical-align: center;
border: 1px solid black;
}
</style>"
# Convert and output the file
convertto-html -head $style -body $report | Out-File -FilePath "$path\index.html" -Encoding utf8
# Export to FTP
# FTP server login details
$server = "ftp://server-url.net/directory/"
# Upload the file
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($credentials.UserName,$credentials.Password)
foreach($item in (Get-ChildItem $path)){
write-host "Uploading $item..."
$uri = New-Object System.Uri($ftp+$item.Name)
$webclient.UploadFile($uri, $item.FullName)
write-host "Uploaded $item!"
}
# FTP server login details
$server = "ftp://ftp.serverUrlHere.net/directory/"
# Credentials
$credentials = Get-Credential
# Path to file to be uploaded
$file = "C:\pathToFile"
#Upload the file
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($credentials.UserName,$credentials.Password)
foreach ($item in (Get-ChildItem $file)) {
write-host "Uploading $item..."
$uri = New-Object System.Uri($ftp+$item.Name)
$webclient.UploadFile($uri, $item.FullName)
write-host "Uploaded $item!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment