shell脚本从windows上传到linux用什么编码,使用PyWinRM将powershell脚本从Linux传输到Windows...

我正在尝试将用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以$键开头的变量名丢失。

有人能解释一下发生了什么事以及如何解决它吗??

提前谢谢。:-)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值