Search code examples
powershellinternet-explorerpowershell-5.0powershell-1.0

Checking website for delivery status returning all packages regardless of status


I am working on writing a script in powershell to pull up the UPS website and check if a package is delivered or not, and if it is, to deposit the delivered tracking numbers into a .csv, however it seems to either deposit all the packages regardless of delivery status or none of them at all. To make matters worse, I'm working with powershell v1.0 on a work computer I cannot update, with no modules other than AppLocker, BitsTransfer, PSDiagnostics, and TroubleshootingPack. Here is my code:

$ie = New-Object -com internetexplorer.application
$ie.visible= $false
$ie.silent= $true
$file= import-csv "C:\**FILEPATH**.csv"
$newfile= New-Item "C:\**FILEPATH**$(get-date -format dd-MM-yyyy)_delivered.csv" -type file -force
foreach ($i in $file) 
    {$tagnum= $i | select-object -expandproperty "Tag Number"
    $trackingid = $i | select-object -expandproperty "Tracking ID"
    $refnum= $i | select-object -expandproperty "Attribute Ref #"
    $findest= $i | select-object -expandproperty "Final Dest"
    if ($trackingid.length -gt 1)
        {$ie.navigate("**URL**=$trackingid")
        while($ie.ReadyState -ne 4) {start-sleep -m 100}
        $trackstate= $ie.getproperty("st_del_en_us")
        if($trackstate -ne "Delivered"){
        break}
        elseif($trackstate="Delivered"){
            "$tagnum, $findest, $trackingid, $refnum" | Add-Content $newfile
        }
    }
}
$ie.close
[system.runtime.interopservices.marshal]::releasecomobject($ie)
remove-variable ie

Here is the applicable source code for the website:

<SPAN><A class="infoAnchor btnIconR hozAnchor" id=tt_spStatus onclick="javascript:return false;" href="javascript:void(0)" jQuery111105469456426992596="209">Delivered </A>
<DIV id=ttc_bullpenspStatus style="DISPLAY: none">
<DIV id=ttc_tt_spStatus><!-- cms: id="st_del_en_us" actiontype="0" -->
<H3>Delivered </H3>UPS has delivered the shipment. <BR><BR>
<TABLE width="100%" border=0 cellSpacing=0 cellPadding=0>
<TBODY>
<TR>
<TD><A><SPAN class=standardheader></SPAN><BR></A></TD></TR></TBODY></TABLE>
<TABLE width="100%" border=0 cellSpacing=0 cellPadding=0>
<TBODY>
<TR>
<TD colSpan=3>Residential deliveries that do not require a signature may be left in a safe place, out of sight and out of weather, at the driver's discretion. This could include the front porch, side door, back porch, or garage area. If you have instructed the driver to leave the shipment with a neighbor or leasing office, this would be noted on a yellow UPS InfoNotice left by the driver.</TD></TR>
<TR>
<TD colSpan=3><IMG width=1 height=10 alt="" src="/img/1.gif" border=0></TD></TR></TBODY></TABLE>
<TABLE width="100%" border=0 cellSpacing=0 cellPadding=0>
<TBODY>
<TR>
<TD></TD></TR></TBODY></TABLE>
<TABLE width="100%" border=0 cellSpacing=0 cellPadding=0>
<TBODY>
<TR>
<TD colSpan=3><BR></TD></TR></TBODY></TABLE></DIV></DIV></SPAN>

** EDIT **

I have now been given access to update powershell and am running v5.0 with all the default modules. I am however still having the same issue.


Solution

  • After updating powershell I was able to change things around a bit and get the script working. Here's the final product for anyone that might be interested:

    $file= import-csv "**FILEPATH**\Intransit_report.csv"
    $newfile= New-Item "**FILEPATH**\$(get-date -format dd-MM-yyyy)_delivered.csv" -type file -force -value "Tag Number,Final Dest,Tracking ID,Reference Number"
    $exceptionfile= New-Item "**FILEPATH**\$(get-date -format dd-MM-yyyy)_delivery_exceptions.csv" -type file -force -value "Tag Number,Final Dest,Tracking ID,Reference Number"
    foreach ($i in $file) {
        $tagnum= $i | select-object -expandproperty "Tag Number"
        $trackingid = $i | select-object -expandproperty "Tracking ID"
        $refnum= $i | select-object -expandproperty "Attribute Ref #"
        $findest= $i | select-object -expandproperty "Final Dest"
        if ($trackingid.length -gt 1){
            $uri= "**URI**=$trackingid"
            $html= Invoke-Webrequest -URI $uri
            $fields= $HTML.ParsedHtml
            $trackstate= $fields.getElementByID("ttc_tt_spStatus")
            if($trackstate.innerText -like "*Business Days*"){
            break}
            elseif($trackstate.innerText -like "Delivered*"){
                "$tagnum, $findest, $trackingid, $refnum" | Add-Content $newfile
            }
            elseif($trackstate.innerText -like "*Attempt Made*"){
                "$tagnum, $findest, $trackingid, $refnum" | Add-Content $exceptionfile
            }
        }
    }