获取使用PowerShell传输的WinSCP文件的信息

huangapple go评论56阅读模式
英文:

Get info on files transferred with WinSCP in PowerShell

问题

我使用此脚本通过WinSCP获取两个日期之间的文件。如何在控制台中打印出在远程计算机上找到的文件和未找到的文件,因为目前我不确定是否已下载所有文件,因为在两个时间之间可能会丢失文件。(类似于:如果我输入从2023年05月07日到2023年07月07日的日期;脚本在远程计算机上没有找到一个或多个文件,则在控制台中返回此消息“从日期x开始缺少文件*.log;与“文件夹$remotefolder存在与否相同)

谢谢,

英文:

I use this script to get file between two dates via WinSCP. How is it possible to print in the console which files were found on the remote computer and which were not because at the moment I am not sure that all the files have been downloaded because it is possible that between the 2 times files are missing. (something like : If I enter date from 05.07.2023 to 07.07.2023 ; and script didn't find on remote computer one or more files return this message in console "File *.log from date x missing on remote computer ; same like "folder $remotefolder exist or not)

# Set up session options
$options = @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = $entry.IP
    UserName = $User
    Password = $Password
    GiveUpSecurityAndAcceptAnySshHostKey = $true
}

try {
    # Set up session options using first password
    $sessionOptions = New-Object WinSCP.SessionOptions -Property $options
    $session = New-Object WinSCP.Session
    # Try Connect
    $session.Open($sessionOptions)
} 
catch {
    # Set up session options using second settings
    $options['HostName'] = $vpnIP
    $options['UserName'] = $User
    $options['Password'] = $Password
    try {
        $sessionOptions = New-Object WinSCP.SessionOptions -Property $options
        $session = New-Object WinSCP.Session
        # Try Connect
        $session.Open($sessionOptions)
    }
    catch {
        Write-Error "Could not open WinSCP session: $($_.Exception.Message)"
        throw
    }
}

# Date 1 START
do {
    $date = $null
    $today = Read-Host -Prompt ('Enter START date (inclusive) (e.g. {0}) [yyyy.MM.dd]' -f (Get-Date -Format "yyyy.MM.dd"))

    try {
        $date = Get-Date -Date $today -Format "yyyy-MM-dd" -ErrorAction Stop
        '[OK] {0} Valid date - OK!' -f $date
    }
    catch {
        '[X] {0} Invalid date!' -f $today
    }
}
until ($date)

# Date 2 STOP

do {
    $date1 = $null
    Write-Host "Add +1 day" -ForegroundColor Red
    $today1 = Read-Host -Prompt ('Enter END date (exclusive) (e.g. {0}) [yyyy.MM.dd]' -f (Get-Date -Format "yyyy.MM.dd"))

    try {
        $date1 = Get-Date -Date $today1 -Format "yyyy-MM-dd" -ErrorAction Stop
        '[OK] {0} Valid date - OK!' -f $date1
    }
    catch {
        '[X] {0} Invalid date!' -f $today1
    }
}
until ($date1)

# ----- Date END

$session = New-Object WinSCP.Session

$file = "*.log"
$localPath = "\temp_files" 
$remotePath = "/C:/log", "/C:/Back_up"

try {
    # Connect
    $session.Open($sessionOptions)

    # Check exists folder
    foreach ($remotePath in $remotePath)
{
    if ($session.FileExists($remotePath))
    {
        Write-Host "[OK] Folder '$remotePath' exist" -ForegroundColor Green

            # Transfer file
        Write-Host "[i] '$date' - '$date1' > '$inputID' downloading..." -ForegroundColor Cyan

    $session.GetFilesToDirectory($remotePath, $localPath, "*.log>=$date<=$date1").Check();

    }
    else
    {
        Write-Host "[X] INFO: Folder: '$remotePath' doesn't exist" -ForegroundColor Red
        }
    }
}
finally {
    $session.Dispose()
}

Thank you,

答案1

得分: 1

使用来自基本WinSCP PowerShell下载示例的代码:

$transferResult =
    $session.GetFilesToDirectory($remotePath, $localPath, "*.log>=$date<=$date1")

# 在任何错误时引发异常
$transferResult.Check()

# 打印结果
foreach ($transfer in $transferResult.Transfers)
{
    Write-Host "下载 $($transfer.FileName) 成功"
}

您的后续问题:
https://stackoverflow.com/q/76747210/850848

英文:

Use the code from the basic WinSCP PowerShell download example:

$transferResult =
    $session.GetFilesToDirectory($remotePath, $localPath, &quot;*.log&gt;=$date&lt;=$date1&quot;)

# Throw on any error
$transferResult.Check()

# Print results
foreach ($transfer in $transferResult.Transfers)
{
    Write-Host &quot;Download of $($transfer.FileName) succeeded&quot;
}

Your follow-up question:
https://stackoverflow.com/q/76747210/850848

huangapple
  • 本文由 发表于 2023年7月10日 18:05:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定