How to Use PowerShell to Complete an Ongoing Task

Share on facebook
Share on twitter
Share on linkedin
Share on email

You Get a Script, You Get a Script, Everyone Gets a Script!

Scripts, scripts, scripts, scripts! Let’s talk about Scripts! Recently, at Mercury New Media we had a client request that we upload IIS log files to their server on a nightly basis. The request was to rename IIS logs to follow the requested naming convention and to upload the log files in a zip folder each night. We tossed a few ideas around like console apps, custom services and ultimately settled on a PowerShell script executed by our VM’s task scheduler to fulfill our client’s need. A simple yet effective solution that would be portable and flexible.

Setup

Before we started working on uploading our client’s files, we made sure that were able to use the PowerShell ISE for v3 and v4 of PowerShell. This is installed on Windows 8 by default which meant there was no download required. Instead all we had to do was launch the ISE and start to dig in! To do this you can search your Windows 8 Machine for PowerShell ISE and be on your merry way!

Step One

To accommodate the client’s needs, we first took a look at renaming IIS log files and zipping them up into the request archives. Luckily, IIS conveniently names log files with a dated naming convention located within its respected site folder making it easy to sort through log files and select the appropriate log for the day. Once located, we could start naming and zipping files to our hearts content! A fairly straightforward task that can be executed with the PowerShell script below:

$date = (get-date).AddDays(-1).ToString("yyMMdd")
$ZipTimestamp = (get-date).AddDays(-1).ToString("yyyyMMdd")
$DestZip='C:\inetpub\logs\LogFiles\W3SVC32\'
$Dest = "C:\inetpub\logs\LogFiles\W3SVC32\u_ex" + $date + ".log"
$ZipFileName  = $DestZip + $ZipTimestamp + "Naming Convention" + ".zip" set-content $ZipFileName ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
# Wait for the zip file to be created.
while (!(Test-Path -PathType leaf -Path $ZipFileName))
{    
    Start-Sleep -Milliseconds 20
} 
$ZipFile = (new-object -com shell.application).NameSpace($ZipFileName)
Write-Output (">> Waiting Compression : " + $ZipFileName)      
$ZipFile.CopyHere($Dest)

The script locates our log files, renames them to the clients requested naming conventions, and then zips the files all in a matter of milliseconds. Pretty quick and easy if you ask me! 

Step Two

Next, we needed to upload the files to our clients FTP server! Another task that can be easily tackled by our beloved PowerShell script!

The client was kind enough to provide FTP information so all we needed to do was grab the files and ship them off to their new home! Now if you notice from the script above, the file is copied to our directory, but never removed. In the next step, we also decided we should remove the unnecessary zipped files once uploaded to avoid cluttering our log folders. The PowerShell script below takes our fresh zip files, uploads them, and then removes them from the directory:

#Send File Through Internet Tubes
$file = $ZipFile.Title
$ftp = [System.Net.FtpWebRequest]::Create("ftp://clientdirectory/$file")
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$ftp.Credentials = new-object System.Net.NetworkCredential("username","password")
$ftp.UseBinary = $true
$ftp.UsePassive = $true
$content = [System.IO.File]::ReadAllBytes("C:\inetpub\logs\LogFiles\W3SVC32\$file")
$ftp.ContentLength = $content.Length
$rs = $ftp.GetRequestStream()
$rs.Write($content, 0, $content.Length)
$rs.Close()
$rs.Dispose()
Remove-Item C:\inetpub\logs\LogFiles\W3SVC32\$file

Wrap Up

The script sends our files to the new client directory and then removes the file from our local folder as to not clog up our directories. We scheduled this PowerShell script to run each morning at 12:01 AM by using Windows Task Scheduler. A simple script was all we needed to tackle this ongoing task!

No Comments

Leave a Comment

Your email address will not be published. Required fields are marked *