简体   繁体   中英

Transferring a powershell script with PyWinRM from Linux to Windows

I am trying to transfer a powershell script written in Linux to a Windows machine hosted in Azure. The idea is to copy the script to the windows machine and execute it. I am using PyWinRM to accomplish this task. There is no direct mechanism available in PyWinRM to transfer the file in one go. We will have to convert the file to a stream and do some character encodings for the file to be inline with PowerShell bfore transferring. For detailed explanation click here . The python script for streaming the file from Linux to windows goes as below

winclient.py

script_text  = """$hostname='www.google.com'
$ipV4 = Test-Connection -ComputerName $hostname -Count 1  | Select  -ExpandProperty IPV4Address
"""

part_1 = """$stream = [System.IO.StreamWriter] "gethostip.txt"
    $s = @"
    """
    part_2 = """
    "@ | %{ $_.Replace("`n","`r`n") }
    $stream.WriteLine($s)
    $stream.close()"""

    reconstructedScript = part_1 + script_text + part_2
    #print reconstructedScript
    encoded_script = base64.b64encode(reconstructedScript.encode("utf_16_le"))

    print base64.b64decode(encoded_script)
    print "--------------------------------------------------------------------"
    command_id = conn.run_command(shell_id, "type gethostip.txt")
    stdout, stderr, return_code = conn.get_command_output(shell_id, command_id)
    conn.cleanup_command(shell_id, command_id)
    print "STDOUT: %s" % (stdout)
    print "STDERR: %s" % (stderr)

Now when I run the script the output what I am getting is

    $stream = [System.IO.StreamWriter] "gethostip.ps1"
    $s = @"
    $hostname='www.google.com'
    $ipV4 = Test-Connection -ComputerName $hostname -Count 1  | Select -ExpandProperty IPV4Address
    "@ | %{ $_.Replace("`n","`r`n") }
    $stream.WriteLine($s)
    $stream.close()
   --------------------------------------------------------------------
   STDOUT: ='www.google.com'
   = Test-Connection -ComputerName  -Count 1  | Select -ExpandProperty IPV4Address


    STDERR: 
    STDOUT: 
    STDERR:

The point of contention here is the following lines in the output.

STDOUT: ='www.google.com' = Test-Connection -ComputerName -Count 1 | Select -ExpandProperty IPV4Address

Have a close look at the above lines and compare it with script_text string in the code, you will find the variable names like $hostname, $ipV4 starting with $ key are missing after the transfer to windows is completed. Can someone explain whats going on and how to resolve it ?? Thanks in advance. :-)

Use your here-string with single apostrophes instead of double quotes. Here-strings are also subjects to replacement for $var into their values.

$s = @'
$hostname='www.google.com'
$ipV4 = Test-Connection -ComputerName $hostname -Count 1  | Select -ExpandProperty IPV4Address
'@ | %{ $_.Replace("`n","`r`n") }

That is, your Python part is probably fine, but what's executed in Powershell needs to be altered slightly.

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