Example from -
http://codealoud.blogspot.com/2011/12/downloading-files-with-powershell.html
Just change the actions to write to a log file rather than show a
display/write-host and you should be all set.
function getfile($url, $filename)
{
$wc = New-Object System.Net.WebClient
Register-ObjectEvent -InputObject $wc -EventName DownloadProgressChanged
-SourceIdentifier WebClient.DownloadProgressChanged -Action { Write-Progress
-Activity "Downloading: $($EventArgs.ProgressPercentage)% Completed" -Status
$url -PercentComplete $EventArgs.ProgressPercentage; }
Register-ObjectEvent -InputObject $wc -EventName DownloadFileCompleted
-SourceIdentifier WebClient.DownloadFileComplete -Action { Write-Host "Download
Complete - $filename"; Unregister-Event -SourceIdentifier
WebClient.DownloadProgressChanged; Unregister-Event -SourceIdentifier
WebClient.DownloadFileComplete; }
try
{
$wc.DownloadFileAsync($url, $filename)
}
catch [System.Net.WebException]
{
Write-Host("Cannot download $url")
}
finally
{
$wc.Dispose()
}
}
$file1 = "URL"
$dir1 = "\\Share\Folder"
$file2 = "URL2"
$dir2 = "\\Share\Folder2"
getfile $file1 $dir1
getfile $file2 $dir2
From: [email protected] [mailto:[email protected]] On
Behalf Of Kelley, Matthew
Sent: Friday, May 30, 2014 11:21 AM
To: '[email protected]'
Subject: [powershell] RE: Logging Question
It looks like there is no return or confirmation from DownloadFile. You will
have to use DownloadFileAsync and watch for the DownloadFileCompleted event to
fire.
Here is a post giving some examples of how to implement system.net.webclient
with powershell:
http://stackoverflow.com/questions/4926060/powershell-runspace-problem-with-downloadfileasync
hope that helps!
Matt
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Orlebeck, Geoffrey
Sent: Friday, May 30, 2014 10:35 AM
To: [email protected]<mailto:[email protected]>
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<file:///\\Share\Folder>"
$file2 = "URL2"
$dir2 = "\\Share\Folder2<file:///\\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
**********************************************************
Electronic Mail is not secure, may not be read every day, and should not be
used for urgent or sensitive issues
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1
**********************************************************
Electronic Mail is not secure, may not be read every day, and should not be
used for urgent or sensitive issues
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1