PowerShell脚本,用于将文件(而不是子文件夹)复制到SFTP并在完成后移动到子文件夹
我正在使用以下脚本将文件从本地文件夹复制到SFTP。上传文件后,我想将文件移动到子文件夹中。我只想上传PowerShell脚本,用于将文件(而不是子文件夹)复制到SFTP并在完成后移动到子文件夹,powershell,sftp,winscp,get-childitem,winscp-net,Powershell,Sftp,Winscp,Get Childitem,Winscp Net,我正在使用以下脚本将文件从本地文件夹复制到SFTP。上传文件后,我想将文件移动到子文件夹中。我只想上传C:\Users\Administrator\Desktop\ftp文件夹中的文件,而不想上传其他子文件夹中的文件 param ( $backupPath = "C:\Users\Administrator\Desktop\ftp\moved" ) # Load the Assembly and setup the session properties try { # Load
C:\Users\Administrator\Desktop\ftp
文件夹中的文件,而不想上传其他子文件夹中的文件
param (
$backupPath = "C:\Users\Administrator\Desktop\ftp\moved"
)
# Load the Assembly and setup the session properties
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$session = New-Object WinSCP.Session
$filelist = Get-ChildItem C:\Users\Administrator\Desktop\ftp
# Connect And send files, then close session
try
{
# Connect
$session.Open($sessionOptions)
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
foreach ($file in $filelist)
{
$transferResult = $session.PutFiles("C:\Users\Administrator\Desktop\ftp\$file", "/", $False, $transferOptions)
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
Move-Item $transfer.FileName $backupPath
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
# Catch any errors
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
此时,如果我运行脚本,则移动的文件夹下的所有文件也将通过SFTP文件夹上传,我只需要上传ftp
文件夹根目录中的文件
我想我需要在这里更改这一行,但不确定如何更改它
$filelist = Get-ChildItem C:\Users\Administrator\Desktop\ftp
尝试使用
Get-Item C:\Users\Administrator\Desktop\ftp -include *.*
此项不应进入子文件夹。要跳过文件夹,请添加
-File
切换到获取子项
(如@politicalist所述):
param (
$backupPath = "C:\Users\Administrator\Desktop\ftp\moved"
)
# Load the Assembly and setup the session properties
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$session = New-Object WinSCP.Session
$filelist = Get-ChildItem C:\Users\Administrator\Desktop\ftp
# Connect And send files, then close session
try
{
# Connect
$session.Open($sessionOptions)
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
foreach ($file in $filelist)
{
$transferResult = $session.PutFiles("C:\Users\Administrator\Desktop\ftp\$file", "/", $False, $transferOptions)
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
Move-Item $transfer.FileName $backupPath
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
# Catch any errors
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
虽然可以用更少的代码实现同样的效果,但如果让WinSCP迭代文件(并跳过文件夹): 您不需要任何
Get ChildItem
调用
以上基本上是WinSCP文章中的代码,只是带有一个
但是请注意,您的代码(与本文相反)没有成功上传的测试。因此,即使无法上传文件,您也会移动这些文件。确保添加了
$transfer.Error-eq$Null
测试
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
Write-Host "Upload of $($transfer.FileName) succeeded, moving to backup"
# Upload succeeded, move source file to backup
Move-Item $transfer.FileName $backupPath
}
else
{
Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
}
}
Get ChildItem-File-only返回文件对象谢谢,不幸的是,当我尝试此操作时,它仍然会进入子文件夹
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
Write-Host "Upload of $($transfer.FileName) succeeded, moving to backup"
# Upload succeeded, move source file to backup
Move-Item $transfer.FileName $backupPath
}
else
{
Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
}
}