How to use pypsrp - 10 common examples

To help you get started, we’ve selected a few pypsrp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jborean93 / pypsrp / tests / test_wsman.py View on Github external
def test_wsman_update_envelope_size_explicit(self):
        wsman = WSMan("")
        wsman.update_max_payload_size(4096)
        assert wsman.max_envelope_size == 4096
        # this next value is dependent on a lot of things such as python
        # version and rounding differences, we will just assert against a range
        assert 1450 <= wsman.max_payload_size <= 1835
github jborean93 / pypsrp / tests / test_powershell.py View on Github external
def test_psrp_run_protocol_version(self, wsman_conn):
        with RunspacePool(wsman_conn) as pool:
            if type(wsman_conn.transport).__name__ == "TransportFake":
                expected_version = \
                    wsman_conn.transport._test_name.split("_")[-1]
                actual_version = pool.protocol_version
                assert actual_version == expected_version

            ps = PowerShell(pool)
            ps.add_script('''begin {
    $DebugPreference = 'Continue'
    Write-Debug "Start Block"
    Write-Error "error"
}
process {
    $input
}
end {
    Write-Debug "End Block"
}''')
            # this tests the merge logic works on v2.1
            ps.merge_error("output")
            actual = ps.invoke(["message 1", 2, ["3", 3]])

            assert len(actual) == 4
github jborean93 / pypsrp / tests / test_spnego.py View on Github external
def test_gssapi_get_sec_context_auto_implicit(self):
        gss = pytest.importorskip("gssapi")

        name_type = gss.NameType.user
        mech = gss.OID.from_int_seq(GSSAPIContext._AUTH_PROVIDERS['auto'])
        spn = "http@hostname"
        username = None
        password = None
        delegate = False
        wrap_required = False
        cbt = None

        with pytest.raises(ValueError) as err:
            GSSAPIContext._get_security_context(name_type, mech, spn, username,
                                                password, delegate,
                                                wrap_required, cbt)
        assert str(err.value) == "Can only use implicit credentials with " \
                                 "kerberos authentication"
