btamulis 12/1/2021 10:22:55 AM

PowerShell Assistance - SmartConnect

First off - not a developer - just a working-class consultant trying to add value.

My client has complex Smart Connect Object for integrating Sales Invoices. It works great - was designed to download a source file of invoices from different billing system (FTP Site) - validate the dataset - then find the GP CustomerID (not provided in source file we have to verify it exists) - then it populates a staging table (with data from source file and the correct GP CustomerID) then it finally after all the preparation integrates the data. 

They have decommissioned the FTP site  - replacing with an internal site. 

Since the object is so complex - I would prefer to simply change the powershell and theoretically all the other stuff should still work. 

The very first task is to download a source file from a WinSCP site.

Here's the code - I need to modify to download a file stored on a separate internal server (10.50.11.41\APFMFN\newest file date that starts with billings) - no password/credentials - windows user will have permissions

# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "66.129.101.73"
    UserName = "APFMFN"
    Password = "2@35Qu!7AC"
    SshHostKeyFingerprint = "ssh-rsa 2048 ywyt75fPTy3unlIuWI7x3k4bUct218VuzIaUn4yyHjM="
}


# Connect
Write-Host "Connecting..."
$session = New-Object WinSCP.Session
$session.SessionLogPath = "\\USW2GPAPP01\GPSHARE\SC_Import\FTP\incremental_download.log"
$session.Open($sessionOptions)


# Download files
$transferResult =
     $session.GetFiles("/*.csv", "\\USW2GPAPP01\GPSHARE\SC_Import\Source\*")
# Process source files
foreach ($transfer in $transferResult.Transfers)
{
    # Success or error?
    if ($transfer.Error -eq $Null)
    {
        $newName =
            "/" +
            #(Get-Date -Format "yyyy-MM-dd-hh-mm-ss") + "_" +
            [IO.Path]::GetFileName($transfer.FileName) + ".processed"
        Write-Host (
            "Download of $($transfer.FileName) succeeded, renaming to backup $newName")
        $session.MoveFile($transfer.FileName, $newName)
    }
    else
    {
        Write-Host (
            "Download of $($transfer.FileName) failed: $($transfer.Error.Message)")
    }
}

Thanks in advance - very little experience with bat files and powershell 

Version: All
Section: Dynamics GP, eConnect, SmartConnect


Table Definition Quick Links
All Tables
SOP Tables
RM Tables
GL Tables
POP Tables
HR Tables
PM Tables
UPR Tables
IV Tables
Olympic Tables
3