Re: powershell session.FileExists($remotePath) foreach
Duplicate post:
https://winscp.net/forum/viewtopic.php?t=14712
https://winscp.net/forum/viewtopic.php?t=14712
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\winscp\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "server01"
$sessionOptions.UserName = "user"
$sessionOptions.Password = "user1"
$sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
$programFileNameList = Get-Content D:\program\programFileName.lst
#Program List Error Check
foreach( $programFileName in $programFileNameList )
{
Write-Host ("name: $programFileName")
$remotePath = "/home/user/utility/Program/$programFileName"
#$remotePath = "/home/user/utility/Program/program2.csv"
Write-Host ("searching: $remotePath")
if($session.FileExists($remotePath))
{
Write-Host ("File {0} exists" -f $remotePath)
}
else
{
Write-Host ("File {0} does not exist" -f $remotePath)
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}