检查网站的交付状态返回所有包裹,无论状态如何

Checking website for delivery status returning all packages regardless of status

我正在用 powershell 编写一个脚本来打开 UPS 网站并检查包裹是否已送达,如果已送达,则将送达的跟踪号码存入 .csv,但它似乎无论交付状态如何,要么存放所有包裹,要么存放 none 个包裹。更糟糕的是,我在无法更新的工作计算机上使用 powershell v1.0,除了 AppLocker、BitsTransfer、PSDiagnostics 和 TroubleshootingPack 之外没有其他模块。这是我的代码:

$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

以下是该网站的适用源代码:

<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>

** 编辑 **

我现在已获得更新 powershell 的权限,并且 运行 v5.0 包含所有默认模块。但是我仍然遇到同样的问题。

更新 powershell 后,我可以稍作更改并使脚本正常运行。这是任何可能感兴趣的人的最终产品:

$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
        }
    }
}