jborean93 / pypsrp

PowerShell Remoting Protocol for Python
MIT License
324 stars 49 forks source link

configure pypsrp for settings like WSManStackVersion #172

Open mabangde opened 1 year ago

mabangde commented 1 year ago

How to configure pypsrp for settings like WSManStackVersion in PowerShell, for example, the following configuration? image

$secureString = ConvertTo-SecureString -String "Pwd" -AsPlainText -Force $UserCredential = New-Object System.Management.Automation.PSCredential -ArgumentList "lab\john", $secureString $version = New-Object -TypeName System.Version -ArgumentList "2.0" $mytable = $PSversionTable $mytable["WSManStackVersion"] = $version $sessionOption = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck -ApplicationArguments @{PSversionTable=$mytable}

jborean93 commented 1 year ago

I'm not sure how you would configure that normally but the same command you've written in PowerShell would look like something like this in Python

from pypsrp.powershell import PowerShell, RunspacePool
from pypsrp.wsman import WSMan

with WSMan(...) as wsman:
    pool = RunspacePool(wsman)
    pool.open({"PSVersionTable": {"WSManStackVersion": "2.0"}})
    with pool:
        ps = PowerShell(pool)
        ...

This may or may not work as the version is just a string here and not a Version object. It can technically be done to create the required type but it's quite complex.

The newer psrp namespace (which is part of the 1.0.0b1 release) makes things a bit easier than before to do what you want.

import psrp
import psrpcore.types

wsman_conn = psrp.WSManInfo("hostname", ...)
app_args = {
    "PSVersionTable": {
        "WSManStackVersion": psrpcore.types.PSVersion("2.0"),
        ... # Whatever else you need to define.
    }
}

with psrp.SyncRunspacePool(wsman_conn, application_arguments=app_args) as rp:
    ps = psrp.SyncPowerShell(rp)
    ps.add_script("$PSVersionTable")
    output = ps.invoke()

    print(output)