github jborean93 / pypsrp / tests / test_messages.py View on Github external
System.Object
                            
                            System.Timers.ElapsedEventArgs
                            
                                <dt>2009-06-17T10:57:23.1568275-07:00</dt>
                            
                        
                    
                
                
                
                
            
        '''
        serializer = Serializer()
        meta = ObjectMeta("Obj", object=UserEvent)
        actual = serializer.deserialize(xml, meta)

        assert str(actual.args[0]) == "System.Timers.Timer"
        assert actual.args[0].adapted_properties['Interval'] == 5000.0
        assert str(actual.args[1]) == "System.Timers.ElapsedEventArgs"
        assert actual.args[1].adapted_properties['SignalTime'] == \
            "2009-06-17T10:57:23.1568275-07:00"
        assert actual.computer is None
        assert actual.data is None
        assert actual.event_id == 1
        assert actual.runspace_id == \
            uuid.UUID("fb9c87e8-1190-40a7-a681-6fc9b9f84a17")
        assert str(actual.sender) == "System.Timers.Timer"
        assert actual.sender.adapted_properties['Interval'] == 5000.0
        assert actual.source_id == "ae6245f2-c179-4a9a-a039-47b60fc44500"
        assert actual.time == "2009-06-17T10:57:23.1578277-07:00"
github jborean93 / pypsrp / tests / test_powershell.py View on Github external
def test_psrp_disconnect_already_disconnected(self):
        wsman = WSMan("")
        rs = RunspacePool(wsman)
        rs.state = RunspacePoolState.DISCONNECTED
        rs.disconnect()
github jborean93 / pypsrp / tests / test_powershell.py View on Github external
def test_psrp_reset_runspace_state_fail(self, wsman_conn):
        pool = RunspacePool(wsman_conn)
        pool.open()

        try:
            with pytest.raises(InvalidPSRPOperation) as err:
                pool.reset_runspace_state()
            assert str(err.value) == "Failed to reset runspace state"
        finally:
            pool.close()
github jborean93 / pypsrp / tests / test_powershell.py View on Github external
def test_psrp_disconnect_invalid_state(self):
        wsman = WSMan("")
        rs = RunspacePool(wsman)
        with pytest.raises(InvalidRunspacePoolStateError) as err:
            rs.disconnect()
        assert err.value.action == "disconnect a Runspace Pool"
        assert err.value.current_state == RunspacePoolState.BEFORE_OPEN
        assert err.value.expected_state == RunspacePoolState.OPENED
        assert str(err.value) == \
            "Cannot 'disconnect a Runspace Pool' on the " \
            "current state 'BeforeOpen', expecting state(s): 'Opened'"
github jborean93 / pypsrp / tests / test_host.py View on Github external
$host.UI.RawUI.ScrollBufferContents($rectangle, $coordinates, $rectangle, $buffer_cell)''')
            actual = ps.invoke()

        assert len(actual) == 17

        assert str(actual[0]) == "White"
        assert str(actual[1]) == "Green"
        assert set_foreground_color.call_count == 1
        assert isinstance(set_foreground_color.call_args[0][0], RunspacePool)
        assert isinstance(set_foreground_color.call_args[0][1], PowerShell)
        assert set_foreground_color.call_args[0][2] == Color.GREEN

        assert str(actual[2]) == "Blue"
        assert str(actual[3]) == "Red"
        assert set_background_color.call_count == 1
        assert isinstance(set_background_color.call_args[0][0], RunspacePool)
        assert isinstance(set_background_color.call_args[0][1], PowerShell)
        assert set_background_color.call_args[0][2] == Color.RED

        assert str(actual[4]) == "1,2"
        assert str(actual[5]) == "11,12"
        assert set_cursor_position.call_count == 1
        assert isinstance(set_cursor_position.call_args[0][0], RunspacePool)
        assert isinstance(set_cursor_position.call_args[0][1], PowerShell)
        assert set_cursor_position.call_args[0][2].extended_properties['x'] \
            == 11
        assert set_cursor_position.call_args[0][2].extended_properties['y'] \
            == 12

        assert str(actual[6]) == "3,4"
        assert str(actual[7]) == "13,14"
        assert set_window_position.call_count == 1
github jborean93 / pypsrp / tests / test_powershell.py View on Github external
def test_psrp_connect_already_opened(self):
        wsman = WSMan("")
        rs = RunspacePool(wsman)
        rs.state = RunspacePoolState.OPENED
        rs.connect()
github jborean93 / pypsrp / tests / test_host.py View on Github external
host_ui.WriteLine1 = mock_write_line1
        host_ui.WriteLine2 = mock_write_line2
        host_ui.WriteLine3 = mock_write_line3
        host_ui.WriteErrorLine = mock_write_error
        host_ui.WriteDebugLine = mock_write_debug
        host_ui.WriteProgress = mock_write_progress
        host_ui.WriteVerboseLine = mock_write_verbose
        host_ui.WriteWarningLine = mock_write_warning
        host_ui.Prompt = mock_prompt
        # seems like PS never calls PromptForCredential1 so we will skip that
        host_ui.PromptForCredential2 = mock_prompt_credential
        host_ui.PromptForChoice = mock_prompt_choice

        host = PSHost(None, None, False, None, None, host_ui, None)

        with RunspacePool(wsman_conn, host=host) as pool:
            pool.exchange_keys()
            mock_read_line_as_ss.return_value = pool.serialize(
                u"ReadLineAsSecureString response", ObjectMeta("SS")
            )
            mock_ps_credential = PSCredential(username="username",
                                              password=u"password")
            mock_prompt_credential.return_value = mock_ps_credential

            ps = PowerShell(pool)
            ps.add_script('''$host.UI.ReadLine()
$host.UI.ReadLineAsSecureString()
$host.UI.Write("Write1")
$host.UI.Write([System.ConsoleColor]::Blue, [System.ConsoleColor]::White, "Write2")
$host.UI.WriteLine()
$host.UI.WriteLine("WriteLine2")
$host.UI.WriteLine([System.ConsoleColor]::Gray, [System.ConsoleColor]::Green, "WriteLine3")