PowerShell 和 WinSCP 脚本检查 SFTP 远程目录中是否有超过 4 个文件
PowerShell and WinSCP script to check if there are more than 4 files in a SFTP remote directory
使用 PowerShell 语言和 WinSCP,我正在尝试创建一个脚本,每天检查 SFTP 远程目录以查看其中是否有超过 4 个文件。
如果少于 4 个文件没关系,但如果超过 4 个文件,它将输出错误消息。
感谢 WinSCP,自动创建了连接,我可以在下面连接到 SFTP 服务器:
& "C:\Program Files (x86)\WinSCP\WinSCP.com"
/log="C:\Users\scripts\WinSCP.log" /ini=nul
/command
"open sftp://..."
"cd" `
"cd ./my remote directory"
#"ls *.csv"
#"exit"
$winscpResult = $LastExitCode
if ($winscpResult -eq 0)
{
Write-Host "Success"
}
else
{
Write-Host "Error"
}
exit $winscpResult
我不知道我是否必须通过脚本或 .NET 汇编语言来执行此操作:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = ""
UserName = ""
Password = ""
SshHostKeyFingerprint = ""
TimeoutInMilliseconds = 60000
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Your code
}
finally
{
$session.Dispose()
}
但是我目前不知道如何执行该条件。
脚本完成后,目标是每天将它用于 Jenkins 运行。
你能帮我用 and else 建立检查条件吗?谢谢!
使用 WinSCP .NET 程序集,这很简单:
$files =
$session.ListDirectory($remotePath).Files |
Where-Object { -Not $_.IsDirectory }
$count = $files.Count
if ($count -gt 4)
{
Write-Host "There are more than 4 files in $remotePath"
}
我终于修改了 Martin 的脚本,现在它可以成功检查我的目录中的文件是少于还是多于 4 个:
$remotePath = "/my-directory"
$files = $session.ListDirectory($remotePath).Files | Where-Object { -Not $_.IsDirectory }
$count = $files.Count
if ($count -le 4)
{
Write-Host "There are less than 4 files into the"$remotePath" directory. All good!"
}
else
{
Write-Host "There are more than 4 files into the"$remotePath" directory. Please check!"
}
我实际上在 $session.ListDirectory.
之后需要 .Files 参数
谢谢!
使用 PowerShell 语言和 WinSCP,我正在尝试创建一个脚本,每天检查 SFTP 远程目录以查看其中是否有超过 4 个文件。
如果少于 4 个文件没关系,但如果超过 4 个文件,它将输出错误消息。
感谢 WinSCP,自动创建了连接,我可以在下面连接到 SFTP 服务器:
& "C:\Program Files (x86)\WinSCP\WinSCP.com"
/log="C:\Users\scripts\WinSCP.log" /ini=nul
/command
"open sftp://..."
"cd" `
"cd ./my remote directory"
#"ls *.csv"
#"exit"
$winscpResult = $LastExitCode
if ($winscpResult -eq 0)
{
Write-Host "Success"
}
else
{
Write-Host "Error"
}
exit $winscpResult
我不知道我是否必须通过脚本或 .NET 汇编语言来执行此操作:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = ""
UserName = ""
Password = ""
SshHostKeyFingerprint = ""
TimeoutInMilliseconds = 60000
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Your code
}
finally
{
$session.Dispose()
}
但是我目前不知道如何执行该条件。
脚本完成后,目标是每天将它用于 Jenkins 运行。
你能帮我用 and else 建立检查条件吗?谢谢!
使用 WinSCP .NET 程序集,这很简单:
$files =
$session.ListDirectory($remotePath).Files |
Where-Object { -Not $_.IsDirectory }
$count = $files.Count
if ($count -gt 4)
{
Write-Host "There are more than 4 files in $remotePath"
}
我终于修改了 Martin 的脚本,现在它可以成功检查我的目录中的文件是少于还是多于 4 个:
$remotePath = "/my-directory"
$files = $session.ListDirectory($remotePath).Files | Where-Object { -Not $_.IsDirectory }
$count = $files.Count
if ($count -le 4)
{
Write-Host "There are less than 4 files into the"$remotePath" directory. All good!"
}
else
{
Write-Host "There are more than 4 files into the"$remotePath" directory. Please check!"
}
我实际上在 $session.ListDirectory.
之后需要 .Files 参数谢谢!