代码之家  ›  专栏  ›  技术社区  ›  Édouard Lopez

如何将字符串复制到剪贴板PowerShell?Linux/Windows/Mac版

  •  0
  • Édouard Lopez  · 技术社区  · 7 年前

    我正在编写一个PowerShell模块,它的目标是分发,我正在寻找一个 跨平台(win/linux/macos)PowerShell ≥5.0 将生成的字符串复制到系统剪贴板的解决方案。

    埃恩

    > $PSVersionTable
    
    Name                           Value
    ----                           -----
    PSVersion                      6.1.2
    PSEdition                      Core
    GitCommitId                    6.1.2
    OS                             Linux 4.15.0-43-generic #46-Ubuntu SMP Thu Dec 6 14:45:28 UTC 2018
    Platform                       Unix
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0
    

    Set-Clipboard

    PS ~/projects/lesspass-powershell> Get-Random | Set-Clipboard
    Set-Clipboard : The term 'Set-Clipboard' is not recognized as the name of a cmdlet, function, script file, or operable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:14
    + Get-Random | Set-Clipboard
    +              ~~~~~~~~~~~~~
    + CategoryInfo
    : ObjectNotFound        : (Set-Clipboard:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    

    Write-Clipboard

    PS ~/projects/lesspass-powershell> Get-Random | Write-Clipboard
    Write-Clipboard : The term 'Write-Clipboard' is not recognized as the name of a cmdlet, function, script file, or operable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:14
    + Get-Random | Write-Clipboard
    +              ~~~~~~~~~~~~~~~
    + CategoryInfo
    : ObjectNotFound        : (Write-Clipboard:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    

    Out-Clipboard

    PS ~/projects/lesspass-powershell> Get-Random | Out-Clipboard
    Out-Clipboard : The term 'Out-Clipboard' is not recognized as the name of a cmdlet, function, script file, or operable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:14
    + Get-Random | Out-Clipboard
    +              ~~~~~~~~~~~~~
    + CategoryInfo
    : ObjectNotFound        : (Out-Clipboard:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Édouard Lopez    7 年前

    解决方案:安装模块

    您可以安装辅助模块 ClipboardText

    定位命令

    Windows(PS) v5.1 )要知道哪个模块包含这些命令:

    PS> get-module | ?{$_.ExportedCommands.Keys -like '*-Clipboard'}
    
        ModuleType Version    Name                                ExportedCommands
        ---------- -------    ----                                ----------------
        Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}
        Script     3.2.0.0    Pscx                                {Add-PathVariable, Clear-MSMQueue, Convert-Xml, ConvertFrom-Base64...}
    

    但是,此结果在ps上为空 v6.1 core !

    get-module Microsoft.PowerShell.Management,pscx | ?{$_.ExportedCommands.Keys -like '*-Clipboard'}
    
    推荐文章