Bug: Start the Debugger: Please don't delete the Clipboard

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
Jehoschua
Posts: 54
Joined: Fri Jan 29, 2016 6:23 am

Bug: Start the Debugger: Please don't delete the Clipboard

Post by Jehoschua » Mon May 27, 2019 3:16 am

Product: PowerShell Studio 2019 (64 Bit)
Build: v5.6.163
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.17134.0


Hello,

If we start the Debugger using F5, then PowerShell Studio always deletes the Clipboard: :-(

Code: Select all

[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low', DefaultParameterSetName = '…')]
Param ( … )

# Text is alyways empty :-(
$Text = Get-Clipboard -Format Text
This is very annoying if one has to write Tools to handle the Clipboard.

Thanks a lot,
kind regards,
Thomas

User avatar
Alexander Riedel
Posts: 7125
Joined: Tue May 29, 2007 4:43 pm

Re: Bug: Start the Debugger: Please don't delete the Clipboard

Post by Alexander Riedel » Mon May 27, 2019 10:21 am

I cannot replicate that. I put something in the clipboard using notepad, then set a breakpoint in a script, start debugging.
While at the breakpoint, using notepad++ to paste the clipboard content. It is exactly what was copied in Notepad before.
Can you see if you can replicate the problem on your machine that way?
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
Jehoschua
Posts: 54
Joined: Fri Jan 29, 2016 6:23 am

Re: Bug: Start the Debugger: Please don't delete the Clipboard

Post by Jehoschua » Mon May 27, 2019 12:34 pm

Good evening

I'm sorry for the unprecise report. We're doing this:
  1. In PowerShell Studio: Create a new PowerShell Script
  2. We add this code and save the file:

    Code: Select all

    $Text = Get-Clipboard -Format Text
    Write-Host "Text: $Text"
    
  3. If we start the Script-File in a PowerShell session, it works as expected
  4. Now, we set a Breakpoint on Line 2 in PowerShell Studio
  5. In Notepad: we copy some Text into the Clipboard
  6. In PowerShell Studio: we start the Debugger using F5
  7. As soon as the breakpoint is hit, $Text is empty
  8. Vers strange: if we now switch to the Notepad and paste the Clipboard, then the right Text is pasted into Notepad!
    Therefore, it looks like Get-Clipboard does not work in PowerShell Studio
Additional Information:
In a normal PowerShell and in Sapien PowerShell Studio, we get this information about the Get-Clipboard command:

Code: Select all

Get-Command Get-Clipboard

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Get-Clipboard                                      3.1.0.0    Microsoft.PowerShell.Management
Thanks a lot, kind regards,
Thomas

User avatar
Alexander Riedel
Posts: 7125
Joined: Tue May 29, 2007 4:43 pm

Re: Bug: Start the Debugger: Please don't delete the Clipboard

Post by Alexander Riedel » Mon May 27, 2019 10:56 pm

Set-Clipboard and Get-Clipboard are functions which only work in STA mode (Single Threaded Apartment).
Make sure to enable STA mode before running or debugging your script.

Interestingly enough, PowerShell spits out an error message when you use Set-Clipboard without STA mode:
ERROR: 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
ERROR: STAThreadAttribute marked on it.
ERROR: At C:\Users\Alex\Desktop\Files\Clipboard Test.ps1:22 char:1
ERROR: + Set-Clipboard -Value "This is a test"
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [Set-Clipboard], ThreadStateException
ERROR: + FullyQualifiedErrorId : System.Threading.ThreadStateException,Microsoft.PowerShell.Commands.SetClipboardCommand
ERROR:
Text:

But it fails to do so for Get-Clipboard.
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
Jehoschua
Posts: 54
Joined: Fri Jan 29, 2016 6:23 am

Re: Bug: Start the Debugger: Please don't delete the Clipboard

Post by Jehoschua » Tue May 28, 2019 12:25 am

Thank you very much!, this was the Problem. I also relied on the usual behavior to get an Exception on STA related errors...

As information for other readers:
I selected this solution to enforce STA and added this comment to all my scripts:

Code: Select all

# %ForceSTA% = True
Thanks a lot for your support!,
kind regards,
Thomas