Reputation: 269
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.
Upvotes: 2
Views: 3422
Reputation: 201652
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.
Upvotes: 4
Reputation: 200273
Use
Invoke-Expression -Command "D:\User\...\Upload1.ftp"
instead of
Invoke-Item "D:\User\...\Upload1.ftp"
Upvotes: 0