Skip to content

Instantly share code, notes, and snippets.

@cchitsiang
Forked from diyan/gist:2850866
Created June 9, 2015 03:45
Show Gist options
  • Save cchitsiang/e5e7e07771e255ed2f00 to your computer and use it in GitHub Desktop.
Save cchitsiang/e5e7e07771e255ed2f00 to your computer and use it in GitHub Desktop.

Revisions

  1. cchitsiang revised this gist Jun 9, 2015. No changes.
  2. @diyan diyan revised this gist Jul 5, 2012. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,7 @@ def remote_sh(target_host, command_text, ignore_error=False):
    $remote_session = New-PSSession -ComputerName {2} -Credential $credentials;
    Invoke-Command -Session $remote_session -ScriptBlock {{ {3} }};
    $remote_last_exit_code = Invoke-Command -Session $remote_session -ScriptBlock {{ $LastExitCode }};
    Remove-PSSession -Session $remote_session;
    exit $remote_last_exit_code\"""".format(
    target_env.login,
    target_env.password,
    @@ -34,4 +35,4 @@ def remote_sh(target_host, command_text, ignore_error=False):
    'but only zero value is expected. '
    'Script block is "{2}".'.format(target_host, error_code, command_text))

    return error_code
    return error_code
  3. @diyan diyan created this gist Jun 1, 2012.
    37 changes: 37 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    # Note that target_env.login and target_env.password is global variables

    # Maybe I should add this into Fabric project (http://docs.fabfile.org/en/1.4.2/index.html).
    # This is complicated task for sure but it would be nice if Fabric could use ssh under Linux and PowerShell Remoting under Windows.

    def remote_sh(target_host, command_text, ignore_error=False):
    print('run PowerShell script block at {0}: {1}'.format(target_host, command_text))

    command_text = command_text.replace('"', '\'')

    # -NoProfile -NonInteractive PowerShell parameters decrease execution time
    power_shell_text = """\
    powershell -NoProfile -NonInteractive
    \"$secure_password = ConvertTo-SecureString '{1}' -asPlainText -Force;
    $credentials = New-Object Management.Automation.PSCredential('{0}', $secure_password);
    $remote_session = New-PSSession -ComputerName {2} -Credential $credentials;
    Invoke-Command -Session $remote_session -ScriptBlock {{ {3} }};
    $remote_last_exit_code = Invoke-Command -Session $remote_session -ScriptBlock {{ $LastExitCode }};
    exit $remote_last_exit_code\"""".format(
    target_env.login,
    target_env.password,
    target_host,
    command_text)

    power_shell_text = dedent(power_shell_text).replace('\n', ' ')

    # This print call could be uncommented for debugging purposes
    # print('run shell command: {0}'.format(power_shell_text))

    error_code = os.system(power_shell_text)

    if error_code and not ignore_error:
    raise Exception('Failed to execute PowerShell script block on host {0}. Actual return code was {1} '
    'but only zero value is expected. '
    'Script block is "{2}".'.format(target_host, error_code, command_text))

    return error_code