Powershell and API

I’ve been using the API via powershell for a while now but seems like the original code no longer works due to maybe a powershell upgrade. Looks like it has issue invoking web requests do to the form parameter. Any suggestions?

Code im running

$Body = @{
    'data[gpg_auth][keyid]' = $MyFingerPrint
}
$Response1 = Invoke-WebRequest -Uri ($Server+$UriLogin) -Method Post -ContentType 'multipart/form-data' -Forms ($Body) -UseBasicParsing
$DecryptedToken = Unprotect-PGP -FilePathPrivate $PrivateKeyPath -Password $Password -String ([System.Web.HttpUtility]::UrlDecode($Response1.Headers.'X-GPGAuth-User-Auth-Token').Replace('\ ',' '))

$Body = @{
    'data[gpg_auth][keyid]' = $MyFingerPrint
    'data[gpg_auth][user_token_result]' = $DecryptedToken
}

$Response2 = Invoke-WebRequest -Uri ($Server+$UriLogin) -Method Post -ContentType 'multipart/form-data' -Forms ($Body) -SessionVariable WebSession -UseBasicParsing
$Response3 = Invoke-WebRequest -Uri ($Server) -Method Get -WebSession $WebSession -UseBasicParsing
$csrfToken = ($WebSession.Cookies.GetCookies($Server) | Where-Object {$_.Name -eq 'csrfToken'}).Value
$Headers = @{
    'X-CSRF-Token' = $csrfToken
}
$Resources = ((Invoke-WebRequest -Uri ($Server+$UriResource) -Method Get -Headers $Headers -WebSession $WebSession -UseBasicParsing) | ConvertFrom-Json).Body
$Users = ((Invoke-WebRequest -Uri ($Server+$UriUsers) -Method Get -Headers $Headers -WebSession $WebSession -UseBasicParsing) | ConvertFrom-Json).Body

Error Im getting.

Invoke-WebRequest : A parameter cannot be found that matches parameter name 'Forms'.
At C:\Admin\PSscripts\passboltpowershell.ps1:49 char:105
+ ... Login) -Method Post -ContentType 'multipart/form-data' -Forms ($Body) ...
+                                                            ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingExce 
   ption
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeWeb 
   RequestCommand
 
Unprotect-PGP : Cannot bind argument to parameter 'String' because it is an empty string.
At C:\Admin\PSscripts\passboltpowershell.ps1:50 char:94
+ ... ord -String ([System.Web.HttpUtility]::UrlDecode($Response1.Headers.' ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Unprotect-PGP], ParameterBindingValidationEx 
   ception
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Unprotec 
   t-PGP
 
Invoke-WebRequest : A parameter cannot be found that matches parameter name 'Forms'.
At C:\Admin\PSscripts\passboltpowershell.ps1:57 char:105
+ ... Login) -Method Post -ContentType 'multipart/form-data' -Forms ($Body) ...
+                                                            ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingExce 
   ption
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeWeb 
   RequestCommand
 
Invoke-WebRequest : The remote server returned an error: (401) Unauthorized.
At C:\Admin\PSscripts\passboltpowershell.ps1:63 char:16
+ ... sources = ((Invoke-WebRequest -Uri ($Server+$UriResource) -Method Get ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [ 
   Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.In 
   vokeWebRequestCommand
 
Invoke-WebRequest : The remote server returned an error: (401) Unauthorized.
At C:\Admin\PSscripts\passboltpowershell.ps1:64 char:12
+ $Users = ((Invoke-WebRequest -Uri ($Server+$UriUsers) -Method Get -He ...
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [ 
   Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.In 
   vokeWebRequestCommand

Hi,

I would say you have a variable who is empty.

Cheers,