none
Help Understanding an Error from PowerShell RRS feed

  • Question

  • Hello, all. Thank you for taking the time to consider my post. 

    This is a short powershell script I'm working on. The idea is to download 3 files from a webserver to a directory on the machine running the script. I'm getting this error:

    "New-Object : Cannot find type [System.Net.WebClient.DownloadFile]: make sure the assembly containing this type is loaded."

    My understanding is that System.Net.WebClient.DownloadFile should be present and available to download on all versions of powershell? 

    Here is the code that generates the error:

    $URL = "http://www.example.com/web/upgrades"
    
    if (Test-Path C:\installs\NSClient++\nsclient.ini)
    {
        "Files found on the C drive"
        $BASEPATH = "C:\installs\NSClient++"
        $DOWNPATH = "C:\installs\20150417"
        }
    elseif (Test-Path D:\installs\NSClient++\nsclient.ini)
    {
        "Files found on the D Drive"
        $BASEPATH = "D:\installs\NSCLient++"
        $DOWNPATH = "D:\installs\20150417"
        }
    else
    {
        "Files not found on C or D drives, bailing"
        exit 1
        }
        
        New-Item -ItemType directory -Force -Path $DOWNPATH
        
        $a = "nsclient.ini"
        $b = "scripts.ini"
        $c = "check_hwinfo.wsf"
        $d = $a,$b,$c 
        
        foreach ($i in $d)
        {    
        $FULLTHERE = "$URL","$i" -join "/"
        $FULLHERE = "$DOWNPATH","$i" -join "\"
        New-Object System.Net.WebClient.DownloadFile($FULLTHERE,$FULLHERE) 
        }

    I'm trying to download the files http://www.example.com/web/upgrades/nsclient.ini scripts.ini, and check_hwinfo.wsf and place them in the direcory {C|D}/installs/20150417. Here's the full text of the errors:

    PS C:\Users\user\Downloads> .\20150417.ps1
    Files found on the C drive
    
    
        Directory: C:\installs
    
    
    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    d----         4/17/2015  12:59 PM            20150417
    New-Object : Cannot find type [System.Net.WebClient.DownloadFile]: make sure the assembly containing this type is loaded.
    At C:\Users\user\Downloads\20150417.ps1:32 char:15
    +     New-Object <<<<  System.Net.WebClient.DownloadFile($FULLTHERE,$FULLHERE)
        + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
        + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
    
    New-Object : Cannot find type [System.Net.WebClient.DownloadFile]: make sure the assembly containing this type is loaded.
    At C:\Users\user\Downloads\20150417.ps1:32 char:15
    +     New-Object <<<<  System.Net.WebClient.DownloadFile($FULLTHERE,$FULLHERE)
        + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
        + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
    
    New-Object : Cannot find type [System.Net.WebClient.DownloadFile]: make sure the assembly containing this type is loaded.
    At C:\Users\user\Downloads\20150417.ps1:32 char:15
    +     New-Object <<<<  System.Net.WebClient.DownloadFile($FULLTHERE,$FULLHERE)
        + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
        + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

    I don't understand why the attempted for each loop is failing so spectacularly. I'm normally a bash user, so I feel I'm on the wrong side of the isle here in any case. Thanks for any insight you can give me into how I've screwed this up!

    Friday, April 17, 2015 5:18 PM

Answers

All replies

  • Hi Sam,

    You should create your WebClient object first and then use the DownloadFile method on the object:

    $wc = New-Object System.Net.Webclient
    $wc.DownloadFile(...)


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Marked as answer by Sam Mulvaney Friday, April 17, 2015 5:36 PM
    Friday, April 17, 2015 5:28 PM
  • Thank you for your time, Mike! That absolutely worked. Why does Powershell care if I call the command by full name or through a variable? Shouldn't the two be interchangeable?

    Thanks again!

    • Proposed as answer by jrv Friday, April 17, 2015 5:36 PM
    • Unproposed as answer by Bill_StewartModerator Friday, April 17, 2015 6:18 PM
    Friday, April 17, 2015 5:36 PM
  • This is because the WebClient object's DownloadFile method is not equivalent to its constructor.


    -- Bill Stewart [Bill_Stewart]

    Friday, April 17, 2015 6:09 PM
    Moderator