[GH-ISSUE #53] Paste from PWSH - WT #40

Closed
opened 2026-02-27 10:15:25 +03:00 by kerem · 1 comment
Owner

Originally created by @CrazyWolf13 on GitHub (May 4, 2024).
Original GitHub issue: https://github.com/matze/wastebin/issues/53

Hi

Awesome tool you made!!

I'd like to write a ps1 function that allows me to pipe into wastebin and then get the url-->

cat "Hello World!" | pasteToWaste

I tried this approach, but cannot really get anything to work, have you got an idea?

function pasteToWaste {
    if ($args.Count -eq 0) {
        Write-Error "No file path specified."
        return
    }

    $FilePath = $args[0]

    if (Test-Path $FilePath) {
        $Content = Get-Content $FilePath -Raw
    } else {
        Write-Error "File path does not exist."
        return
    }

    $uri = "http://bin.mydomain.tld"
    try {
        $response = Invoke-RestMethod -Uri $uri -Method Post -Body @{text=$Content} -ErrorAction Stop
        $url = "http://bin.mydomain.tld/$($response.key)"
        Write-Output $url
    } catch {
        Write-Error "Failed to upload the document. Error: $_"
    }
}

Originally created by @CrazyWolf13 on GitHub (May 4, 2024). Original GitHub issue: https://github.com/matze/wastebin/issues/53 Hi Awesome tool you made!! I'd like to write a ps1 function that allows me to pipe into wastebin and then get the url--> `cat "Hello World!" | pasteToWaste` I tried this approach, but cannot really get anything to work, have you got an idea? ``` function pasteToWaste { if ($args.Count -eq 0) { Write-Error "No file path specified." return } $FilePath = $args[0] if (Test-Path $FilePath) { $Content = Get-Content $FilePath -Raw } else { Write-Error "File path does not exist." return } $uri = "http://bin.mydomain.tld" try { $response = Invoke-RestMethod -Uri $uri -Method Post -Body @{text=$Content} -ErrorAction Stop $url = "http://bin.mydomain.tld/$($response.key)" Write-Output $url } catch { Write-Error "Failed to upload the document. Error: $_" } } ```
kerem closed this issue 2026-02-27 10:15:26 +03:00
Author
Owner

@CrazyWolf13 commented on GitHub (May 5, 2024):

$WastebinServerUrl = "https://bin.mydomain.tld"
$DefaultExpirationTime = 3600  # Default expiration time: 1 hour (in seconds)
$DefaultBurnAfterReading = $false  # Default value for burn after reading setting

function ptw {
    [CmdletBinding()]
    param (
        [Parameter(Position=0)]
        [string]$FilePath,
        
        [Parameter(Position=1)]
        [int]$ExpirationTime = $DefaultExpirationTime,
        
        [Parameter(Position=2)]
        [bool]$BurnAfterReading = $DefaultBurnAfterReading
    )

    process {
        if (-not $FilePath) {
            Write-Host "File path not provided."
            return
        }
        
        if (-not (Test-Path $FilePath)) {
            Write-Host "File '$FilePath' not found."
            return
        }

        try {
            $FileContent = Get-Content -Path $FilePath -Raw
            
            $Payload = @{
                text = $FileContent
                extension = $null
                expires = $ExpirationTime
                burn_after_reading = $BurnAfterReading
            } | ConvertTo-Json

            $Response = Invoke-RestMethod -Uri $WastebinServerUrl -Method Post -Body $Payload -ContentType 'application/json'
            $Path = $Response.path -replace '\.\w+$'

            Write-Host ""
            Write-Host "$WastebinServerUrl$Path"
        }
        catch {
            Write-Host "Error occurred: $_"
        }
    }
}
function pptw {
    param (
        [Parameter(ValueFromPipeline=$true)]
        [string]$InputContent,
        [int]$ExpirationTime = $DefaultExpirationTime,
        [bool]$BurnAfterReading = $DefaultBurnAfterReading
    )
    begin {
        $AllInputContent = @()  # Array to store all lines of input
    }
    process {
        $AllInputContent += $InputContent  # Add each line to the array
    }

    end {
        try {
            # Concatenate all lines into a single string
            $CombinedInput = $AllInputContent -join "`r`n"

            $Payload = @{
                text = $CombinedInput
                extension = $null
                expires = $ExpirationTime
                burn_after_reading = $BurnAfterReading
            } | ConvertTo-Json

            $Response = Invoke-RestMethod -Uri $WastebinServerUrl -Method Post -Body $Payload -ContentType 'application/json'
            $Path = $Response.path -replace '\.\w+$'

            Write-Host ""
            Write-Host "$WastebinServerUrl$Path"
        }
        catch {
            Write-Host "Error occurred: $_"
        }
    }
}

I could solve it myself, if anyone has a similar problem, the above worked for me, the function ptw (PasteToWaste) takes ptw {filepath} {time} {true/false(burn after read)} and pptw is when using the pipe, like this: cat test.txt | pptw

<!-- gh-comment-id:2094783465 --> @CrazyWolf13 commented on GitHub (May 5, 2024): ``` $WastebinServerUrl = "https://bin.mydomain.tld" $DefaultExpirationTime = 3600 # Default expiration time: 1 hour (in seconds) $DefaultBurnAfterReading = $false # Default value for burn after reading setting function ptw { [CmdletBinding()] param ( [Parameter(Position=0)] [string]$FilePath, [Parameter(Position=1)] [int]$ExpirationTime = $DefaultExpirationTime, [Parameter(Position=2)] [bool]$BurnAfterReading = $DefaultBurnAfterReading ) process { if (-not $FilePath) { Write-Host "File path not provided." return } if (-not (Test-Path $FilePath)) { Write-Host "File '$FilePath' not found." return } try { $FileContent = Get-Content -Path $FilePath -Raw $Payload = @{ text = $FileContent extension = $null expires = $ExpirationTime burn_after_reading = $BurnAfterReading } | ConvertTo-Json $Response = Invoke-RestMethod -Uri $WastebinServerUrl -Method Post -Body $Payload -ContentType 'application/json' $Path = $Response.path -replace '\.\w+$' Write-Host "" Write-Host "$WastebinServerUrl$Path" } catch { Write-Host "Error occurred: $_" } } } ``` ``` function pptw { param ( [Parameter(ValueFromPipeline=$true)] [string]$InputContent, [int]$ExpirationTime = $DefaultExpirationTime, [bool]$BurnAfterReading = $DefaultBurnAfterReading ) begin { $AllInputContent = @() # Array to store all lines of input } process { $AllInputContent += $InputContent # Add each line to the array } end { try { # Concatenate all lines into a single string $CombinedInput = $AllInputContent -join "`r`n" $Payload = @{ text = $CombinedInput extension = $null expires = $ExpirationTime burn_after_reading = $BurnAfterReading } | ConvertTo-Json $Response = Invoke-RestMethod -Uri $WastebinServerUrl -Method Post -Body $Payload -ContentType 'application/json' $Path = $Response.path -replace '\.\w+$' Write-Host "" Write-Host "$WastebinServerUrl$Path" } catch { Write-Host "Error occurred: $_" } } } ``` I could solve it myself, if anyone has a similar problem, the above worked for me, the function ptw (PasteToWaste) takes `ptw {filepath} {time} {true/false(burn after read)}` and pptw is when using the pipe, like this: `cat test.txt | pptw`
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/wastebin-matze#40
No description provided.