Powershell FTPWebRequest and EnableSsl = True - powershell

I searched this site and found an FTPWebRequest example via Powershell. I put it to use and it works fine. However, when I enable SSL via EnableSsl=$True, all I get is timeouts or a delayed "227 Entering Passive Mode", which breaks the process. As soon as I disable EnableSsl, I can fly right through. Can someone point me in the right direction? SSL is supported on the FTP host.
I'd eventually like change the method to DownloadFile and loop the code to download files, after I get the list and find matches. I'd like to do it securely, though.
# Create an FTPWebRequest object to handle the connection to the FTP server
$ftprequest = [System.Net.FtpWebRequest]::Create($sourceuri)
# Set the request's network credentials for an authenticated connection
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)
# Set FTPWebRequest method to ListDirectory
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
$ftprequest.EnableSsl = $True
$ftprequest.UseBinary = $False
$ftprequest.UsePassive = $True
$ftprequest.KeepAlive = $False
$ftpresponse = $ftprequest.GetResponse()
Write-Out $ftpresponse.StatusCode
Write-Out $ftpresponse.StatusDescription

Come to find out, spontaneous issue was server-side.

Related

PowerShell - Connect and disconnect from SPO

The script connects to SPO and read from excel. At the end I close the local excel instance and disconnect from SPO. Usually I am running the script so this is really necessary to do it every run? connect ad disconnect. Maybe there is another way to do it? ask if there is a valid active connection?
I saw that if the credentials are wrong for example the script is still reading from the sheet maybe from the memory, how can I protect from this scenario?
#Connecting to SharePoint Online site
Write-host "Connecting to SharePoint Online"
Connect-PnPOnline -Url $SharePointSiteURL # -Credentials $PSCredentials
$ExcelObject = New-Object -ComObject Excel.Application
$ExcelWorkBook = $ExcelObject.Workbooks.Open($SharePointSiteURL)
$ExcelWorkSheet = $ExcelWorkBook.Sheets.Item("VIP List")
function QuitExcel {
# when done, quit Excel and remove the used COM objects from memory (important)
$ExcelWorkBook.Close()
$ExcelObject.Quit()
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelWorkSheet)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelWorkBook)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelObject)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
Disconnect-PnPOnline

Powershell script to download file from FTPS server

I have seen mixed posts but nothing definitive and recent. I am running Filezilla Server FTPS Explicit TLS. I need a Powershell script that when run will securely connect to my server across the internet and download a specific file. Does the below code looks good minus the TLS, and can someone help me finish it? I think all of the commands I would need would be from here https://winscp.net/eng/docs/library_sessionoptions#ftpsecure
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property #{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "ftp.example.com"
UserName = "user"
Password = "mypassword"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Download files
$session.GetFiles("/directory/to/download/*", "C:\target\directory\*").Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}

Download all files in FTP folder with Powershell [duplicate]

This question already has answers here:
PowerShell FTP download files and subfolders
(2 answers)
Closed 4 years ago.
The vendor I'm working with uploads zip files to an FTP. I need to download whatever is uploaded there and process, as needed.
Using Powershell, how do I download *.* from an FTP folder?
(In reference to https://social.technet.microsoft.com/Forums/office/en-US/744ee28a-9340-446a-b698-4b96e081b501/download-files-from-ftp-server?forum=winserverpowershell)
# Config
$Username = "user"
$Password = "password"
$LocalFile = "C:\tools\file.zip"
$RemoteFile = "ftp://myftpserver:22/Folder1/Folder/file.csv"
# 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
$ftpRequest.EnableSsl = $true
# 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)
Is there any way to make $RemoteFile something like ftp://myftpserver:22/Folder1/Folder/*.zip or ftp://myftpserver:22/Folder1/Folder/*.*
My apologies if there is a post the same. I saw some similar, but not close enough to answer the question.
I have created PowerShell SFTP, FTP, and FTPS script. You will have to download it from my github. I do not use any third party applications, becuase I do not trust them. What I use is REBEX.dll for the SFTP section and .Net WebRequest for FTP and FTPS.
https://github.com/FallenHoot/ZOTECH-PS/blob/master/SFTP.zip
If you have issues understanding the code please let me know. If you are not going to use the Get-SFTP function just comment it out.

Powershell Web with Proxy

I am getting a proxy error with my script. "The remote server returned an error: (407) Proxy Authentication Required."
IE uses a script to configure it's connection (that's all the info I have). I looked in there and tried some IPs/ports in my script, but none seemed to work. Is there some other way of using this IE connection script in PS?
$web_client = New-Object System.Net.WebClient;
$web_client.Headers.Add("user-agent", "PowerWeb");
$proxyAddr = "http://172.16.192.34:9090"
$proxy = new-object System.Net.WebProxy
$proxy.useDefaultCredentials = $true
$proxy.Address = $proxyAddr
$web_client.proxy = $proxy
$data = $web_client.DownloadData("http://stackoverflow.com")
If you are using PowerShell 3.0 you can use Invoke-WebRequest

Is it possible to use powershell to logon https site (secure http)

I have previously automated login process on a website before which uses http however the same code is not working for another website which is using https (secure protocol). so i am not sure whether i have to do some extra work to logon that website using powershell.
EDIT: Adding code from comment
$ie = New-Object -ComObject "internetExplorer.Application"
$ie.Visible = $true
$ie.Navigate("secure.websitename.com/xyz/login.aspx";)
while ($ie.Busy -eq $true){Start-Sleep -Milliseconds 1000;}
Write-Host -ForegroundColor Magenta "Attempting to login";
$doc = $ie.Document
$LoginName = $doc.getElementsByName("txtUserName")
$LoginName.value = "username"
$txtPassword = $doc.getElementsByName("txtUserPass")
$txtPassword = "password"
$btnLogin = $doc.getElementsByName("cmdLogin")
You will need to include the "https" in the $ie.Navigate. I also believe if you pipe $ie> to Get-Member there is a Credentials property that you can use to pass the username and password. I don't have PowerShell in front of me right now but have played with this before with a https site.