Does PSCP work with PowerShell? - windows

I have a PowerShell script that produces a text file. At the end, I would like to copy this file to a Linux server.
From CMD.EXE, I can use PSCP (from Putty), it works and copies the file.
But from PowerShell, either interactively or from a PowerShell batch, PSCP has no visible effect: no error messages and the file is not copied.
Even if I run simply .\PSCP.EXE without arguments, on the CMD command line it displays the options, but from PowerShell it does nothing.
Can PSCP be used from inside PowerShell?

Executing a program from within PowerShell should work identically to CMD, but depending upon how that program produces its output (does it write to STDOUT, STDERR, other?) that may behave differently.
I've been using Rebex's components for FTPS & SFTP within .NET apps & PowerShell scripts; the SFTP package includes an SCP class. Yes, it costs money, but depending upon your usage it may be worthwhile.

Just attempted to automate PSCP from PowerShell. Remember to use pscp's -batch parameter so that, should you do something like enter the wrong password, you won't get asked for input.
$Cmd = "pscp -l username -pw password -batch c:\folder\file.txt server:/home/user1"
Invoke-Expression "& $( $Cmd )"
Otherwise your script will just grind to a halt.

Yes - most any executable can be called from PowerShell. There isn't anything peculiar about pscp.exe in this regard. You may need to preface it with the call operator - the ampersand - &:
PS C:\>& "C:\Program Files (x86)\Putty\pscp.exe" -V
pscp: Release 0.62
The above is direct output from my PowerShell prompt. The call operator is particularly helpful if the path to your executable contains spaces - the call operator is used to tell PowerShell to treat what would be considered a string as something it should try to execute instead.
Please include the full command your are trying to execute as it will help in providing a better answer. You may have a problem with your PATH variable or something else weird if you don't get any output.

