Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ftp issue....

51 views
Skip to first unread message

Rick

unread,
Jan 12, 2007, 9:32:00 AM1/12/07
to
I'm trying to make the mundane task of ftp one which is used hundreds of time
daily work in posh.
I can retrieve the directory list with this:

$req = [System.Net.FtpWebRequest]::create('ftp://myserver' )
$req.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
$req.Credentials = New-Object net.NetworkCredential ("USER" ,"PAss" )
$response = $req.GetResponse()
$stream = $response.GetResponseStream()
$reader = New-Object io.streamreader($stream)
$reader.ReadToEnd()

but I error out on upload or download.

$req = [System.Net.FtpWebRequest]::create('ftp://myserver' )
$req.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$req.Credentials = New-Object net.NetworkCredential ("USER" ,"PAss" )
# $req.Binary = False <<errors hence commented out
$Put = $req.GetRequestStream
$Put.Write("C:\tlist") <<<<<<error here
# $Put.Write("C:\tlist")
Method invocation failed because [System.Management.Automation.PSMethod]
doesn't contain a method named 'Write'
At line:1 char:11
+ $Put.Write( <<<< "C:\tlist")
$Put.Close()
$response = $req.GetResponse()
$stream = $response.GetResponseStream()
$reader = New-Object io.streamreader($stream)
$reader.ReadToEnd()

shouldn't I be able to change listdirectory for any of these valuse?
# AppendFile -Represents the FTP APPE protocol method that is used to
append a file to an existing file on an FTP server.
# DeleteFile -Represents the FTP DELE protocol method that is used to
delete a file on an FTP server.
# DownloadFile -Represents the FTP RETR protocol method that is used to
download a file from an FTP server.
# GetDateTimestamp
# GetFileSize -Represents the FTP SIZE protocol method that is used to
retrieve the size of a file on an FTP server.
# ListDirectory -Represents the FTP NLIST protocol method that gets a
short listing of the files on an FTP server.
# ListDirectoryDetails -Represents the FTP LIST protocol method that
gets a detailed listing of the files on an FTP server.
# MakeDirectory -Represents the FTP MKD protocol method creates a
directory on an FTP server.
# PrintWorkingDirectory -Represents the FTP PWD protocol method that
prints the name of the current working directory.
# RemoveDirectory -Represents the FTP RMD protocol method that removes a
directory.
# Rename -Represents the FTP RENAME protocol method that renames a
directory.
# UploadFile -Represents the FTP STOR protocol method that uploads a
file to an FTP server.
# UploadFileWithUniqueName
I am obvivously a neewbe to .net and hopefully missing something simple.
any help would be greatly appreciated.

Maximilian Hänel

unread,
Jan 12, 2007, 11:48:37 AM1/12/07
to
Hi Rick,

> $Put = $req.GetRequestStream
> $Put.Write("C:\tlist") <<<<<<error here
> # $Put.Write("C:\tlist")

GetRequestStream is a Method so you must write parenthesis:

$Put = $req.GetRequestStream()

hth

Max

Keith Hill [MVP]

unread,
Jan 12, 2007, 1:56:22 PM1/12/07
to
"Rick" <Ri...@discussions.microsoft.com> wrote in message
news:EF6AD474-4D5F-4B7A...@microsoft.com...

> I'm trying to make the mundane task of ftp one which is used hundreds of
> time
> daily work in posh.
> I can retrieve the directory list with this:
>
> $req = [System.Net.FtpWebRequest]::create('ftp://myserver' )
> $req.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
> $req.Credentials = New-Object net.NetworkCredential ("USER" ,"PAss" )
> $response = $req.GetResponse()
> $stream = $response.GetResponseStream()
> $reader = New-Object io.streamreader($stream)
> $reader.ReadToEnd()
>
> but I error out on upload or download.
>
> $req = [System.Net.FtpWebRequest]::create('ftp://myserver' )
> $req.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
> $req.Credentials = New-Object net.NetworkCredential ("USER" ,"PAss" )
> # $req.Binary = False <<errors hence commented out

