Created
July 27, 2014 13:53
-
-
Save Nora-Ballard/305ca7417ca2110b374a to your computer and use it in GitHub Desktop.
Function for getting the contents of the clipboard from Powershell, will try to identify the content and output the appropriate object; File paths as file objects, csv content as named properties, single column list as array, etc. Also contains useful functions for working with streams.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function New-PSObjectFromArrays { | |
| param( | |
| [Parameter(Mandatory)] | |
| [ValidateNotNullOrEmpty()] | |
| [array]$Values, | |
| [Parameter()] | |
| [ValidateNotNullOrEmpty()] | |
| [string[]]$PropertyNames | |
| ) | |
| $i = 0 | |
| $hashtable = [ordered]@{} | |
| $Values | ForEach-Object { | |
| if ($hashtable.Keys -Contains $PropertyNames[$i]) { | |
| Write-Error ("The member {0} is already present." -f $PropertyNames[$i]) | |
| } | |
| else { | |
| $hashtable.Add($PropertyNames[$i],$_) | |
| } | |
| $i++ | |
| } | |
| Write-Output ([pscustomobject]$hashtable) | |
| } | |
| function ConvertTo-MemoryStream { | |
| param( | |
| [Parameter(Mandatory)] | |
| [string[]]$InputObject | |
| ) | |
| $ByteArray = ([system.Text.Encoding]::UTF8).GetBytes($InputObject) | |
| [System.IO.MemoryStream]($ByteArray) | |
| } | |
| function Import-CsvFromStream { | |
| param( | |
| [Parameter(Mandatory, ValueFromPipeline)] | |
| [ValidateScript({$_ -is [System.IO.Stream]})] | |
| $InputObject, | |
| [Parameter()] | |
| [array]$Header, | |
| [Parameter()] | |
| [string]$Delimiter = ',' | |
| ) | |
| BEGIN { | |
| Add-Type -AssemblyName Microsoft.VisualBasic | |
| } | |
| PROCESS { | |
| $Stream = $InputObject | |
| $Stream.Seek(0,'Begin') | Out-Null | |
| $Parser = ([Microsoft.VisualBasic.FileIO.TextFieldParser]$Stream) | |
| $Parser.HasFieldsEnclosedInQuotes = $true | |
| $Parser.SetDelimiters($Delimiter) | |
| $PropertyNames = $Parser.ReadFields() | |
| if ($PSBoundParameters.ContainsKey('Header')) { | |
| $PropertyNames = $Header | |
| } | |
| if ($PropertyNames.Count -eq 1) { | |
| Write-Output $PropertyNames | |
| while (!$Parser.EndOfData) | |
| { | |
| Write-Output $Parser.ReadFields() | |
| } | |
| } | |
| else { | |
| while (!$Parser.EndOfData) | |
| { | |
| $fields = $Parser.ReadFields() | |
| if ($fields -ne @()) { New-PSObjectFromArrays -Values $fields -PropertyNames $PropertyNames} | |
| } | |
| } | |
| } | |
| } | |
| function Get-UriFromText { | |
| param( | |
| [Parameter(Mandatory, ValueFromPipeline)] | |
| [string[]]$InputObject | |
| ) | |
| PROCESS { foreach ($String in $InputObject) { | |
| $Uri = $null | |
| $IsUri = [System.Uri]::TryCreate($String, [System.UriKind]::Absolute, [ref]$Uri) | |
| if ($IsUri) { | |
| if ($Uri.IsFile -or $Uri.IsUNC) { | |
| Write-Output (Get-Item $Uri.LocalPath) | |
| } | |
| else { | |
| Write-Output $Uri | |
| } | |
| } | |
| else { | |
| Write-Output $String | |
| } | |
| }} | |
| } | |
| function Get-Clipboard { | |
| Add-Type -Assembly PresentationCore | |
| $CommaSeparatedValue = [System.Windows.Forms.DataFormats]::CommaSeparatedValue | |
| if ([Windows.Clipboard]::ContainsData($CommaSeparatedValue)) { | |
| $ClipStream = [Windows.Clipboard]::GetData($CommaSeparatedValue) | |
| Import-CsvFromStream -InputObject $ClipStream | |
| } | |
| elseif ([Windows.Clipboard]::ContainsFileDropList()) { | |
| $clipLines =[Windows.Clipboard]::GetFileDropList() | |
| $clipLines | Get-UriFromText | |
| } | |
| elseif ([Windows.Clipboard]::ContainsText()) { | |
| $clipLines =[Windows.Clipboard]::GetText().split("`n") | |
| $clipLines | Get-UriFromText | |
| } | |
| elseif ([Windows.Clipboard]::ContainsImage()) { | |
| Write-Output ([Windows.Clipboard]::GetImage()) | |
| } | |
| } | |
| Export-ModuleMember -Function Get-Clipboard |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment