Page 1 of 1

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

Posted: Mon May 27, 2019 3:16 am
by Jehoschua
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

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

Posted: Mon May 27, 2019 10:21 am
by Alexander Riedel
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?

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

Posted: Mon May 27, 2019 12:34 pm
by Jehoschua
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

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

Posted: Mon May 27, 2019 10:56 pm
by Alexander Riedel
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.

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

Posted: Tue May 28, 2019 12:25 am
by Jehoschua
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