0

Right now I have a powershell script that is callign an FTP request bundled in a CMD file.

It works, but I want to integrate the FTP request in the powershell script because it'll give me 1 less file to keep track of.

So far I have tried doing this, but it produces an error (see below).

# run the command script to extract the file
#defines command to be run
$command = '
@echo off
setlocal
set uname=name
    set passw=pass
    set hostname=hstname
    set filespec=spec
echo %uname%>                     test.ftp
echo %passw%>>                    test.ftp
echo cd DIRNAME>>                    test.ftp
echo binary>>             test.ftp
echo get %filespec%>>             test.ftp
echo bye>>                        test.ftp
ftp -s:test.ftp %hostname%
if errorlevel 1 pause
endlocal
'
# runs command
iex $command

Error:

Invoke-Expression : The splatting operator '@' cannot be used to reference variables in an expression. '@echo' can be used only as an argument to a command. To reference variables in an expression use '$echo'. At Dir\File.ps1:32 char:4 + iex <<<< $command + CategoryInfo : ParserError: (echo:String) [Invoke-Expression], ParseException + FullyQualifiedErrorId : SplattingNotPermitted,Microsoft.PowerShell.Commands.InvokeExpressionCommand

I also tried changing the script to $echo but it produces the following error:

Invoke-Expression : Unexpected token 'off' in expression or statement. At Dir\File.ps1:32 char:4 + iex <<<< $command + CategoryInfo : ParserError: (off:String) [Invoke-Expression], ParseException + FullyQualifiedErrorId : UnexpectedToken,Microsoft.PowerShell.Commands.InvokeExpressionCommand

1 Answer 1

1

Alex,

This may not be exactly what you want but here is how I could make it work.

Once you have the commands, pipe all of them to create a batch file. For example:

Set-Content -path $env:temp\mycommands.cmd -value $command

Then, execute that file

iex "cmd /c $env:temp\mycommands.cmd"

The reason what you are doing is not working is because each of these lines are still getting interpreted by PowerShell and some of them have meanings in PowerShell that is not matching what is in CMD shell.

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

6 Comments

right, but i thought the CMD prompt could be saved to a string and executed thru the cmd line with iex $command - can you please explain why this does not work? what should i do instead? i am looking up how to write a powershell FTP request, but i was hoping to have a quick-n-dirty solution in the meantime...
Oh of course, it is still executing it in PowerShell context... Let me actually look into it and post a proper response once I find it. Need some coffee.
@AlexV, OK I am updating the answer I came up with that I hope it helps until you get the ftp pieces done within the PS.
+1 for effort. I cannot accept this, because I need to find a way to incorporate the FTP request in the PowerShell script without using or producing external files.
In that case, you really need to look at .NET class like System.Net.WebClient and use its methods DownloadData() or DownloadFile() depending on your intention. You just cant use the syntax you used for CMD as is in PowerShell but effectively achieve the same thing.
|

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.