仅当文件存在时,才在 PowerShell 中使用 WinSCP 从 SFTP 服务器从阵列下载文件

问题描述

我有这个 PowerShell 代码

我有一个小问题:

  • 我下载了两个远程文件Device.logSaveinfo.dat;并非所有机器都有 device.logsaveinfo.dat。有些机器只有 device.log,当我尝试使用只有 device.log 的机器时,脚本失败。我能做什么?

  • 如果机器有两个文件存档,如果机器只有 device.log 存档只有那个文件 (device.log)。

谢谢

Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
 
$db = import-csv -Path "C:\Program Files (x86)\WinSCP\db.csv"
 
$inputID = Read-Host -Prompt "ID"
 
$entry = $db -match $inputID

Write-Host "IP:" $entry.IP

    $User = "user"
    $Password = "password"
    $Command = "C:\SaveBVInfo.exe"

    $secpasswd = ConvertTo-securestring $Password -AsPlainText -Force
    $Credentials = New-Object System.Management.Automation.PSCredential($User,$secpasswd)

Get-SSHTrustedHost | Remove-SSHTrustedHost

$SessionID = New-SSHSession -ComputerName $entry.IP -Credential $Credentials -AcceptKey:$true

Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command

# Set up session options
$sessionoptions = New-Object WinSCP.Sessionoptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = $entry.IP
    UserName = "$User"
    Password = "$Password"
    GiveUpSecurityAndAcceptAnySshHostKey = "true"
}
 
$session = New-Object WinSCP.Session

$file = "Device.log","SaveBVInfo.dat"
$localPath = "E:\loguri\Log\Arhive\*"
$remotePath = "/C:/Program Files/Common Files/logs/Device.log","/C:/Program Files/Pc/SaveBVInfo.dat"
 
try {

    # Connect
    $session.Open($sessionoptions)

    # Check files exists
 
        if ($session.FileExists($remotePath))
        {
            Write-Host "File $remotePath exists"
            # Transfer files

            $session.GetFiles($remotePath,$localPath).Check()
 
            exit 0
        }
        else
        {
            Write-Host "File $remotePath does not exist"
            exit 1
        }
}
finally {
    $session.dispose()
}
foreach ($file in "E:\loguri\Log\Arhive\Device.log","E:\loguri\Log\Arhive\SaveBVInfo.dat") {
    Compress-Archive $file -DestinationPath "E:\Arhive\$inputID.zip" -Update
    Remove-Item $file -Force
}

解决方法

脚本在哪里失败?我可以看到您假设文件存在的两个地方。在您调用 $session.GetFiles() 之前,您一定要先调用 $session.FileExists() 以验证它是否存在。见https://winscp.net/eng/docs/script_checking_file_existence

其次,如果存在任一文件,则您的 Test-Path 将为真,但无论是否只有一个文件,您都将两者都添加到存档中。检查每个文件,然后添加。

foreach ($file in "E:\Arhive\Device.log","E:\Arhive\Saveinfo.dat") {
    if (Test-Path $file) {
        Compress-Archive $file -DestinationPath "E:\Arhive\$inputID.zip" -Update
        Remove-Item $file
     }
}
,

您不能将 PowerShell 数组传递给不接受数组的 .NET 方法。

你必须循环处理你的文件:

$remotePaths = "/C:/Program Files/Common Files/logs/Device.log","/C:/Program Files/Pc/SaveBVInfo.dat"
foreach ($remotePath in $remotePaths)
{
    if ($session.FileExists($remotePath))
    {
        Write-Host "File $remotePath exists"
        # Transfer files

        $session.GetFiles($remotePath,$localPath).Check()
    }
    else
    {
        Write-Host "File $remotePath does not exist"
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...