Question
Upload ftp
- fabrice
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
Réduire
Plus d'informations
- Messages : 2
- Remerciements reçus 0
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
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.
- Laurent Dardenne
- Hors Ligne
- Modérateur
-
Réduire
Plus d'informations
- Messages : 6311
- Remerciements reçus 68
il y a 16 ans 5 mois #5484
par Laurent Dardenne
Tutoriels PowerShell
Réponse de Laurent Dardenne sur le sujet Re:Upload ftp
Salut,
fab44 écrit:
<br><br>Message édité par: Laurent Dardenne, à: 14/10/09 19:29
fab44 écrit:
Peux-tu proposer un code reformaté ?Pourriez-vous m'aider
Tutoriels PowerShell
Connexion ou Créer un compte pour participer à la conversation.
- fabrice
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
Réduire
Plus d'informations
- Messages : 2
- Remerciements reçus 0
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 = \"ftp://.../test.trf\"
$localfile = \"D:\...\test.trf\"
$user = \"user\"
$pass = \"pass\"
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
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 = \"ftp://.../test.trf\"
$localfile = \"D:\...\test.trf\"
$user = \"user\"
$pass = \"pass\"
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
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Entraide pour les débutants
- Upload ftp