PoshCode Archive  Artifact [06c886c6bf]

Artifact 06c886c6bfd8efade2cfd4a55e3ad1b791f222f84e67e6c41adb18f251b6e44d:

  • File FTP-functions.ps1 — part of check-in [b64e138eb4] at 2018-06-10 13:59:20 on branch trunk — Send-XMLFile (user: redyey size: 2668)

# encoding: ascii
# api: powershell
# title: FTP functions
# description: Send-XMLFile
# version: 0.1
# type: function
# author: redyey
# license: CC0
# function: Send-XMLFile
# x-poshcode-id: 5690
# x-archived: 2016-06-08T09:41:20
# x-published: 2016-01-14T17:24:00
#
# Verify-XMLFileUpload
#
function Send-XMLFile ($Path, $IP, $Username, $Password)
{
	[Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }
	if (!(Test-Path $Path\SSP.XML))
	{
		Write-Error "No file found in directory exiting script"
	}
	
	else
	{
		$Filename = Get-Childitem $Path\SSP.XML -File | Select-Object Name, FullName
		Write-Output $Filename.FullName | Out-Null
		$FTP = [System.Net.FTPWebRequest]::Create("FTP://$IP/" + $Filename.Name)
		$FTP = [System.Net.FTPWebRequest]$FTP
		$FTP.UsePassive = $True
		$FTP.UseBinary = $True
		$FTP.EnableSsl = $True
		$FTP.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
		$FTP.Method = [System.Net.WebRequestMethods+FTP]::UploadFile
		$RS = $FTP.GetRequestStream()
		
		$Reader = New-Object System.IO.FileStream ($Filename.FullName, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read)
		[byte[]]$Buffer = New-Object byte[] 4096
		[int]$Count = 0
		do
		{
			$Count = $Reader.Read($Buffer, 0, $Buffer.Length)
			$RS.Write($Buffer, 0, $Count)
		}
		while ($Count -gt 0)
		$Reader.Close()
		$RS.Close()
	}
}

function Verify-XMLFileUpload($Username, $Password, $FTP_URI, $SubFolder)
{
	try
	{
		$FTP_URIX = $FTP_URI + $SubFolder
		$URI = [System.URI]$FTP_URIX
		$Encoding = [System.Text.Encoding]::GetEncoding(28591) #Set Encoding To ISO-8859-1 (Westeuropean ISO)
		[Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }
		$FTP = [System.Net.FTPWebRequest]::Create($URI)
		$FTP.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
		$FTP.Method = [system.Net.WebRequestMethods+FTP]::ListDirectoryDetails
		$FTP.UsePassive = $True
		$FTP.UseBinary = $True
		$FTP.EnableSsl = $True
		$Response = $FTP.GetResponse()
		$Strm = $Response.GetResponseStream()
		$Reader = New-Object System.IO.StreamReader($Strm, $Encoding)
		$List = $Reader.ReadToEnd()
		$Reader.Close | Out-Null
	}
	catch [System.InvalidOperationException]
	{
		throw "GetResponse or BeginGetResponse was used more than once OR EnableSSL was set to True but the server does not support this feature."
	}
}


# Execute like this

Send-XMLFile -IP "10.0.0.1" -Password $Pass -Username "redyey" -Path "C:\toupload"
Verify-XMLFileUpload -Username "redyey" -Password $Pass -FTP_URI FTP://10.0.0.1/ -subfolder /