使用 PyWinRM 将 powershell 脚本从 Linux 传输到 Windows
Transferring a powershell script with PyWinRM from Linux to Windows
我正在尝试将用 Linux 编写的 powershell 脚本传输到托管在 Azure 中的 Windows 机器。思路是将脚本复制到windows机器上执行。我正在使用 PyWinRM 来完成这项任务。 PyWinRM 中没有直接机制可以一次性传输文件。我们必须将文件转换为流,并在传输之前对文件进行一些字符编码,以便与 PowerShell 内联。详细解释 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)
现在,当我 运行 脚本时,我得到的输出是
$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:
这里的争论点是输出中的以下几行。
STDOUT: ='www.google.com'
= Test-Connection -ComputerName -Count 1 | Select -ExpandProperty IPV4Address
仔细看上面几行,和代码中的script_text字符串比较,你会发现变量名像$主机名、以 $ 键开头的 $ipV4 在传输到 windows 完成后丢失。
有人可以解释发生了什么以及如何解决它吗?
提前致谢。 :-)
使用带有单撇号而不是双引号的此处字符串。 Here-strings 也可以将 $var
替换为它们的值。
$s = @'
$hostname='www.google.com'
$ipV4 = Test-Connection -ComputerName $hostname -Count 1 | Select -ExpandProperty IPV4Address
'@ | %{ $_.Replace("`n","`r`n") }
也就是说,您的 Python 部分可能没问题,但在 Powershell 中执行的内容需要稍作改动。
我正在尝试将用 Linux 编写的 powershell 脚本传输到托管在 Azure 中的 Windows 机器。思路是将脚本复制到windows机器上执行。我正在使用 PyWinRM 来完成这项任务。 PyWinRM 中没有直接机制可以一次性传输文件。我们必须将文件转换为流,并在传输之前对文件进行一些字符编码,以便与 PowerShell 内联。详细解释 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)
现在,当我 运行 脚本时,我得到的输出是
$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:
这里的争论点是输出中的以下几行。
STDOUT: ='www.google.com' = Test-Connection -ComputerName -Count 1 | Select -ExpandProperty IPV4Address
仔细看上面几行,和代码中的script_text字符串比较,你会发现变量名像$主机名、以 $ 键开头的 $ipV4 在传输到 windows 完成后丢失。 有人可以解释发生了什么以及如何解决它吗? 提前致谢。 :-)
使用带有单撇号而不是双引号的此处字符串。 Here-strings 也可以将 $var
替换为它们的值。
$s = @'
$hostname='www.google.com'
$ipV4 = Test-Connection -ComputerName $hostname -Count 1 | Select -ExpandProperty IPV4Address
'@ | %{ $_.Replace("`n","`r`n") }
也就是说,您的 Python 部分可能没问题,但在 Powershell 中执行的内容需要稍作改动。