search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Posted in Windows Powershell | 2 Comments | 11,151 views | 11/08/2012 17:01

I’ve used this script on my one of the projects to get last file of FTP directory.
So you always get latest log file in a directory with Powershell.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# FTP Config
$FTPHost = "10.10.10.20"
$Username = "ftpusr"
$Password = "12345678"
$FTPFolder = "%2f/my/log/path"
 
do
{
	# List Directory
	$FTPRequest = [System.Net.FtpWebRequest]::Create("$FTPFolderUrl") 
	$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username, $Password) 
	$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
 
	# Get Last Log File
	$FTPResponse = $FTPRequest.GetResponse()
	$ResponseStream = $FTPResponse.GetResponseStream()
	$FTPReader = New-Object System.IO.Streamreader -ArgumentList $ResponseStream
	do
	{
		try
		{
			$LogData = $FTPReader.ReadToEnd()
			Add-Content -Value $LogData -Path "$ScriptPath\FileList.txt"
			$LastLogFile = Get-Content -Path "$ScriptPath\FileList.txt" | ? {$_.trim() -ne ""} | Select -Last 1
			Clear-Content -Path "$ScriptPath\FileList.txt"
		}
		catch
		{
			Write-Debug $_
		}
	}
	while ($FTPReader.ReadLine() -ne $null)
	$FTPReader.Close()
 
	if ($LastLogFile -ne $null -and $LastLogFile -ne "" -and $LastLogFile -ne " ")
	{
		$ShouldProcess = "Continue"
	}
}
while ($ShouldProcess -ne "Continue")
 
Write-Host "Log File: $LastLogFile"

$LastLogFile is the name of the latest log file.