How can I set thread and start it with some executable code with ApartmentState = "STA"
? I just find this method but I don't know how I can pass my scriptblock to it.
$r = [RunspaceFactory]::CreateRunspace()
$r.ApartmentState = 'STA'
$r.Open()
I need to get clipboard text in this thread, like this:
$getclipboardtext = [System.Windows.Clipboard]::GetText()
$getclipboardtext | Out-File c:\examplepath
I also tried Start-Job
.
You need to put the runspace in a PowerShell instance (see here):
where
$r
is the runspace you opened and$sb
is the scriptblock you want to execute.You also have an error in your scriptblock. The
Clipboard
class is part of theSystem.Windows.Forms
namespace, so it'sSystem.Windows.Forms.Clipboard
, notSystem.Windows.Clipboard
. Plus, you need to load the respective assembly first.Full example: