Monday, February 25, 2013

Powershell- I'm starting to like you...

Latest script that checks to see if a file has already been downloaded.

If the file is there, do nothing.

Else, download the file only IF it exists. This particular vendor does not upload their file on the same date/time every month, so it made it a little trickier.

$dMonth= (Get-Date).AddMonths(-1).ToString("yyyyMM")

$dMonth1= (Get-Date).AddMonths(-1).ToString("MMM")
$dYear= (Get-Date).AddMonths(-1).ToString("yyyy")

<# Download #>
If (Test-Path "E:\AutoCount\$dMonth.txt")
{
 Exit
}

Else
{
<# Set the Location #>
Set-Location -Path E:\AutoCount

<# The Webclient object does the downloading #>
$WebClient = New-Object System.Net.Webclient

<# File we want to download #>
$source = "https://downloads.autocount.com/AutoOneAC/$dMonth1 $dYear TX Statewide VehicleRegistrationsNoVIN.txt"

<# Where we want to download file to #>
$destination = "E:\AutoCount\$dMonth.txt"

<# Logon credentials #>
$WebClient.Credentials = New-Object System.Net.NetworkCredential("UserName","password")

$WebClient.DownloadFile($source,$destination)

Exit
}

No comments:

Post a Comment