У меня есть этот код PowerShell.
У меня небольшая проблема:
Я скачиваю два удаленных файла
Device.log
иSaveinfo.dat
; Не все машины имеют одновременноdevice.log
иsaveinfo.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
}