繁体   English   中英

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

[英]Download files from an array from SFTP server using WinSCP in PowerShell only when the files exist

我有这个 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 arrays 传递给不采用 arrays 的 .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"
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM