Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

version 5.1 fails in MTA mode #2

Closed
iricigor opened this issue May 25, 2018 · 1 comment
Closed

version 5.1 fails in MTA mode #2

iricigor opened this issue May 25, 2018 · 1 comment

Comments

@iricigor
Copy link

If PowerShell v.5.1 is running in MTA mode, code will fail on

Set-Clipboard -Value ($allText, $null)[$allText.Length -eq 0]

Error is:

Set-Clipboard : Current thread must be set to single thread apartment (STA) mode before OLE calls can be made

Steps to reproduce

Start PowerShell in MTA mode

powershell.exe -MTA

Start tests

Invoke-Pester

Expected results: Tests are successful

Actual results: Tests fail with error specified above.

Possible resolution

Check for STA/MTA before checking the version. Then execute 5.1 code only if running in STA mode.

Something like:

if (test-WindowsPowerShell) { # *Windows PowerShell*
    Add-Type -AssemblyName System.Windows.Forms
    $STAMode = [threading.thread]::CurrentThread.ApartmentState.ToString() -eq 'STA'
    if (($PSVersionTable.PSVersion -ge [version] '5.1.0') -and $STAMode) { # Ps*Win* v5.1+ now has Get-Clipboard / Set-Clipboard cmdlets.
...

The same should be done in both Get and Set commandlet. Still tests with the empty string will fail. But that is another issue.

mklement0 added a commit that referenced this issue May 25, 2018
Now supports running Windows PowerShell v3+ with -MTA
@mklement0
Copy link
Owner

Thanks, @iricigor: good catch.

I've just published v0.1.2, which should fix the problem.

Incidentally, there was another bug, albeit a benign one: Set-Clipboard and Get-Clipboard were introduced in v5.0, not v5.1.

Still tests with the empty string will fail. But that is another issue.

I assume you mean the warning that is issued to alert users that in MTA mode you cannot copy an empty string to the clipboard for technical reasons. That's a documented limitation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants