sion_corn
sion_corn

Reputation: 3141

integrating FTP request in powershell script instead of calling .CMD file

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

Upvotes: 0

Views: 942

Answers (1)

Adil Hindistan
Adil Hindistan

Reputation: 6605

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.

Upvotes: 1

Related Questions