BW CLI and powershell

HI

I’m tryning to extract list meber by bw.exe on windows in powershell.

I’m using Org API KEY.

I’ve found the next powershell script:
cd C:\PScripts\bitwarden

$organization_id = “organization.8875e0ce-7162-407a-8716-b05800ed99ba” # Set your Org ID
$cloud_flag = 1 # Self-hosted Bitwarden or Cloud?
if ($cloud_flag -eq 1) {
$api_url = “https://api.bitwarden.eu
$identity_url = “https://identity.bitwarden.eu
}

Set up CLI and API auth

$org_client_secret = Get-Content “orgsecureString.txt” | ConvertTo-SecureString
$client_creds = New-Object System.Management.Automation.PSCredential “null”, $org_client_secret
$org_client_secret_key = , $client_creds.GetNetworkCredential().password
$org_client_id = $organization_id

Get Access Token

$body = “grant_type=client_credentials&scope=api.organization&client_id=$org_client_id&client_secret=$org_client_secret_key”
$bearer_token = (Invoke-RestMethod -Method POST -Uri $identity_url/connect/token -Body $body).access_token

$headers = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”
$headers.Add(‘Authorization’,(‘Bearer {0}’ -f $bearer_token))
$headers.Add(‘Accept’,‘application/json’)
$headers.Add(‘Content-Type’,‘application/json’)

Perform CLI and API auth

powershell -c ‘bw status’

$password = Get-Content “secureString.txt” | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential “null”, $password
$session_key = , $cred.GetNetworkCredential().password | powershell -c ‘bw unlock --raw’

At the last instruction, I met an issue when I want to get the session key in a variable:
$session_key = , $cred.GetNetworkCredential().password | powershell -c ‘bw unlock --raw’
? Master password: [hidden]
mac failed.
mac failed.
Immediately after, it I unlock with same instructions without getting the session key in a variable with the password stored in $cred.GetNetworkCredential().password , tha’s execute correctly:
powershell -c ‘bw unlock --raw’
? Master password: [hidden]
IoLef/dWSjNCYY79AdgsmGhfu9YGdO4wXCEMDVgRQkh0WaOcmpib8oRtGS92itecun9YpWTGyVbPIgd6B5bXdQ==

I need help, thanks in advance.

Regards