我正在写一个PowerShell中的脚本来拉起UPS网站并检查包裹是否已送达,如果是,则将交付的跟踪号码存入.csv,但似乎要么存款所有包裹无论交货状态如何,或者根本不包括任何交付状态。更糟糕的是,我在无法更新的工作计算机上使用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>
&#13;
**编辑**
我现在已获得更新powershell的权限,并且正在运行带有所有默认模块的v5.0。然而,我仍然遇到同样的问题。
答案 0 :(得分: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
}
}
}