Search code examples
powershellfile-uploadhttpserver

PowerShell Script Returning Unexpected Output (random numbers)


Problem
I am writing a script in PowerShell that uploads a file to an http server. The upload completes successfully, but its returning a bunch of numbers in the console upon execution (far more than what is displayed below).

Output: enter image description here

Here's the script I'm running:

Param([Parameter(Mandatory=$True,Position=1)]
       [string]$user,
  [Parameter(Mandatory=$True,Position=2)]
       [string]$pass,
  [Parameter(Mandatory=$True,Position=3)]
       [string]$dir,
  [Parameter(Mandatory=$True,Position=4)]
       [string]$fileName,
  [Parameter(Mandatory=$True,Position=5)]
       [string]$url 
 )

$filePath = ("$dir" + "$fileName")

$webClient = New-Object System.Net.WebClient;
    $webClient.Credentials = New-Object System.Net.NetworkCredential("$user", "$pass");
    ("*** Uploading {0} file to {1} ***" -f ($filePath, $url) ) | write-host -ForegroundColor Blue -BackgroundColor White
    $webClient.UploadFile($url, "PUT", $filePath);

Question
Where are these numbers coming from, and how do I suppress them?


Solution

  • It looks like the numbers are the output coming from $webClient.UploadFile. Try adding > $null after it, like this:

    $webClient.UploadFile($url, "PUT", $filePath) > $null;
    

    This should send the output to null, making those numbers not display. Unfortunately I am unable to test this particular situation myself.