Tag: .NET Framework

Windows Server FTP

PowerShell FTP upload and download

A friend asked me if Powershell can do simple file up and downloads. My answer was, yes of course, very easy. So this is a post with a little information about how you can do a FTP Upload or a FTP Download using Powershell. To be clear, of course you can do much more with PowerShell and FTP. But this post should show you the basic function of FTP transfers in Powershell based on the .NET Framework. With that here is how PowerShell FTP works:

FTP Upload file using PowerShell

This is how you can upload a file using FTP with PowerShell.

# Config
$Username = "FTPUSER"
$Password = "[email protected]"
$LocalFile = "C:\Temp\file.zip"
$RemoteFile = "ftp://thomasmaurer.ch/downloads/files/file.zip"
 
# 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()

FTP download file using PowerShell

And this is how you can download files from an FTP server using PowerShell.

# Config
$Username = "FTPUSER"
$Password = "[email protected]"
$LocalFile = "C:\Temp\file.zip"
$RemoteFile = "ftp://thomasmaurer.ch/downloads/files/file.zip"
 
# 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)

So let me know how FTP file transfers with PowerShell are working for you. Scenarios I have used this are simple automation tasks where we needed to upload or download some files.



Random Numbers in Powershell

Powershell Header

In one of my last scripts I needed random numbers in Powershell. Now this Blog post should show you how you can generate random numbers in Powershell very simple.

To create random numbers in Powershell you can use the .NET Framework class “System.Random”. First you have to create a new object.

[Object]$Random = New-Object System.Random

Lets checkout the $Random object:

$Random | Get-Member
TypeName: System.Random
 
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
Next Method int Next(), int Next(int minValue, int maxValue), int Next(int maxValue)
NextBytes Method System.Void NextBytes(byte[] buffer)
NextDouble Method double NextDouble()
ToString Method string ToString()

Now with this object you can now create random numbers very simple:

Create a random double number:

$Random.NextDouble()

Create a random int number:

$Random.Next()

Create a random number between 1 or 10:

$Random.Next(1,11)

Create a random number between 12 or 25:

$Random.Next(12,26)

Create a random number 1 or 0:

$Random.Next(0,2)