Geoff,

 

Try something like this:

 

# Declare a log path

$Log = 'c:\mylog.log';

 

# Create an array of HashTables, each of which has a source URL &
destination path

$ItemList = @(

        @{

            Source = 'URL';

            Destination = '\\Share\Folder\file.name';

        };

        @{

            Source = 'URL2';

            Destination = '\\Share\Folder2\file.name';

        };

    );

 

# Create the System.Net.WebClient object

$WebClient = New-Object -TypeName System.Net.WebClient;

 

# Iterate over list of items, and download each one within a try..catch
block

foreach ($Item in $ItemList) {

    try {

        $WebClient.DownloadFile($Item.Source, $Item.Destination)

        Add-Content -Path $Log -Value ('Item downloaded successfully: {0}'
-f $Item.Source);

    }

    catch {

        Write-Warning -Message ('Item failed to download: {0}' -f
$Item.Source);

    }

} 

 

Cheers,

Trevor Sullivan

Microsoft PowerShell MVP

 

From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com]
On Behalf Of Orlebeck, Geoffrey
Sent: Friday, May 30, 2014 9:35 AM
To: powershell@lists.myITforum.com
Subject: [powershell] Logging Question

 

Hello Group

 

I'm pretty new to PowerShell, and while I have figured out logging in most
situations, I'm trying to log a web download success/failure when using
"New-Object System.Net.WebClient DownloadFile". Is this possible? I've been
able to get output files created, but they are always blank. Like I said,
I'm pretty new to PowerShell so just a little help would be greatly
appreciated.

 

I'm attempting to download multiple files, but the gist of the script looks
like this:

 

---------------------------------

$file1 = "URL"

$dir1 = "\\Share\Folder"

$file2 = "URL2"

$dir2 = "\\Share\Folder2"

$wc = New-Object System.Net.WebClient

$wc.DownloadFile($file1, $dir1)

$wc.DownloadFile($file2, $dir2)

--------------------------------

 

I wasn't sure if it has to do with using objects on the command (don't think
that makes a difference). I've tried out-file, start-transcript, verbose in
various attempts but each command has resulted in the same blank text file.
Just wondering if there is a better way. The goal is to get basic "file
downloaded success" and the path it downloaded to, just so we can confirm
if/when review is necessary. The script works to grab the files and place
them in the appropriate folders, just want log output so other people can
see it, too.

 

Thanks.

-Geoff

 

Confidentiality Notice: This is a transmission from Community Hospital of
the Monterey Peninsula. This message and any attached documents may be
confidential and contain information protected by state and federal medical
privacy statutes. They are intended only for the use of the addressee. If
you are not the intended recipient, any disclosure, copying, or distribution
of this information is strictly prohibited. If you received this
transmission in error, please accept our apologies and notify the sender.
Thank you. 


================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1 



================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

Reply via email to