Need guidance server to server

You definitely do. :wink: You may restart with explaing what you’re actually trying to achieve. Try to explain the bigger picture - not the way you think you have to go to come to this bigger picture.

Depending on the content of your zip files you may need to extract each zip file to its own destination folder. If so need to create them before you run Expand-Archive.

yes

pls allow me to explain;
i have a daily ftp transfer which provide a zip folder with updates by folder name & date
what i do manually now is extract that protected zip folder into my server and execute a exe file which takes these data and insert into my sql db.

so i have sorted the ftp file transfer automated to my ftp folder via batch script

now the second half of
copy from ftp folder to local server unzipped content into specific folder and run exe and send me email that it has been successful or not…does this make sense?

That actually raises more questions than it answers.

You get a daily file transfer. OK.
Is it only one file per day or more?
If it’s more than one file - could these files be imported at once or do they have to be imported separately?
If they have to be imported separatly does the order matter?
When you start your executable for the import - do you specify a particular file or will it grab the file or files from a given folder?
Does the import executable exits by itself when it’s finished?
Will it output a return code if the import was successful or not?
Depending on your answers, more questions may arise.

Is it only one file per day or more? Yes only 1 zip file a day named ending by date

When you start your executable for the import - do you specify a particular file or will it grab the file or files from a given folder? it will grab files from the given folder

Does the import executable exits by itself when it’s finished? yes its exits automatically .

Will it output a return code if the import was successful or not? it just generates a log file of the progress output time it took to process

So what’s actually the problem then?

  1. You have a folder with 1 file. The name does not matter if it’s just one file anyway. You get the file with Get-ChildItem specifying the ftp download folder and save the metadata in a variable.
  2. You create a new temporary folder using the name property of the metadata object of the zip file.
  3. You extract the zip file with Expand-Archive to this newly created folder.
  4. You start your import executable pointing to the extraction folder and wait for termination of the executable.
  5. You parse the log file for the result you’re after
  6. You send the email with the status
  7. you clean up the zip file - or you move it to a backup folder for savety reasons - and the temporarily created extraction folder
    … and you’re done.

Do ONE Step at a time. When this one step works as expected move on to the next.

1 Like
  1. You have a folder with 1 file. The name does not matter if it’s just one file anyway. You get the file with Get-ChildItem specifying the ftp download folder and save the metadata in a variable.
  • this is where the issue is, it keeps 1 weeks files and get over written so thats where the wildcard doesnt work as you mentioned earlier…

for item 1-3 i presume it would be something like this, please correct me if im wrong;

Get-ChildItem '\\x.x.x.x\SanctionList\JAG01TD_WorldCompliancePlus (date).Zip' -Filter *.zip | Expand-Archive -DestinationPath 'D:\Omni_DataMart_Lexisnexis\DataSource' -Force

That’s what I meant when I said

You say “… it keeps 1 weeks files …” … what is " it "? And why does it keep the files? And when the files have a date in their name why do they get overwritten?

It depends pretty much on the zip file. If it has a folder structure in it you could use this, but the extracted files will always end up in the folder you specified.

let me try editing tonight the rsync script of the folder to delete source that way I would only receive latest file, and only remain the latest single file from source…that may help my situation I presume

That’s actually not necessary. PowerShell is easily able to grab only the newest file from a folder but because I don’t know your environment I couldn’t know that you need to take care about this detail. That’s what I meant with

I know it might be anoying but since I cannot see your screen and I cannot read you mind I cannot recommend to deal with something I don’t know of.

Here’s another suggestion combining information you gave and infomration I assumed because you did not provide yet:

$FTPDownloadFolder = '\\x.x.x.x\SanctionList'
$DestinationRoot = 'D:\Omni_DataMart_Lexisnexis\DataSource'

Get-ChildItem -Path $FTPDownloadFolder -Filter 'JAG01TD_WorldCompliancePlus*.Zip' -File |
    Sort-Object -Property LastWriteTime |
        Select-Object -Last 1 |
            ForEach-Object {
                $DestinationPath = Join-Path -Path $DestinationRoot $_.BaseName
                if (-not (Test-Path -Path $DestinationPath)) {
                    New-Item -Path $DestinationPath -ItemType Directory |Out-Null
                }
                Expand-Archive -Path $_.FullName -DestinationPath $DestinationPath

                <# Assuming your executable file can take a folder as parameter where 
                it expects the files to import #>
                & 'Path to your executable' $DestinationPath

                <# Here you could parse the log file and extract the info you're after.
                When finished you can clean up the temporarily created folder #>
                
                Remove-Item -Path $DestinationPath -Recurse -Force
            }
1 Like

Hi Olaf,

This works but do allow me to explain what happens,

The file being copies is password protected so unable to extract it completely, and the exe file immediately executes despite the file not unzipped. It extracts into a folder instead of the content to be dumped into destination, and this error below is thrown;

Exception calling "ExtractToFile" with "3" argument(s): "The archive entry was compressed using an unsupported compression method."
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:1059 char:25
+ ...             [System.IO.Compression.ZipFileExtensions]::ExtractToFile( ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidDataException
 

So I see three options: Get the zip without password protection, extract the zip beforehand manually and only automate the rest or use a third party tool like 7-Zip what’s able to extract password protected files from the command line.

There is a 7zip4Powershell module which seems to support extracting password protected files using cmdlet:

Expand-7Zip -ArchiveFileName [FILENAME] -Password [PASSWORD]

So option four is to install that and and import the module in your script.