FriedrichWeinmann / EntraAuth

MIT License
44 stars 1 forks source link

Add support for PowerShell 5 to request data #23

Open weyCC81 opened 1 week ago

weyCC81 commented 1 week ago

The sign-in (connect) does work with PowerShell 5, but I am not able to request data, for example with "Get-MdExposureScore" or "Get-MdMachine"...

Apparently Invoke-RestMethod can not handle empty body's like {} in the parameters: https://github.com/FriedrichWeinmann/EntraAuth/blob/bfc239c163e6a52bc1e3feb71b0f55f18c758467/EntraAuth/functions/Core/Invoke-EntraRequest.ps1#L152

I believe we would have to adjust this section: https://github.com/FriedrichWeinmann/EntraAuth/blob/bfc239c163e6a52bc1e3feb71b0f55f18c758467/EntraAuth/functions/Core/Invoke-EntraRequest.ps1#L139

The following workaround after line 145 works for requesting data:

If ($Body.Count -eq 0){  
  # PowerShell 5 support, Error: Cannot send a content-body with this verb-type.
  $parameters.Remove('Body')
}

Functions with the following param would have to be validated

Body = $PSBoundParameters | ConvertTo-HashTable -Include @('
FriedrichWeinmann commented 15 hours ago

Heya, thanks for reporting this issue. The 1.4.21 release should resolve the issue :)

Technically this was not really caused by EntraAuth directly - incorrectly providing a body where none belong is really on the calling module. Which in your case is DefenderAPI, another one of mine, so it was going to land on me to fix anyway ;) That said, I decided to handle this issue centrally within EntraAuth, removing the body in PS5 with the incorrect methods (GET, DEFAULT and HEAD), since this is the PS v5.1 command complaining, rather than some APIs, so it's worth handling centrally.

Oh, I also added another feature I needed for another engagement you may find convenient: You can now directly connect with a RefreshToken! While the DefenderAPI module will not natively support that until the next release, this means you can now already do something like this:

# Ensure the module's entra services are registered
Import-Module DefenderAPI

# Interactive delegate login via Browser
$token = Connect-EntraService -Service DefenderAPI.Endpoint -ClientID $clientID -TenantID $tenantID -PassThru

# Use the same token to log into the second service without having to go through the browser again
Connect-EntraService -Service DefenderAPI.Security -RefreshTokenObject $token
weyCC81 commented 11 hours ago

Hi @FriedrichWeinmann

Thanks for the fast fix, I can confirm it works with PowerShell ISE now. Have a nice week!

Reference: https://github.com/FriedrichWeinmann/EntraAuth/blob/0604b316951650e5c2f2933861b8703992d71cc8/EntraAuth/functions/Core/Invoke-EntraRequest.ps1#L147