Question Upload ftp

Plus d'informations
il y a 16 ans 5 mois #5478 par fabrice
Upload ftp a été créé par fabrice
Bonjour,
Je débute en PowerShell
J'ai trouvé ce code pour downloader un fichier du serveur ftp vers local, celui-ci fonctionne parfaitement

1. function Get-FTPFile ($Source,$Target,$UserName,$Password)
2. {
3.
4. # Create a FTPWebRequest object to handle the connection to the
ftp server
5. $ftprequest = [System.Net.FtpWebRequest]::create($Source)
6.
7. # set the request's network credentials for an authenticated connection
8. $ftprequest.Credentials =
9. New-Object System.Net.NetworkCredential($username,$password)
10.
11. $ftprequest.Method = System.Net.WebRequestMethods
+Ftp]::DownloadFile
12. $ftprequest.UseBinary = $true
13. $ftprequest.KeepAlive = $false
14.
15. # send the ftp request to the server
16. $ftpresponse = $ftprequest.GetResponse()
17.
18. # get a download stream from the server response
19. $responsestream = $ftpresponse.GetResponseStream()
20.
21. # create the target file on the local system and the download buffer
22. $targetfile = New-Object IO.FileStream ($Target,
[IO.FileMode]::Create)
23. [byte[]]$readbuffer = New-Object byte[] 1024
24.
25. # loop through the download stream and send the data to the
target file
26. do{
27. $readlength = $responsestream.Read($readbuffer,0,1024)
28. $targetfile.Write($readbuffer,0,$readlength)
29. }
30. while ($readlength -ne 0)
31.
32. $targetfile.close()
33. }
34.
35. $sourceuri = \"ftp://MyFtpServer/FolderPath\File.txt\"
36. $targetpath = \"C:\temp\MyFile.txt\"
37. $user = \"Username\"
38. $pass = \"Password\"
39. Get-FTPFile $sourceuri $targetpath $user $pass



Pourriez-vous m'aider pour l'upload j'arrive en modifiant ce script à écrire le fichier sur le serveur (Methods +Ftp]::UploadFile) mais il est vide

Merci

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 16 ans 5 mois #5484 par Laurent Dardenne
Réponse de Laurent Dardenne sur le sujet Re:Upload ftp
Salut,
fab44 écrit:

Pourriez-vous m'aider

Peux-tu proposer un code reformaté ?
:whistle:<br><br>Message édité par: Laurent Dardenne, à: 14/10/09 19:29

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 16 ans 5 mois #5488 par fabrice
Réponse de fabrice sur le sujet Re:Upload ftp
Bonjour,
Voilà le script modifié

#=====================DEBUT BLOC FUNCTION PUT-FTPFile===========================
function Put-FTPFile ($Destination,$localfile,$UserName,$Password)
{
# Create a FTPWebRequest object to handle the connection to the ftp server
$ftprequest = [System.Net.FtpWebRequest]::create($Destination)
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$ftprequest.UsePassive = $true
$ftprequest.UseBinary = $true
$ftprequest.KeepAlive = $false

# send the ftp request to the server
$ftpresponse = $ftprequest.GetResponse()
$responsestream = $ftpresponse.GetResponseStream()
# create the target file on the local system and the download buffer
$targetfile = New-Object IO.FileStream ($localfile,[IO.FileMode]::Create)
[byte[]]$readbuffer = New-Object byte[] 1024

# loop through the download stream and send the data to the target file
do{
$readlength = $responsestream.Read($readbuffer,0,1024)
$targetfile.Write($readbuffer,0,$readlength)
}
while ($readlength -ne 0)
$targetfile.close()
}
#=====================FIN BLOC FUNCTION PUT-FTPFile=============================

#Envoi fichier
$Destination = \&quot;ftp://.../test.trf\&quot;
$localfile = \&quot;D:\...\test.trf\&quot;
$user = \&quot;user\&quot;
$pass = \&quot;pass\&quot;
Put-FTPFile $Destination $localfile $user $pass

J'arrive à créer le fichier test sur le serveur mais il n'est pas vraiment uploader juste créer à o
Merci pour votre aide

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.043 secondes
Propulsé par Kunena