Windows Powershell Script Help - Slight Tweak

This topic contains 6 replies, has 2 voices, and was last updated by Profile photo of Dirk Dirk 3 days, 16 hours ago.

  • Author
    Posts
  • #73207
    Profile photo of
    Anonymous

    Hello,

    I would be grateful if someone could help me with my issue. I have got a powershell script which downloads files from a SFTP folder into a local folder on my computer and then the file is deleted. It works fine. However, I want it to download into two local folders and then for the file to be deleted. Can anyone please assist me with this. Here is the current code below, just want to add an additional local path:

    [CODE]param (
    $localPath = "C:\Users\Veer\Desktop\In International\",
    $remotePath = "/accounts/12015328/Reports/"
    )

    try
    {
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"

    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "sftp.channeladvisor.com"
    UserName = "veergudka"
    Password = "Veergudka1"
    SshHostKeyFingerprint = "ssh-ed25519 256 5b:26:29:d1:ec:66:c8:77:dd:64:04:11:a9:11:6e:fe"
    }

    $session = New-Object WinSCP.Session

    try
    {
    # Connect
    $session.Open($sessionOptions)

    # Synchronize files to local directory, collect results
    $synchronizationResult = $session.SynchronizeDirectories(
    [WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $False)

    # Deliberately not calling $synchronizationResult.Check
    # as that would abort our script on any error.
    # We will find any error in the loop below
    # (note that $synchronizationResult.Downloads is the only operation
    # collection of SynchronizationResult that can contain any items,
    # as we are not removing nor uploading anything)

    # Iterate over every download
    foreach ($download in $synchronizationResult.Downloads)

    }
    }
    finally
    {
    # Disconnect, clean up
    $session.Dispose()
    }

    exit 0
    }
    catch [Exception]
    {
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1
    }[/CODE]

    Thanks in advance!

  • #73226
    Profile photo of Dirk
    Dirk
    Participant

    Hi,

    this should do the trick (just pasting the relevant parts):

    $localPaths = 'c:\folder1\', 'c:\folder2\'
    # Synchronize files to local directory, collect results
    foreach ($localPath in $localPaths){
        $synchronizationResult = $session.SynchronizeDirectories(
        [WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $False)
    }
  • #73274
    Profile photo of
    Anonymous

    Hello,

    Thanks so much for your response, I am very grateful for this. However, it does not work. This is the code I amended it too, please help. This is the image of the error:

      param (
      $localPaths = "C:\Users\Veer\Desktop\In\", "C:\Users\Veer\Desktop\In International\",
      $remotePath = "/accounts/12015328/Reports/"
      )

      try
      {
      # Load WinSCP .NET assembly
      Add-Type -Path "WinSCPnet.dll"

      # Setup session options
      $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
      Protocol = [WinSCP.Protocol]::Sftp
      HostName = "sftp.channeladvisor.com"
      UserName = "veergudka"
      Password = "Veergudka1"
      SshHostKeyFingerprint = "ssh-ed25519 256 5b:26:29:d1:ec:66:c8:77:dd:64:04:11:a9:11:6e:fe"
      }

      $session = New-Object WinSCP.Session

      try
      {
      # Connect
      $session.Open($sessionOptions)

      # Synchronize files to local directory, collect results
      foreach ($localPath in $localPaths){
      $synchronizationResult = $session.SynchronizeDirectories(
      [WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $False)
      }

      # Deliberately not calling $synchronizationResult.Check
      # as that would abort our script on any error.
      # We will find any error in the loop below
      # (note that $synchronizationResult.Downloads is the only operation
      # collection of SynchronizationResult that can contain any items,
      # as we are not removing nor uploading anything)

      # Iterate over every download
      foreach ($download in $synchronizationResult.Downloads)
      {
      # Success or error?
      if ($download.Error -eq $Null)
      {
      Write-Host ("Download of {0} succeeded, removing from source" -f
      $download.FileName)
      # Download succeeded, remove file from source
      $removalResult = $session.RemoveFiles($session.EscapeFileMask($download.FileName))

      if ($removalResult.IsSuccess)
      {
      Write-Host ("Removing of file {0} succeeded" -f
      $download.FileName)
      }
      else
      {
      Write-Host ("Removing of file {0} failed" -f
      $download.FileName)
      }
      }
      else
      {
      Write-Host ("Download of {0} failed: {1}" -f
      $download.FileName, $download.Error.Message)
      }
      }
      }
      finally
      {
      # Disconnect, clean up
      $session.Dispose()
      }

      exit 0
      }
      catch [Exception]
      {
      Write-Host ("Error: {0}" -f $_.Exception.Message)
      exit 1
      }

  • #73339
    Profile photo of Dirk
    Dirk
    Participant

    Hi,

    I don't see the error message. Without it, it's pretty hard to tell what is wrong.

  • #73490
    Profile photo of
    Anonymous

    Hello,

    Thanks for your response, apologies I never saw it. Let me try again, see link below for screenshot:

    2017-06-22_1656

    I look forward to your response.

  • #73564
    Profile photo of
    Anonymous

    Any help would be greatly appreciated.

    Thanks so much.

  • #73573
    Profile photo of Dirk
    Dirk
    Participant

    Ok, you'll need to put the paths into parenthesis, since it is used as an argument for a parameter in your script:

    $localPaths = ('c:\folder1\', 'c:\folder2\')
    

You must be logged in to reply to this topic.