2017-04-25 11 views
0

Linuxで書かれたpowershellスクリプトをAzureでホストされているWindowsマシンに転送しようとしています。考え方は、スクリプトをWindowsマシンにコピーして実行することです。私はPyWinRMを使ってこの作業を行っています。 PyWinRMでは、ファイルを一度に転送するための直接的なメカニズムはありません。ファイルをストリームに変換し、転送する前にファイルがPowerShellでインライン化されるようにするために、いくつかの文字エンコーディングを行う必要があります。詳細な説明はclick hereです。私は、スクリプトを実行すると、私は取得していますどのような出力がポイントPyWinRMを使用したPowerShellスクリプトのLinuxからWindowsへの転送

$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: 

ある

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) 

winclient.py

次のようにLinuxからWindowsにファイルをストリーミングするためのPythonスクリプトが行きますここで競合するのは出力の次の行です。

STDOUT:= 'www.google.com' =テスト接続-ComputerName -Count 1 | -ExpandProperty IPv4Addressを

を選択し、上記の行をよく見を持っており、コード内script_text文字列と比較し、あなたが$キーで始まる$ホスト名、$ IPv4ののような変数名を検索しますウィンドウへの転送が完了した後に欠落しています。 誰かが何が起こっているのか、それを解決する方法を説明できますか? ありがとうございます。 :-)

+0

質問に対する回答は必ずしも必要ではありませんが、LinuxでPowerShellを実行しようとしましたか? https://github.com/PowerShell/PowerShell – lit

+0

この場合は、Windowsマシンでタスクを実行することです。ここでの関心は、Windows-Linux通信であり、powershellではありません。とにかくそれは良い情報です。私は試してみますそれは確かに.. –

答えて

3

ここでは、二重引用符ではなくアポストロフィを使用します。ここでは、文字列は$varの値に置き換えられます。

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

つまり、あなたのPythonの部分はおそらく大丈夫ですが、Powershellで実行されるものは少し変更する必要があります。

+0

働く.. !!!大変ありがとうございます...あなたはただ素晴らしいです。 –

関連する問題