仅当文件存在时,才在 PowerShell 中使用 WinSCP 从 SFTP 服务器从阵列下载文件
Download files from an array from SFTP server using WinSCP in PowerShell only when the files exist
我有这个 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
}
脚本哪里出错了?我可以看到您认为文件存在的两个地方。在调用 $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"
}
}
我有这个 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
}
脚本哪里出错了?我可以看到您认为文件存在的两个地方。在调用 $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"
}
}