0

When I ssh to the windows machine it will not be in powershell prompt and I don't know how I'm going to execute the powershell script I have.

I tried the following code without success.

ssh_connect(ip1, " powershell.exe <mtu-read.ps1")

def ssh_connect(address, script):

    #key_filename1 = 'id_rsa' #no authentication for now testing
    host_user = "Administrator"
    try:
        process1 = subprocess.Popen("ssh -oStrictHostKeyChecking=no " + host_user + "@" + address + script, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output1 = process1.stdout.readlines()
        print(output1)
        return output1

    except Exception as er2:
        print(str(er2) + ' connection issue ')
        return "0"

The result I get is that it does not recognize the command in the powershell.

2
  • Honestly, the best way to do this is to have the python script download powershell as a string and execute it using IEX. You can host your powershell script on a webserver and serve it like that. Commented Jul 31, 2019 at 9:00
  • @AlexanderSinno It really isn't. Using Invoke-Expression should be for very select use-cases. The issue is OP's use of < Commented Jul 31, 2019 at 12:56

1 Answer 1

0

Your syntax is invalid in the call:

powershell.exe -File mtu-read.ps1

Note your working directory or the script file won't be found. I suggest fully-qualifying your script path as a best-practice.


As an aside, I question why you're using to execute instead of just writing a script and utilizing (native to Windows) instead of .

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

3 Comments

the files are not in the server i should point out that i wanted to feed them to the ssh connection using <
@nadeem You can't. You'd need to read the file content and use the -Command argument in that scenario while also dealing with all the cmd escaping rules. Alternatively, use base64 and -EncodedCommand
"OpenSSH has been added to Windows as of autumn 2018, and is included in Windows 10 and Windows Server 2019." link

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.