mklement0 / clipboardtext Goto Github PK
View Code? Open in Web Editor NEWUniversal clipboard text support for PowerShell, notably also in PowerShell Core (cross-platform) and Windows PowerShell v2-v4
Universal clipboard text support for PowerShell, notably also in PowerShell Core (cross-platform) and Windows PowerShell v2-v4
Initially, I was not sure if this module runs on Windows PowerShell v.5. I tested it and it works fine.
This should be clear from readme.md
, because that means module can be used in universal PowerShell scripts. Universal PowerShell scripts are the ones which run on both Windows PowerShell and PowerShell Core.
In PS, the commands included out of the box are Set-Clipboard and Get-Clipboard. PS Core doesn't have such commands out of the box but this awesome module makes that possible, however it does so with slightly different names.
In order to ensure I can run the same scripts in PS and PS Core without modifications and without extra branching logic, the clipboard commands should have the same name.
I tried using this on windows and I get an error because the script host has been disabled through a domain policy by our IT department. Would it not be possible to P/Invoke relevant APIs on windows instead?
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
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.
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.
Thanks for this awesome module! Removes one more reason for not switching to PowerShell Core 6.x ;-)
I'd like to report a relatively minor issue.
If the current working directory is a UNC path, invoking Set-ClipboardText results in a message from CMD:
PS //pnjnas/public/olddiscs> (pwd).ProviderPath
\\pnjnas\public\olddiscs
PS //pnjnas/public/olddiscs> "foo" | Set-ClipboardText
'\\pnjnas\public\olddiscs'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
The solution is quite simple: wrap the invocation of CMD in a piece of code that preserves the current working directory, switches to some location on a non-network drive, and last restores the initial working directory, something like:
$oldpwd = (pwd).path
cd c:/ # ideal is to find a local drive instead of hardcoding c:
cmd.exe /c clip.exe '<' $tmpFile
cd $oldpwd
Problem
For whatever reason I nowadays get below error message when running Set-ClipboardText
from Powershell Core:
VERBOSE: Windows: using clip.exe
'clip.exe' is not recognized as an internal or external command,
operable program or batch file.
Set-ClipboardText : Invoking the platform-specific clipboard utility failed unexpectedly.
Running clip.exe works fine but not when called via cmd.exe
:
C:\>clip.exe /?
CLIP
Description:
Redirects output of command line tools to the Windows clipboard.
This text output can then be pasted into other programs.
Parameter List:
/? Displays this help message.
Examples:
DIR | CLIP Places a copy of the current directory
listing into the Windows clipboard.
CLIP < README.TXT Places a copy of the text from readme.txt
on to the Windows clipboard.
C:\>cmd.exe /c clip.exe /?
'clip.exe' is not recognized as an internal or external command,
operable program or batch file.
Solution
Works fine when using Get-Command to resolve full path
[string]$clipExePath = (Get-Command clip.exe).Path;
cmd.exe /c $clipExePath '<' $tmpFile
$ Get-ClipboardText
https://github.com/mklement0/ClipboardText/issues/new
$ ([string] (Get-ClipboardText))
https://github.com/mklement0/ClipboardText/issues/new
$ .\pwsh.exe -Command Get-ClipboardText
https://github.com/mklement0/ClipboardText/issues/new
$ .\pwsh.exe -Command Start-Process -Wait -NoNewWindow pwsh.exe -ArgumentList Get-ClipboardText
The argument 'Get-ClipboardText' is not recognized as the name of a script file. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
The error message is the same as when the clipboard contains no item.
EDIT:
To be more clear, is it possible to retain PS5 Get-Clipboard behavior?
$ .\pwsh.exe -Command Start-Process -Wait -NoNewWindow powershell.exe -ArgumentList Get-Clipboard
https://github.com/mklement0/ClipboardText/issues/new
My PowerShell profile invokes
Set-StrictMode -Version Latest
As a side effect, the first time I use the module in a PS session (i.e. when the module gets auto-loaded), I get a message telling me that "xclip" is not installed... whereas I'm running Windows PowerShell 5.1 on Windows 10 ;-)
Here's the culprit: CheckPrereqs.ps1 tests the value of $IsLinux, which doesn't exist in Windows PowerShell. If StrictMode is not in effect, the test works, because $IsLinux silently evaluates to $null.
But if StrictMode is in effect, the test fails...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.