jborean93 / pypsrp

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

how to add command Get-Maibox and Select #96

Closed altamiro closed 3 years ago

altamiro commented 3 years ago

Hi

how to add this command "Get-Mailbox -ResultSize 10 | Select id, RecipientType, RecipientTypeDetails, PrimarySmtpAddress" using pypsrp and getting the return

My code that I use as an example

` wsman = WSMan(server="outlook.office365.com", port=443, path="powershell-liveid", auth="basic", username=username, password=password, cert_validation=False)

    with RunspacePool(wsman, configuration_name="Microsoft.Exchange") as pool:

        ps = PowerShell(pool)
        ps.add_cmdlet("Get-Mailbox").add_parameter("ResultSize",10)
        output = ps.invoke()

        print ("==============================================")
        if ps.had_errors:
            print("ERROR:\n%s" % "\n".join([str(s) for s in ps.streams.error]))
        else:
            print("OUTPUT:\n%s" % "\n".join([str(s) for s in output]))
        print("==============================================")

`

Thanks for the help

jborean93 commented 3 years ago

PowerShell returns an object back with your properties and by default Python doesn't know how to format them. You can access those properties under output[0].extended_properties or output[0].adapted_properties. Both of those attributes are a dictionary with the key being the name of the property and the value being the value. I've got plans to try and make all this easier to manage within python but right now that's in the really early stages.

So you have 3 options

The last option is what is done for the high level client https://github.com/jborean93/pypsrp/blob/c70487480cbb651a89ee7d3efe5dfd21014ecc92/pypsrp/client.py#L212-L215. It wraps the command, in your case Get-Mailbox -ResultSize 10 inside Invoke-Expression and makes sure the output from that is a string to mimick what PowerShell would usually display on it's console.

K-V4 commented 3 years ago

I am the local Exchange, how should I access Exchange shell to make the get-Mailbox work.

Python: wsman = WSMan(server="ex.test.com",port=80,path="powershell",username=username, password=pwd,ssl=False) Error:SpnegoError (4294967295): [WinError -2146893039] 无法为身份验证与颁发机构取得联系。, Context: Processing security token

Powershell: New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://ex.test.com/powershell/" -Credential $Credentials -Authentication Kerberos -Name ex

jborean93 commented 3 years ago

@K-V4 the error you have is SEC_E_NO_AUTHENTICATING_AUTHORITY 0x80090311

No authority could be contacted for authentication. The domain name of the authenticating party could be wrong, the domain could be unreachable, or there might have been a trust relationship failure.

What this usually means is the process cannot reach the active directory domain controller and start the Kerberos authentication process. I'm not sure why PowerShell itself is working.