In PowerShell, come si fa inviare l'output di un comando negli appunti, mauscita del tubo negli appunti utilizzando PowerShell
- ancora in grado di pipe i dati a più processi
- dipendenze da applicazioni esterne come
clip.exe
- per farlo funzionare come un filtro, così vediamo uscita sulla linea di comando immediatamente
?
EDIT: 14 maggio 2015
Dopo 3 anni, ho pensato di condividere la mia ClipboardModule
(spero mi è permesso di):
Add-Type -AssemblyName System.Windows.Forms
Function Get-Clipboard {
param([switch]$SplitLines)
$text = [Windows.Forms.Clipboard]::GetText();
if ($SplitLines) {
$xs = $text -split [Environment]::NewLine
if ($xs.Length -gt 1 -and -not($xs[-1])) {
$xs[0..($xs.Length - 2)]
} else {
$xs
}
} else {
$text
}
}
function Set-Clipboard {
$in = @($input)
$out =
if ($in.Length -eq 1 -and $in[0] -is [string]) { $in[0] }
else { $in | Out-String }
if ($out) {
[Windows.Forms.Clipboard]::SetText($out);
} else {
# input is nothing, therefore clear the clipboard
[Windows.Forms.Clipboard]::Clear();
}
}
function GetSet-Clipboard {
param([switch]$SplitLines, [Parameter(ValueFromPipeLine=$true)]$ObjectSet)
if ($input) {
$ObjectSet = $input;
}
if ($ObjectSet) {
$ObjectSet | Set-Clipboard
} else {
Get-Clipboard -SplitLines:$SplitLines
}
}
Set-Alias cb GetSet-Clipboard
Export-ModuleMember -Function *-* -Alias *
io di solito uso l'alias cb
(per GetSet-Clipboard
) perché è a due vie cioè in grado di ottenere o impostare l'appunti:
cb # gets the contents of the clipboard
"john" | cb # sets the clipboard to "john"
cb -s # gets the clipboard and splits it into lines
Perché non vuoi usare 'clip'? –
Caleb - Quindi posso vedere l'output anche sulla riga di comando. –
Tahir, cosa c'è di sbagliato in 'Get-Foo | tee -v output; $ output | clip' allora? – Joey