简体   繁体   中英

how to excute powershell script on remote machine using ssh in python

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM