Skip to content

Instantly share code, notes, and snippets.

@Luzifer
Last active December 30, 2017 02:04
Show Gist options
  • Save Luzifer/071b30106af5b5c7f34e60def528386e to your computer and use it in GitHub Desktop.
Save Luzifer/071b30106af5b5c7f34e60def528386e to your computer and use it in GitHub Desktop.
Using HTTP listening version of share utility from Windows Explorer
param (
[System.IO.FileInfo] $filename = $null,
[System.IO.FileInfo] $passwordFile = "C:\Users\Luzifer\Documents\share.pwd",
[string]$username = 'luzifer',
[string]$uri = "https://tools.hub.luzifer.io/share/post"
)
function Post-ShareFile {
param (
[System.IO.FileInfo] $filename = $null
)
# PowerShell by default uses TLS1.0 or something, the server is using TLS1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$secpasswd = Get-Content $passwordFile | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PsCredential($username, $secpasswd)
$boundary = [guid]::NewGuid().ToString()
$filebody = [IO.File]::ReadAllBytes($FileName)
$filebodyenc = [System.Text.Encoding]::GetEncoding("iso-8859-1").GetString($filebody)
#creating the formdata manually
$LF = "`r`n"
$basename = $filename.Name
$bodyLines = (
"--$boundary",
"Content-Disposition: form-data; name=`"file`"; filename=`"$basename`"",
"Content-Transfer-Encoding: binary$LF",
$filebodyenc,
"--$boundary--"
) -join $LF
$result = Invoke-WebRequest -UseBasicParsing -Uri $uri -Credential $cred -Method POST -Body $bodyLines -ContentType "multipart/form-data; boundary=$boundary"
$result.Content
}
# Source: https://github.com/proxb/PowerShell_Scripts/blob/master/Invoke-BalloonTip.ps1
Function Invoke-BalloonTip {
<#
.Synopsis
Display a balloon tip message in the system tray.
.Description
This function displays a user-defined message as a balloon popup in the system tray. This function
requires Windows Vista or later.
.Parameter Message
The message text you want to display. Recommended to keep it short and simple.
.Parameter Title
The title for the message balloon.
.Parameter MessageType
The type of message. This value determines what type of icon to display. Valid values are
.Parameter SysTrayIcon
The path to a file that you will use as the system tray icon. Default is the PowerShell ISE icon.
.Parameter Duration
The number of seconds to display the balloon popup. The default is 1000.
.Inputs
None
.Outputs
None
.Notes
NAME: Invoke-BalloonTip
VERSION: 1.0
AUTHOR: Boe Prox
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$True,HelpMessage="The message text to display. Keep it short and simple.")]
[string]$Message,
[Parameter(HelpMessage="The message title")]
[string]$Title="Attention $env:username",
[Parameter(HelpMessage="The message type: Info,Error,Warning,None")]
[System.Windows.Forms.ToolTipIcon]$MessageType="Info",
[Parameter(HelpMessage="The path to a file to use its icon in the system tray")]
[string]$SysTrayIconPath='C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe',
[Parameter(HelpMessage="The number of milliseconds to display the message.")]
[int]$Duration=1000
)
Add-Type -AssemblyName System.Windows.Forms
If (-NOT $global:balloon) {
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
#Mouse double click on icon to dispose
[void](Register-ObjectEvent -InputObject $balloon -EventName MouseDoubleClick -SourceIdentifier IconClicked -Action {
#Perform cleanup actions on balloon tip
Write-Verbose 'Disposing of balloon'
$global:balloon.dispose()
Unregister-Event -SourceIdentifier IconClicked
Remove-Job -Name IconClicked
Remove-Variable -Name balloon -Scope Global
})
}
#Need an icon for the tray
$path = Get-Process -id $pid | Select-Object -ExpandProperty Path
#Extract the icon from the file
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($SysTrayIconPath)
#Can only use certain TipIcons: [System.Windows.Forms.ToolTipIcon] | Get-Member -Static -Type Property
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]$MessageType
$balloon.BalloonTipText = $Message
$balloon.BalloonTipTitle = $Title
$balloon.Visible = $true
#Display the tip and specify in milliseconds on how long balloon will stay visible
$balloon.ShowBalloonTip($Duration)
Write-Verbose "Ending function"
}
Add-Type -AssemblyName System.Windows.Forms
try {
$url = Post-ShareFile -filename $filename
Set-Clipboard -Value $url
Invoke-BalloonTip -Message "URL has been copied to clipboard" -Title "Share @ knut.cc"
} catch {
Write-Host $_.Exception.Message
Invoke-BalloonTip -Message "Something went wrong. :(" -Title "Share @ knut.cc" -MessageType Error
Break
}
### Installation
# Set password encrypted to file:
# (Get-Credential).Password | ConvertFrom-SecureString | Out-File "C:\Users\Luzifer\Documents\share.pwd"
# Register shell extension:
# New-Item -Path "HKCU:\Software\Classes\*\shell\Share @ knut.cc\command" -Verbose
# Set-ItemProperty -Path "HKCU:\Software\Classes\*\shell\Share @ knut.cc\command" -Name "(Default)" -Value "powershell.exe -WindowStyle Hidden -NonInteractive -NoProfile C:\Users\Luzifer\Documents\share.ps1 -filename '%1'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment