2015-4-6写的代码(Dell), 不知道如何对报错进行友好化处理,于是采用了"非空"和"非空的补集"处理方式.
$service = New-WebServiceProxy -Uri http://143.166.84.118/services/assetservice.asmx?WSDL $guid = [Guid]::NewGuid() Import-Csv -Path .sn.csv | foreach { $info = $service.GetAssetInformation($guid,'diy',$_.sn) if ($info -ne $null) { $result = $info.Entitlements | foreach { $_.EndDate } | Measure-Object -Maximum $result.Maximum.ToString("yyyy/M/d") | Out-File -FilePath . esult.csv -Append } else { "lenovo" | Out-File -FilePath . esult.csv -Append #联想电脑暂时不知道如何获取保修期 } }
2015-4-21写的代码(Dell), 含Try, Catch, Finally用法
$service = New-WebServiceProxy -Uri http://143.166.84.118/services/assetservice.asmx?WSDL $guid = [Guid]::NewGuid() Import-Csv -Path .sn.csv | foreach { try { $info = $service.GetAssetInformation($guid,'diy',$_.sn) $result = $info.Entitlements | foreach { $_.EndDate } | Measure-Object -Maximum $result.Maximum.ToShortDateString() | Out-File -FilePath . esult.csv -Append } catch [System.Management.Automation.RuntimeException] { "The serail no. is Lenovo!" | Out-File -FilePath . esult.csv -Append } }
2015-5-4写的代码(Lenovo)
Import-Csv -Path .ln.csv | ForEach-Object { $webreq = Invoke-WebRequest ("http://support1.lenovo.com.cn/lenovo/wsi/usercenter/computersearch/machinesearch.aspx?intcmp=index&id=" + $_.sn + "&showradio=1&showdriver=no") $gc = $webreq.Content [regex]::Matches("$gc",'<span id="lblOnSiteEndDate_LK">dddd-dd-dd') | Foreach-object { #正则表达式 $_.value.substring($_.value.length - 10, 10) } }
2015-5-4写的代码(Dell & Lenovo)
$lenovo_parttern = "w{10}" $dell_parttern = "w{7}" Import-Csv -Path .sn.csv | ForEach-Object { if(($_.sn -match $dell_parttern) -eq $true) { $service = New-WebServiceProxy -Uri http://143.166.84.118/services/assetservice.asmx?WSDL $guid = [Guid]::NewGuid() $info = $service.GetAssetInformation($guid,'diy',$_.sn) $result = $info.Entitlements | foreach { $_.EndDate } | Measure-Object -Maximum $result.Maximum.ToShortDateString() | Out-File -FilePath . esult.csv -Append } elseif(($_.sn -match $lenovo_parttern) -eq $true) { $webreq = Invoke-WebRequest ("http://support1.lenovo.com.cn/lenovo/wsi/usercenter/computersearch/machinesearch.aspx?intcmp=index&id=" + $_.sn + "&showradio=1&showdriver=no") $gc = $webreq.Content [regex]::Matches("$gc",'<span id="lblOnSiteEndDate_LK">dddd-dd-dd') | Foreach-object { $_.value.substring($_.value.length - 10, 10) | Out-File -FilePath . esult.csv -Append } } else {"others" | Out-File -FilePath . esult.csv -Append} }
2017-3-27写的代码(之前Dell的查询方式已经失效,现在需要自己去Dell开发者网站申请API Key,人工审核通过后才行)
# https://api.dell.com/support/assetinfo/v4/getassetwarranty/4V11Q2X?apikey=eXXXXXXXXXX3 # https://sandbox.api.dell.com/support/assetinfo/v4/getassetwarranty/4V11Q2X?apikey=eXXXXXXXXXX3 $apikey = "eXXXXXXXXXX3" "52WDVB2","JBBWTF3" | foreach { $url = "https://sandbox.api.dell.com/support/assetinfo/v4/getassetwarranty/" + $_ + "?apikey=" + $apikey $result = (Invoke-WebRequest -Uri $url).content | ConvertFrom-Json <# 1. Get details of warranty #> $result.AssetWarrantyResponse.AssetEntitlementData | select @{n='ServiceTag';e={$result.AssetWarrantyResponse.assetheaderdata.ServiceTag}}, @{n='ShipDate';e={$result.AssetWarrantyResponse.assetheaderdata.ShipDate.Substring(0,10)}}, ServiceLevelDescription, @{n='StartDate';e={$_.StartDate.substring(0,10)}}, @{n='EndDate';e={$_.EndDate.substring(0,10)}} | sort EndDate <# 2. Get the warranty date $result | select @{n='ServiceTag';e={$result.AssetWarrantyResponse.assetheaderdata.ServiceTag}}, @{n='ExpiredDate';e={$result.AssetWarrantyResponse.AssetEntitlementData[0].EndDate.Substring(0,10)}} #> }
参考资料:
http://blog.csdn.net/itanders/article/details/32707483
http://stackoverflow.com/questions/6779186/powershell-try-catch-finally