Skip to content

Instantly share code, notes, and snippets.

@royashbrook
Created May 4, 2021 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save royashbrook/a99528709418bde046fd0cfdaf102081 to your computer and use it in GitHub Desktop.
Save royashbrook/a99528709418bde046fd0cfdaf102081 to your computer and use it in GitHub Desktop.
Post a text file to a discord webhook
function Submit-TextFile($filePath,$Uri){
$filename = (Get-ChildItem $filePath).Name
$filecontents = Get-Content $filePath -raw
$boundary = [guid]::NewGuid().ToString()
$contentinfo = "Content-Disposition: form-data; name=`"file`"; filename=`"$filename`"`nContent-Type: text/html; charset=utf-8`n"
$body = "--$boundary`n$contentinfo`n$filecontents`n--$boundary--`n"
$params = @{
Uri = $Uri
Body = $body
Method = 'Post'
ContentType = "multipart/form-data; boundary=$boundary"
}
Invoke-RestMethod @params
}
#generate a random file to test with
$randomlogfile = "{0:yyyyMMddhhmmss}.txt" -f (get-date)
"Random Data`n--------------`n" > $randomlogfile
0..20|%{[guid]::NewGuid().ToString()>>$randomlogfile}
$Uri = 'yourwebhookurl'
$filePath = (Get-Item $randomlogfile).FullName
# call the function
Submit-TextFile $filePath $Uri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment