我正在尝试将用Linux编写的powershell脚本传输到Azure中托管的Windows计算机。其思想是将脚本复制到windows机器上并执行它。我使用PyWinRM来完成这个任务。PyWinRM中没有直接机制可以一次性传输文件。在传输之前,我们必须将文件转换为流,并对文件进行一些字符编码,以便与PowerShell bfore进行内联。详细解释click here。将文件从Linux流式传输到windows的python脚本如下所示
在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)
现在,当我运行脚本时,我得到的输出是
^{pr2}$
这里的争论点是输出中的以下几行。在STDOUT: ='www.google.com'
= Test-Connection -ComputerName -Count 1 | Select -ExpandProperty IPV4Address
仔细看看上面的几行代码,并将其与代码中的script_text字符串进行比较,您会发现像$hostname,$ipV4以$键开头的变量名丢失。
有人能解释一下发生了什么事以及如何解决它吗??
提前谢谢。:-)