If using pscsp from inside a script, e.g. perl
no ampersand
quote like this "my password"
e.g.
"C:\Program Files\Putty\pscp.exe" -C -p -pw "password" /local_dir/file_to_copy user#hostname:/remote_directory
in perl (beware that \ is an escape char in a "string" )
$cmd = q("C:\Program Files\Putty\pscp.exe" -C -p -pw "password" /local_dir/file_to_copy user#hostname:/remote_directory);
system($cmd);

Related

execute powershell commands with Lua

I have a program that I work with, that has an onboard lua compiler to allow for custom written actions.
Since the tool itself is very limited, especially if it goes for complex reactions over networks, I want to use Powershell over lua.
Methods like os.execute() or io.popen() use the standard command line from windows and not Powershell.
Is there a way to use Powershell with lua?
I tried to write a command line script with the Powershell editor and run this script with os.execute, but it opens it as a textfile, it would be better to write the commands directly in lua but if there is no other way, executing a Powershell script directly would also be fine. (In Windows itself you can execute the script with right mouse "click/Execute with Powershell")
-- You can generate PowerShell script at run-time
local script = [[
Write-Host "Hello, World!"
]]
-- Now create powershell process and feed your script to its stdin
local pipe = io.popen("powershell -command -", "w")
pipe:write(script)
pipe:close()
Your description of the problem makes it sound like you're using a command such as os.execute("powershellscript.ps1"), and that call invokes cmd.exe with your string as the proposed command line. Normally, Windows will open a .PS1 file for editing; this was a deliberate decision for safety. Instead, try altering the os.execute() command to explicitly call PS: os.execute("powershell.exe -file powershellscript.ps1"). If you need to pass parameters to your script, enclose them in {}. See https://msdn.microsoft.com/en-us/powershell/scripting/core-powershell/console/powershell.exe-command-line-help for more info on invoking PowerShell from the command line.

execute user inputed Windows (or bash) commands from batch (or bash) file?

Ok, so I have this batch script and what I want to happen is that when you run the script it does some standard stuff like change the path and access files etc... but after it's done that it goes back to being a normal cmd prompt/terminal where I can type in commands at free will.
Can this be done (in either dos or bash)? Is there like an execute command that I can put in an internal while loop or is there a command where when the scirpt ends it can go back to the normal cmd/terminal?
Do you need a full bash prompt?
Or would something like this be enough?
#!/bin/bash
echo -n "Enter cmd: "
read COMMAND
echo ${COMMAND} | bash
Also, in a script, you can just execute bash and get a full prompt in the current environment.
In Dos / windows command prompt if you run the batch file from command line you will get the prompt back always by default. Just like running any other command in command prompt.
Also in windows when the batch file execution is complete you can just put Cmd.exe when everything has finished running I.e at the end of the batch file.
Hope this helps!
E.g
#echo off
Echo running
.
.
.
Cmd.exe
Or even at the end
Echo %command% | Cmd.exe
Never mind, I got a good solution using this code: (for windows)
set /p command=CMD:
%command%

how to ftp remote server through running a shell script

ftp ipaddress
bin
hash
cd path
get filename
quit
i want these line to be executed in a shell script only first line is executing after entering username and password the rest line are not executing and the control is stuck at ftp> prompt
Once your script invokes the program ftp, the shell loses control until the ftp program finishes. So you need to use some new technique. One way is with the program expect, which you can get a hint about here: https://stackoverflow.com/a/12598169/4323 . Another way is to use a more "scriptable" FTP client, such as lftp on Linux, which has specific features to enable the sort of scripted use you're going for.
You need to use a non-interactive FTP client if you want to do this in a script rather than an interactive shell. ncftp is one you could check out.
Or you can use a shell "here" document:
#!/bin/bash
ftp -in <<EOS
user pswd
bin
hash
cd path
get filename
quit
EOS
The here document sends everything via the open program's stdin, just as if you typed it in at the command line. Note that ftp clients can be finicky and each one seems to have it's own set of gotchas, so some experimentation and use of man ftp will likely be required.
Looks like this has come up before on Stackoverflow, you might want to read through
how to ftp multiple file using shell script
IHTH

[ncftp]how to pass value to command into a bash script

I have a bash script and I want to use ncftp to do something. if I have this:
#!/bin/sh
HOST='my_IP_FTP_HOST'
USER='username'
PASSWD='password'
ncftp -u $USER -p $PASSWD $HOST <<END_SCRIPT
pwd
quit
END_SCRIPT
I get this error:
Syntax error in parameters or arguments
I don't understand why.
If I give it only the value and not the variables it works...
if I launch
$ sh -x script.sh
I get:
+ HOST=$'xxx.x.xx.xx\r'
+ USER=$'username\r'
+ PASSWD=$'password\r'
+ ncftp -u $'username\r' -p $'password\r' $'xxx.x.xx.xx\r'
NcFTP 3.2.1 (Jul 29, 2007) by Mike Gleason (http://www.NcFTP.com/contact/).
Welcome to FTP server
Syntax error in parameters or arguments
hmmm.... \r creates problems sure.
Are you editing this file on Microsoft Windows? It doesn't works because the original file uses Windows carriage returns, which are passed to variables and then to ncftp, which then halts.
Fix it by using another text editor, or by using dos2unix. Ideally you'd first use dos2unix once and then change the text editor.
Alternatively (but really, really not recommended) you could just parse the variables and remove the extra \r, like below, especially when the problem arises at some input reading, which doesn't seems to be the case here.
HOST=${HOST%\\r}
USER=${USER%\\r}
PASSWD=${PASSWD%\\r}
Take care when copying text from Windows, or when editing without appropriate software.

How to tell if vim is being run in command line vs. powershell

I would like to make a function which does the following
if (vim is running in powershell)
call system("only works in powershell")
else
echo "Skipping powershell command"
Does anyone know how to tell what program vim is being run from?
EDIT: echo &term outputs "win32" in both cases.
From what I see you don’t need to check what vim was run from. You need to check the value of &shell because vim will run command in powershell if &shell option tells it to run command in powershell. If name of the shell is posh then checking may be done with
if stridx(&shell, 'posh')!=-1
call system('only works in powershell')
else
echo 'Skipping powershell command'
endif
. But I would rather suggest to run powershell directly with something like
call system('posh -c '.shellescape('only works in powershell'))
(note: I am not sure about what 'posh -c ' string should actually look like) or temporary set &shell option:
let savedsh=[&shell, &shellcmdflag, &shellxquote, &shellxescape, &shellquote, &shellpipe, &shellredir]
set shell=posh shellcmdflag=-c shellxquote=\" shellquote=\" shellpipe=> shellredir=>
try
call system('only works in powershell')
finally
let [&shell, &shellcmdflag, &shellxquote, &shellxescape, &shellquote, &shellpipe, &shellredir]=savedsh
endtry
(again, not sure about exact values of options): these ways command will always run in powershell.
You can check $term. For example on my cygwin and git bash, I get $term as cygwin.
For Powershell, you may set in your profile the following and check against that:
$env:TERM = "posh"
Note that &shell will cmd.exe for both cmd and powershell.

Resources