2

I am trying to write a powershell script which call the .ftp file (www.scriptftp.com). When Powershell call the .ftp script file which is use to upload a zip file to ftp server

here is my upload.ftp file code

OPENHOST("www.abcd.in","sdfdsfs","pwd")
CHDIR("/dirctory/from/user/")

$result=PUTFILE("D:\MyZipTest.zip")

IF($result!="OK")
    PRINT("ERROR in GetFile, exiting")
    # Exit with error code 1
    EXIT(1)
ELSE
    EXIT()
    PRINT("File uploaded successfuly")
END IF

CLOSEHOST

In Above code i return EXIT(1) or EXIT(0) to calling method ie powershell script file

my powershell script file code:

Invoke-Item "D:\USER\Raj\BLACKswastik DEV\FTPScript\Upload1.ftp"

Start-Sleep -Second 15

echo $lastexitcode

echo $?

here i want to fetch exit code which return by .ftp file script so i use $lastexitcode and $? but both of them not showing me proper results. IS there any other way to fetch the exit code of child process please suggest me.

2 Answers 2

4

Try using Start-Process. You can get back a Process object that you can query later for an exit code e.g.:

$ftpPath = "D:\USER\Raj\BLACKswastik DEV\FTPScript\Upload1.ftp"
$p = Start-Process <path_to_scriptftp.exe> -Wait -PassThru -ArgumentList $ftpPath
$p.ExitCode

The problem with using Invoke-Expression (or launching the exe directly) is that you're launching a Windows subsystem EXE and unlike a console subsystem exe, Invoke-Expression will return right after the exe has launched (and before it exits). Invoke-Item also returns before the Windows EXE has exited.

Sign up to request clarification or add additional context in comments.

Comments

0

Use

Invoke-Expression -Command "D:\User\...\Upload1.ftp"

instead of

Invoke-Item "D:\User\...\Upload1.ftp"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.