Try using $false.

> $Put = $req.GetRequestStream
> $Put.Write("C:\tlist") <<<<<<error here
> # $Put.Write("C:\tlist")

The Stream Write method has this signature: Write(byte[] bytes, int offset,
int count). So try:

$put.Write(([text.encoding]::Unicode.GetBytes("C:\tlist")), 0, 0)

--
Keith


Rick

unread,
Jan 12, 2007, 2:11:00 PM1/12/07
to
am I the only one who has tried to ftp?
I now get error:
221[C:\Powershell]
# $req = [System.Net.FtpWebRequest]::create('ftp://myserver' )
222[C:\Powershell]
# $req.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
223[C:\Powershell]
# $req.Credentials = New-Object net.NetworkCredential ("USER" ,"pass" )
224[C:\Powershell]
# #$req.Binary = $False
225[C:\Powershell]
# $Put = $req.GetRequestStream()
Exception calling "GetRequestStream" with "0" argument(s): "The requested
URI is invalid for this FTP com
At line:1 char:29
+ $Put = $req.GetRequestStream( <<<< )
226[C:\Powershell]
# $put.Write(([text.encoding]::Unicode.GetBytes("C:\tlist")), 0, 0)

Method invocation failed because [System.Management.Automation.PSMethod]
doesn't contain a method named '
At line:1 char:11
+ $put.Write( <<<< ([text.encoding]::Unicode.GetBytes("C:\tlist")), 0, 0)
227[C:\Powershell]
# $Put.Close()

Jim B

unread,
Jan 17, 2007, 10:38:19 PM1/17/07
to
No you aren't the only one. I'm have been working on the same issue
and am stuck at the same spot. (it took me a while to learn about the
"+" notation and still haven't figured out when I need to use "::" so I
just try it when "." doesn't work when I think it should) I found a C#
ftp program example which got me to this stage but like you I get
errors that I just don't understand. I read that someone wrote an ftp
provider but I can't find it anywhere.

Brandon Shell

unread,
Jan 17, 2007, 11:40:08 PM1/17/07
to
I haven't used the "+" notation so I can not speak to that, but the "::" is
used when referencing a static method, static field, or enumeration.

Example:
Static Method
[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey()
http://msdn2.microsoft.com/en-us/library/microsoft.win32.registrykey.openremotebasekey.aspx

Static Field
[Microsoft.Win32.Registry]::LocalMachine
http://msdn2.microsoft.com/en-us/library/microsoft.win32.registry.localmachine.aspx

Enumeration:
[Microsoft.Win32.RegistryHive]::LocalMachine
http://msdn2.microsoft.com/en-us/microsoft.win32.registryhive.aspx

"Jim B" <jim....@gmail.com> wrote in message
news:1169091499.3...@m58g2000cwm.googlegroups.com...

Jim B

unread,
Jan 18, 2007, 12:33:13 AM1/18/07
to
oh ok so whenever I do a $myobject | gm -static I can use the "::" to
invoke it. Thanks for the info. I'm still wishing for that ftp
provider code.

campi

unread,
Sep 24, 2007, 1:05:34 PM9/24/07
to

Thanks for pointing me in the right direction, I finally got this to
work for binary mode FTP from powershell:

function ftpfile{
param([string]$filename,[string]$port=21,[string]$user,[string]$pass,[string]$ftpUri)

$fileContent = [System.IO.File]::ReadAllBytes($fileName)
$req = [System.Net.FtpWebRequest]::create($ftpUri)
$req.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$req.Credentials = New-Object System.net.NetworkCredential($user,$pass)
$req.UseBinary = $true
$req.ContentLength = $fileContent.Length
$requestStream = $req.GetRequestStream()
$requestStream.Write($fileContent,0, $fileContent.Length)
$requestStream.Close()
}

ftpfile -filename "C:\test.exe" -port 21 -user "uid" -pass "pwd"
-ftpUri "ftp://servername.domain/path/test.exe"


--
campi

0 new messages