Exception calling "UploadFile" with "2" argument(s)

Hosea Motshwaedi 0 Reputation points
2025-02-26T09:23:43.6433333+00:00

Hi, I have this code, it used to work, but now it's giving an error:

#we specify the directory where all files that we want to upload
$Dir="C:\path\Generate XML Files"
$DirArch="C:\path\Archive" #ftp server
$ftp = "server"
$user = "UserName"
$pass = "Paswword" $webclient = New-Object System.Net.WebClient $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) #remove junk and old xml files
Remove-Item ($DirArch + "*.xml") -Force
Remove-Item ($Dir + "*_.xml") -Force #list every xml file
foreach($item in (dir $Dir "*.xml")){
"Uploading $item..."
$uri = New-Object System.Uri($ftp+$item.Name)
$webclient.UploadFile($uri, $item.FullName)
#Archive Uploaded Files By Date
$date = (Get-Date).ToString('MM-dd-yyyy')
#$path = ($DirArch + $date)
$path = ($DirArch)
#If Today's folder doesn't exist, make one.
If(!(test-path $DirArch))
{
New-Item -ItemType Directory -Force -Path $path
}
#Move file to today's folder
Move-Item $item.FullName -destination $path -Force
}
#Beep When Finished
function b($a,$b){
[console]::beep($a,$b)
}
b 1000 100;
b 1333 200; Exception calling "UploadFile" with "2" argument(s): "An exception occurred during a WebClient request." At C:\path\uploadxmlfiles.ps1:22 char:5

  • $webclient.UploadFile($uri, $item.FullName)  
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
    
    • CategoryInfo : NotSpecified: (:) [], MethodInvocationException

I get this error:

Exception calling "UploadFile" with "2" argument(s): "An exception occurred during a WebClient request." file.ps1:23 char:5

  • $webclient.UploadFile($uri, $item.FullName)
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    • FullyQualifiedErrorId : WebException
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,830 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 35,596 Reputation points
    2025-02-26T14:13:25.1966667+00:00

    You do not have trailing backslashes in your directory name variables.

    $Dir="C:\path\Generate XML Files\" 
    $DirArch="C:\path\Archive\" #ftp server
    
    
    

    So when you append the extension you are going to get "C:\path\Generate XML Files*.XML". I would expect that to be a problem.

    Add the back slashes and in your foreach loop output the names of what you are processing. Verify that the names are correct.

    $uriname = $ftp+$item.Name
    "Uploading File={0}    URI={1}" -f $item.FullName, $uriname 
    $uri = New-Object System.Uri($uriname) 
    $webclient.UploadFile($uri, $item.FullName)
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.