如何制作 PowerShell FTPS 脚本 (SSL) 来上传单个文件?
How to make a PowerShell FTPS script (SSL) to upload a single file?
我需要一个脚本来帮助我将单个文件上传到云端。
我找到了一些使用 SFTP (SSH) 协议的答案,但找不到使用 FTPS (SSL) 的脚本。
我试过这个脚本,但它不起作用:
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.ParseUrl("**ftps**://**login**:**password**@**ipoftheremoteserver**:990/")
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
$session.PutFiles("D:\QAPPLI\Quadra\DATABASE\PAIE[=10=]0001\qpaie.mdb", "/FOLDER1/08h00").Check()
$session.Dispose()
我有这个错误:
PS C:\Windows\system32> D:\Scripth00_000001_Client1_to_ftps.ps1 Exception lors de l'appel de « Check » avec « 0 » argument(s) :
« Erreur lors du transfert du fichier 'D:\QAPPLI\Quadra\DATABASE\PAIE[=11=]0001\qpaie.mdb'.
Server sent passive reply with unroutable address 172.16.59.131, using host address instead.
Copie de fichiers vers le coté distant échouée.
Filename invalid
Au niveau de D:h00_000001_Client1_to_ftps.ps1 : 8
Caractère : 85
+ $session.PutFiles("D:\QAPPLI\Quadra\DATABASE\PAIE[=11=]0001\qpaie.mdb", "/FOLDER1/08h00").Check <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
翻译成英语:
D:Script08h00_000001_Client1_to_ftps.ps1 Exception when calling "Check" with "0" argument (s): «"Error transferring file ' D:QAPPLIQuadraDATABASEPAIE000001qpaie.mdb '. Server sent passive reply with unroutable address 172.16.59.131, using host address instead.
Copying files to the failed remote side.
Filename Invalid
at D:08h00_000001_Client1_to_ftps.ps1:8 character: 85 + $session. PutFiles ( "D:QAPPLIQuadraDATABASEPAIE000001qpaie.mdb", "/FOLDER1/08h00"). Check < < < () + CategoryInfo: NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId: DotNetMethodException
非常感谢您的帮助。
使用 PowerShell 中的 .Net Framework 的 FTPWebRequest class,使用 EnableSsl 属性 启用 FTPS
[Net.ServicePointManager]::ServerCertificateValidationCallback={$true}
$Dir = "D:\FolderWithBackupFilesToMove"
foreach($item in (dir $dir))
{
write-output "-------------"
$fileName = $item.FullName
write-output $fileName
$ftp = [System.Net.FtpWebRequest]::Create("ftp://some.ftp.server.com/someRootFolder/"+$item.Name)
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.UsePassive = $true
$ftp.UseBinary = $true
$ftp.EnableSsl = $true
$ftp.Credentials = new-object System.Net.NetworkCredential("UserName","Password")
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$rs = $ftp.GetRequestStream()
$reader = New-Object System.IO.FileStream ($fileName, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read)
[byte[]]$buffer = new-object byte[] 4096
[int]$count = 0
do
{
$count = $reader.Read($buffer, 0, $buffer.Length)
$rs.Write($buffer,0,$count)
} while ($count -gt 0)
$reader.Close()
$rs.Close()
write-output "+transfer completed"
$item.Delete()
write-output "+file deleted"
}
Session.PutFiles
的 remotePath
(第二个)参数是:
Full path to upload the file to.
虽然您似乎只传递了文件夹的路径,而不是文件的完整路径。
这应该是正确的:
$session.PutFiles(
"D:\QAPPLI\Quadra\DATABASE\PAIE[=10=]0001\qpaie.mdb", "/FOLDER1/08h00/qpaie.mdb").Check()
我需要一个脚本来帮助我将单个文件上传到云端。
我找到了一些使用 SFTP (SSH) 协议的答案,但找不到使用 FTPS (SSL) 的脚本。
我试过这个脚本,但它不起作用:
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.ParseUrl("**ftps**://**login**:**password**@**ipoftheremoteserver**:990/")
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
$session.PutFiles("D:\QAPPLI\Quadra\DATABASE\PAIE[=10=]0001\qpaie.mdb", "/FOLDER1/08h00").Check()
$session.Dispose()
我有这个错误:
PS C:\Windows\system32> D:\Scripth00_000001_Client1_to_ftps.ps1 Exception lors de l'appel de « Check » avec « 0 » argument(s) :
« Erreur lors du transfert du fichier 'D:\QAPPLI\Quadra\DATABASE\PAIE[=11=]0001\qpaie.mdb'. Server sent passive reply with unroutable address 172.16.59.131, using host address instead.
Copie de fichiers vers le coté distant échouée.
Filename invalid
Au niveau de D:h00_000001_Client1_to_ftps.ps1 : 8 Caractère : 85 + $session.PutFiles("D:\QAPPLI\Quadra\DATABASE\PAIE[=11=]0001\qpaie.mdb", "/FOLDER1/08h00").Check <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
翻译成英语:
D:Script08h00_000001_Client1_to_ftps.ps1 Exception when calling "Check" with "0" argument (s): «"Error transferring file ' D:QAPPLIQuadraDATABASEPAIE000001qpaie.mdb '. Server sent passive reply with unroutable address 172.16.59.131, using host address instead.
Copying files to the failed remote side.
Filename Invalid
at D:08h00_000001_Client1_to_ftps.ps1:8 character: 85 + $session. PutFiles ( "D:QAPPLIQuadraDATABASEPAIE000001qpaie.mdb", "/FOLDER1/08h00"). Check < < < () + CategoryInfo: NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId: DotNetMethodException
非常感谢您的帮助。
使用 PowerShell 中的 .Net Framework 的 FTPWebRequest class,使用 EnableSsl 属性 启用 FTPS
[Net.ServicePointManager]::ServerCertificateValidationCallback={$true}
$Dir = "D:\FolderWithBackupFilesToMove"
foreach($item in (dir $dir))
{
write-output "-------------"
$fileName = $item.FullName
write-output $fileName
$ftp = [System.Net.FtpWebRequest]::Create("ftp://some.ftp.server.com/someRootFolder/"+$item.Name)
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.UsePassive = $true
$ftp.UseBinary = $true
$ftp.EnableSsl = $true
$ftp.Credentials = new-object System.Net.NetworkCredential("UserName","Password")
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$rs = $ftp.GetRequestStream()
$reader = New-Object System.IO.FileStream ($fileName, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read)
[byte[]]$buffer = new-object byte[] 4096
[int]$count = 0
do
{
$count = $reader.Read($buffer, 0, $buffer.Length)
$rs.Write($buffer,0,$count)
} while ($count -gt 0)
$reader.Close()
$rs.Close()
write-output "+transfer completed"
$item.Delete()
write-output "+file deleted"
}
Session.PutFiles
的 remotePath
(第二个)参数是:
Full path to upload the file to.
虽然您似乎只传递了文件夹的路径,而不是文件的完整路径。
这应该是正确的:
$session.PutFiles(
"D:\QAPPLI\Quadra\DATABASE\PAIE[=10=]0001\qpaie.mdb", "/FOLDER1/08h00/qpaie.mdb").Check()