|
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 .\result.csv -Append
}
else {
"lenovo" | Out-File -FilePath .\result.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 .\result.csv -Append
}
catch [System.Management.Automation.RuntimeException] {
"The serail no. is Lenovo!" | Out-File -FilePath .\result.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">\d\d\d\d-\d\d-\d\d') | Foreach-object { #正则表达式
$_.value.substring($_.value.length - 10, 10)
}
}
2015-5-4写的代码(Dell&Lenovo)
$lenovo_parttern = "\b\w{10}\b"
$dell_parttern = "\b\w{7}\b"
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 .\result.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">\d\d\d\d-\d\d-\d\d') | Foreach-object {
$_.value.substring($_.value.length - 10, 10) | Out-File -FilePath .\result.csv -Append
}
}
else {"others" | Out-File -FilePath .\result.csv -Append}
}
参考资料:
http://blog.iyunv.com/itanders/article/details/32707483
http://stackoverflow.com/questions/6779186/powershell-try-catch-finally |
|
|