Keeper-Security/keeper-sdk-dotnet

Copy-KeeperToClipboard erroring out

Surowa opened this issue · 4 comments

Surowa commented

Hi, our usage of Copy-KeeperToClipboard errors out with the following error message:

Set-Clipboard : Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. At C:\Program Files\WindowsPowerShell\Modules\PowerCommander\RecordCommands.ps1:124 char:25 + Set-Clipboard -Value $value + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Set-Clipboard], ThreadStateException + FullyQualifiedErrorId : System.Threading.ThreadStateException,Microsoft.PowerShell.Commands.SetClipboardCommand

I came across this issue:
https://stackoverflow.com/questions/17762037/current-thread-must-be-set-to-single-thread-apartment-sta-error-in-copy-stri
Maybe [STAThread] is missing (see proposed solutions)?

Version is 1.0.4

Surowa commented

Hi there, can I get some help with this issue?
It would make this module far more usable, now I cannot easily get the password generated via Add-KeeperRecord

Sorry, the original message has skipped out attention.
That STAThreadAttribute error is known to KeeperSdk and is already handled in the .Net code

thread.SetApartmentState(ApartmentState.STA);

This error is not expected in PowerShell application. PowerShell does not use multi-threading a lot.
What PowerShell terminal do you use?
Can you run the following commands in your PowerShell terminal?

PS C:\Work\keeper-sdk-dotnet> Set-Clipboard -Value "ffsdfsdfsd"
PS C:\Work\keeper-sdk-dotnet> [System.Threading.Thread]::CurrentThread.GetApartmentState()
STA
PS C:\Work\keeper-sdk-dotnet>

It looks like powershell has a switch that controls the appartment state -Sta
https://stackoverflow.com/questions/16072041/is-powershell-sta-apartment-state-preferred

Surowa commented

We use the Azure Automation Powershell terminal for all our automated scripts. This runs commands inside a special sandbox, and probably has a different configuration than default Powershell.
The output of the commands:

Set-Clipboard : Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. At line:1 char:2 + Set-Clipboard -Value "ffsdfsdfsd" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Set-Clipboard], ThreadStateException + FullyQualifiedErrorId : System.Threading.ThreadStateException,Microsoft.PowerShell.Commands.SetClipboardCommand

MTA

The fix is pushed to the master branch.
There is a workaround if the fix does not work.
Copy-KeeperToClipboard has -Output parameter.

$password = Copy-KeeperToClipboard <RECORD_UID> -Output Stdout