Trying to download a file from ftp site but receiving errors. "You cannot call a method on a null-valued expression"
Is anybody achieving this?
# create the FtpWebRequest
$ftp=[system.net.ftpwebrequest]::Create('ftp://ftp.site/files/export/')
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.Method = [System.Net.WebRequestMethods+Ftp]:ownloadFile
$ftp.Credentials = new-object System.Net.NetworkCredential("id","pw")
$ftp.UseBinary = $true
$ftp.UsePassive = $true
$folderPath="ftp://ftp.site/files/export"
$ftp.Method=[system.net.webrequestmethods+ftp]::ListDirectoryDetails
$response=$ftp.GetResponse()
$strm=$response.GetResponseStream()
$reader=New-Object System.IO.StreamReader($strm,'UTF-8')
$reader.ReadToEnd()
# create the target file on the local system and the download buffer
$targetfile = New-Object IO.FileStream ($targetpath = "C:\Exports\ftpfile7",[IO.FileMode]::Create)
[byte[]]$readbuffer = New-Object byte[] 1024
$folderName = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")
New-Item -itemType Directory -Path c:\Exports -Name $FolderName
#Loop through the download
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$LocalFileFile.Write($ReadBuffer,0,$ReadLength)
}
while ($ReadLength -ne 0)
$LocalFileFile.close()
Is anybody achieving this?
# create the FtpWebRequest
$ftp=[system.net.ftpwebrequest]::Create('ftp://ftp.site/files/export/')
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.Method = [System.Net.WebRequestMethods+Ftp]:ownloadFile
$ftp.Credentials = new-object System.Net.NetworkCredential("id","pw")
$ftp.UseBinary = $true
$ftp.UsePassive = $true
$folderPath="ftp://ftp.site/files/export"
$ftp.Method=[system.net.webrequestmethods+ftp]::ListDirectoryDetails
$response=$ftp.GetResponse()
$strm=$response.GetResponseStream()
$reader=New-Object System.IO.StreamReader($strm,'UTF-8')
$reader.ReadToEnd()
# create the target file on the local system and the download buffer
$targetfile = New-Object IO.FileStream ($targetpath = "C:\Exports\ftpfile7",[IO.FileMode]::Create)
[byte[]]$readbuffer = New-Object byte[] 1024
$folderName = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")
New-Item -itemType Directory -Path c:\Exports -Name $FolderName
#Loop through the download
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$LocalFileFile.Write($ReadBuffer,0,$ReadLength)
}
while ($ReadLength -ne 0)
$LocalFileFile.close()