How to run a cmd line application in PowerShell - powershell

I want to run this command
telnet -a servername port
-CASServer V2.00 connection from 0.0.0.0 [] Diag mode only
status
Clients: Static 0/50 Dynamic 19/50
Application1: 1 0xcb4388 hSock(0x2d4)
Application2: 0 0
OK
I am basically only interested in "Dynamic 19/50". If possible, I'd like to get this information from PowerShell

The PowerShell script at http://community.spiceworks.com/scripts/show/1887-get-telnet-telnet-to-a-device-and-issue-commands might work for what your're trying to accomplish. It creates a PowerShell function called 'Get-Telnet'; it can establish a telnet connection and send a series of commands.
Your usage would be something like:
Get-Telnet -RemoteHost [servername] -Port [port] -OutputhPath results.txt
After which you could parse the data you need (e.g., the 'Clients' line) from 'results.txt'.

In order to run a cmd line application in PowerShell, specifically the telnet command, you first need to install telnet.
Use this command, with admin privs:
pkgmgr /iu:"TelnetClient"
After installing you can run your telnet -a servername port command in PowerShell:
Running a command line application (telnet) in PowerShell

I am not positive on what exact command line application you are trying to run. However here is an example of running a command line application in powershell.
&'C:\Program Files\gs\gs9.14\bin\gswin64c.exe' -sDEVICE=tiffg4 -dBATCH -dNOPAUSE -q -r600 "-sOutputFile=$OutputFullDirectory" "$InputPath"
This would be ghostscript however the key is using the & symbol to call the command line and then enter in the path to the exe file you want to run then you can pump in the parameters. Hope this helps lead you in the right direction

Related

How to execute ssh connect by script?

I have local Windows 10 and remote Ubuntu server.
I want to automate connection to server and write executable script witch connects by ssh to server and open new terminal from another server.
What it's supposed to look like
I double click on bat
And then script
inits ssh connect
writes password
gives the user a terminal with a ready ssh connection.
That is, it mimics the following
Problems
How to wait ssh password request? All commands executes immediately.
(additional) can I write it in .sh script, run script, execute all in "start" terminal (from which I run .sh script) and then pass ssh control to invoked terminal?
It's best if someone writes a ready-made script
Automatically enter SSH password with script
Answers:
Direct answer - use expects. But sshpass is better. Also RSA-key can be used.
Can`t tell anything.
Can be done without any 3rd party tools like this:
$env:TMPPW=Get-Content -Path 'secure_file.txt' ; $un='MyUserName'
$j=Start-Job -ScriptBlock{Start-Sleep -Seconds 1
(New-Object -ComObject wscript.shell).SendKeys("$env:TMPPW{ENTER}")}
& ssh.exe -q -4 -l $un 127.0.0.1 'whoami'
$env:TMPPW=([guid]::NewGuid()).Guid ; $env:TMPPW=$null

ttpmacro doesn't run windows built in tftp

I tried to upload test.txt using ttpmacro.
This is what I did in the ttl file
exec 'cmd /k tftp -i 100.1.1.1 put E:\tftp\test.txt'
And also tried, the following.
exec 'tftp -i 100.1.1.1 put E:\tftp\test.txt'
PC(100.1.1.1) already up and running tftp server and tftp client also installed in my windows 10 machine. but cmd is oppened without running tftp.
output is something like "tftp is not executable file or batch."
In fact, if I open cmd on windows start menu and copy the same tftp command, it works perfectly.
Anybody knows how I can execute this?
Thank you.
You can't execute the tftp command from the tll macro without elevated privileges, to solve use your macro to execute a .bat file which contains the RUNAS command, detailed below.
RUNAS /profile /user:[your username] "tftp -i 100.1.1.1 put E:\tftp\test.txt"
note - this will execute the tftp as administrator and if the current user has a password, they may be prompted to enter it.

Noninteractive Remote Commands with Powershell

I am developing a script which uses ssh to connect to a windows host, run a powershell command, and parse the output. While I can connect to the host and run the command, powershell will not exit and return control back to the local script until I press the enter key.
At the moment, the specific command being run is ssh HOSTNAME 'echo $(hostname)' and the ssh server is configured to pass remote execution requests to powershell -noninteractive -command CMD, where HOSTNAME is the name of the windows host and CMD is the remote command to be run (in this case echo $(hostname)).
The end goal is to have the script which is calling remote powershell commands to run completely noninteractively, but this is currently impossible as the powershell command will not run noninteractively.
How do I get powershell to run remote commands noninteractively?
I've resolved the issue. While I haven't determined what was causing the issue, I can at least overcome the issue at hand by redirecting stdin to /dev/null on the side that initiates the ssh connection.
$ ssh HOSTNAME "CMD" </dev/null
This solution doesn't involve powershell at all, but rather treats the symptoms from the other side of the connection.

executing win32ole script on remote windows machine through telnet

I am trying to create a word document on a remote windows machine. What I am trying is to telnet to the remote windows machine and run a perl script that creates word document through Win32::OLE. But it doesn't seem to work. Is this possible? Because my script has {visible} set to 1 but will that telnet session have access to instances of word application? Atleast I tried it didn't work.
Telnet may not be the best tool to accomplish this, I'm not sure what kind of permissions it has. I recommend using PsExec, which allows remote command execution on windows servers. If it works locally, it will work using PsExec.
For example:
PsExec.exe \\remotecomputer -u userName -p Password Perl C:\path\to\file\file.pl
You can use the -s flag to run as system account, and the -i flag to run it interactively on the desktop. Without the -i flag, it will run in the console session.

How does PUTTY/PLINK determine a command has returned?

Is it newline? prompt? What exactly?
Trying to run powershell over plink and the command executes but plink doesn't recognise its finished and the session hangs. Most curiously though, the command executes successfully when sent through the shell (via Putty). However, when sent via plink, the same command hangs...
Any ideas?
Telnet is nearly a raw TCP connection. All Putty needs back is a response from the server. The rest is controlled by the shell and SSH/Telnet server.
While your task is running, it's not going to return a command prompt.
On Linux, Unix, and Mac OS X you could put a & after the command to run it in the background and return to the command prompt.
Try running it in the local terminal/command shell. You should basically see the same thing.
Ok, well I'm still not quite sure what the problem is, but I've found a workaround via the TeamCity forums.
Basically you want to echo some abitrary string and pipe that output into your powershell executable, like thus:
echo 'executing powershell...' | C:\windows\system32\windowspowershell \v1.0\powershell.exe exit 1
So then your full plink command becomes:
plink.exe user#someIp -i key.ppk -P 22 -batch -v "echo 'executing powershell...' | C:\windows\system32\windowspowershell\v1.0\powershell.exe exit 1"
Nb. Plink will still pass through return codes and console output using this method.
Link to TeamCity forum:
http://youtrack.jetbrains.net/issue/TW-6021
Hope this helps
I had the same problem with an other program. I used the >&2 (redirect output to std err) after the last command, this worked fine for me.
Just add "return XX" into remote shell script, it will be return value to local console. After plink has been finished, type echo %errorlevel% to see return code XX.