Get Dell ESX host warranty info via PowerCLI version 3

Summary:
Had a script that was doing a screen scrape of Dell's support page for warranty info.  Now they require a log in.  So it was time to finally revise my script to use Dell's wsdl (SOAP) interface.

Example:


Details:
I was able to simplify the script a bit using the code provided by Jon Gurgul to pull from Dell's provided SOAP service.  I copied the code into a Get-DellAssetInformation.psm1 file so I could call the function as a module.

The variable you should probably update is the $ServiceDescription variable to match the warranty type you expect from your boxes so you get an actual return of data.

I simply plugged this into my existing code removing the screen scrape code and this is what I ended up with:

Script:


Add-PSsnapin VMware.VimAutomation.Core
#Module code courtesy of Jon Gurgul
Import-Module C:\myModuleDirectory\Get-DellAssetInformation.psm1 $VIServer = Connect-VIServer myvCenterServer $date = Get-Date $htmpath = "C:\inetpub\wwwroot\" $csvpath = "C:\inetpub\wwwroot\csv\" $htmname = "VMHostInvReport.htm" $csvname = "VMHostInvReport.csv"
#You can find out what service descriptions are available by using calling the Get-DellAssetInformation module and seeing what ones are available to you. Like so:
#[xml]$AssetInfo = Get-DellAssetInformation servicetagname
#$AssetInfo.ArrayOfAsset.Asset.entitlements.entitlementdata | select ServiceLevelDescription
$ServiceDescription  = "4HR"


#Report Title appears in <title> section and shows in the browser window.
$ReportTitle     = "VM/ESX Host Inventory Data"

#IconPath is the icon that is rendered on supported web browsers tab or address bar
$IconPath        = "/images/mySuperAwesomeIcon.ico"

#AppleIconType, set this to "Apple-touch-icon" if you would like the iOS device to render a glass effect on top of your custom icon.
#Otherwise use "Apple-touch-icon-precomposed" to just use your stock image.  45x45 is recommended, but higher rez can be used.
$AppleIconType     = "apple-touch-icon-precomposed"

#AppleTouchIcon is the image that appears on an iOS and/or Android device when a home screen bookmark is created.
$AppleTouchIcon = "/images/apple-touch-icon.png"

#Stylesheet is the path where your CSS file lies.
$Stylesheet        = "./styles/style.css"

#SortScript is where the sorttable.js script resides.  This allows dynamic sorting of table information within the browser.
$SortScript     = "./scripts/sorttable.js"

#Used to generate encrypted xml password file
#New-VICredentialStoreItem -Host doesntreallymatter -User root -Password "" -File "C:\CredentialStore\yourESXiRootCreds.xml"
#xml is only valid w/ the account that created the xml file, so create it with the service account you plan to run w/ this.
$Creds = Get-VICredentialStoreItem -File C:\CredentialStore\yourESXiRootCreds.xml
$ESXServers = Get-VMHost -Server $VIServer
$ESXServerView = @()
$Count = $ESXServers.Count
While ($Count -gt 0)
{
$Count = $Count - 1
$VIESXHost = Connect-VIServer $ESXServers[($Count)].Name -User $Creds.User -Password $Creds.Password
If ($? -ne $true){}
Else{$ESXServerView += Get-VMHost -Server $VIESXHost | Get-View}
Disconnect-VIServer -Server $VIESXHost -Confirm:$false
}
#$ESXServerView = $ESXServers | Get-View
#$ServiceTagList = foreach($VMHost in $ESXServerView){($VMHost.hardware.systeminfo.OtherIdentifyingInfo | where {$_.IdentifierType.Key -eq "servicetag"}).identifierValue}
$ESXInfo = @()

Foreach ($ESX in $ESXServers){
$TargetESXServerView = $ESXServerView | where {$_.Name -eq $ESX.Name}
$NewObj         = "" | Select Cluster, Name, Model, Version, BIOs, ServiceTag, AssetTag, ShipDate, ExpiryDate
$NewObj.Cluster = $ESX.Parent.Name
$NewObj.Name    = $ESX.Name
$NewObj.Model    = $ESX.Model
$NewObj.Version    = $ESX.Version
$NewObj.BIOs    = (($TargetESXServerView.Runtime.HealthSystemRuntime.SystemHealthInfo.NumericSensorInfo | where {$_.Name -like "*BIOS*"  -and $_.SensorType -eq "Software Components"}).Name) -replace(".* BIOS ","") -replace(" .*","")
$NewObj.ServiceTag     = ($TargetESXServerView.hardware.systeminfo.OtherIdentifyingInfo | where {$_.IdentifierType.Key -eq "servicetag"}).identifierValue -replace(" ","")
$NewObj.AssetTag    = ($TargetESXServerView.hardware.systeminfo.OtherIdentifyingInfo | where {$_.IdentifierType.Key -eq "assettag"}).identifierValue -replace(" ","")

## Gets ServiceTag Info from Dell
$ServiceTag = $null
If ($NewObj.ServiceTag -eq $null -or $NewObj.ServiceTag -eq "unknown" -or $NewObj.ServiceTag -eq "") {$ServiceTag = $NewObj.AssetTag} Else {$ServiceTag = $NewObj.ServiceTag}

[xml]$AssetInfo = Get-DellAssetInformation $ServiceTag
$Warranty = $null
If ($Warranty -ne $null -or $Warranty -eq " " -and $ESX.Manufacturer -match "Dell"){$Warranty = "Expired"}
$Warranty = $AssetInfo.ArrayOfAsset.Asset.entitlements.entitlementdata | where {$_.DaysLeft -gt "0" -and $_.ServiceLevelDescription -match "$($ServiceDescription)" -and $_.EntitlementType -match "Active"}

$ShipDate = ([datetime]$AssetInfo.ArrayOfAsset.Asset.AssetHeaderData.SystemShipDate).toShortDateString()
$EndDate = ([datetime]$Warranty.EndDate).toShortDateString()

If ($Warranty -ne "Expired")
{
If ([int]$Warranty.DaysLeft -lt "15") {$EndDateHTM = "<p style=`"font-weight:bold;color:red`">$($EndDate)</p>"}
ElseIf ([int]$Warranty.DaysLeft -lt "90") {$EndDateHTM = "<p style=`"font-weight:bold;color:orange`">$($EndDate)</p>"}
ElseIf ([int]$Warranty.DaysLeft -gt "90") {$EndDateHTM = $EndDate}
}
If ($Warranty -eq "Expired") {$EndDateHTM = "<p style=`"font-weight:bold;color:red`">$($Warranty)</p>"}
$NewObj.ShipDate = $ShipDate $NewObj.ExpiryDate = $EndDateHTM $ESXInfo += $NewObj $ShipDate = $null $EndDate = $null $EndDateHTM = $null } $Head = "<LINK REL=`"SHORTCUT ICON`" HREF=`"$($IconPath)`"><link rel=`"$($AppleIconType)`" href=`"$($AppleTouchIcon)`"/><title>$($ReportTitle)</title><link rel=`"stylesheet`" type=`"text/css`" href=`"$($Stylesheet)`"><script src=`"$($SortScript)`"></script>" $ESXInfo | ConvertTo-Html –body "<a name=`"Data`"></a><strong>$ReportTitle $date</strong> <br><a href=./csv/$($csvname)>(Click here to download csv copy)</a>" -head "$($Head)" | foreach {$_.replace("&lt;","<").replace("&gt;",">").replace("<tr><th>","<table class=`"sortable`"><thead><tr><th>").replace("</th></tr>","</th></tr></thead><tbody>").replace("</table>","</tbody><tfoot></tfoot></table>").replace("&quot;","`"")} | Out-File -Encoding ASCII -FilePath "$($htmpath)$($htmname)" $ESXInfo | convertto-csv -NoTypeInformation | foreach {$_ -replace("<p style=`"`"font-weight:bold;color:orange`"`">","") -replace("<p style=`"`"font-weight:bold;color:red`"`">","") -replace("</p>","")} | Out-File -Encoding ASCII -FilePath "$($csvpath)$($csvname)"

Disconnect-VIServer -Confirm:$false -Server $VIServer

5 comments:

Robby said...

I'm getting an error:

Unexpected token 'Active"}
$ShipDate = ([datetime]$AssetInfo.ArrayOfAsset.Asset.AssetHeaderData.SystemShipDate).toShortDateString()
$EndDate = ([datetime]$Warranty.EndDate).toShortDateString()

If ($Warranty -ne "Expired")
{If ([int]$Warranty.DaysLeft -lt "15") {$EndDateHTM = "<p' in expression or statement.
At C:\Users\e19671\Documents\scripts\DellWarrantyInfo.ps1:77 char:58
+ $Warranty = $AssetInfo.ArrayOfAsset.Asset.entitlements.en <<<< titlementdata | where {$_.DaysLeft -gt "0" -and $_.ServiceLevelDescription -match "$($ServiceDescription)-and $_.Entit
lementType -match "Active"}
+ CategoryInfo : ParserError: (Active"}
$Ship...ndDateHTM = "<p:String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken

Chris Nakagaki said...

Sorry for the late response. I was on vacation in Japan. I left out some quotes by accident.

Here is the line it's complaining about:
$Warranty = $AssetInfo.ArrayOfAsset.Asset.entitlements.entitlementdata | where {$_.DaysLeft -gt "0" -and $_.ServiceLevelDescription -match "$($ServiceDescription)" -and $_.EntitlementType -match "Active"}

It's corrected here and I've corrected it on the post. Give it a try and let me know.

Robby said...

FINALLY getting back around to making this work. Now I'm getting the following error:

Cannot convert null to type "System.DateTime"

You cannot call a method on a null-valued expression.
Cannot convert null to type "System.DateTime".
At C:\Users\e19671\Documents\scripts\DellHost\Get-DellVMHostWarrantyInfo.ps1:96 char:70
+ $ShipDate = ([datetime]$AssetInfo.ArrayOfAsset.Asset.AssetHeaderData. <<<< SystemShipDate).toShortDateString()
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

Cannot convert null to type "System.DateTime".
At C:\Users\e19671\Documents\scripts\DellHost\Get-DellVMHostWarrantyInfo.ps1:97 char:33
+ $EndDate = ([datetime]$Warranty. <<<< EndDate).toShortDateString()
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

Robby said...

Thanks again for all the great work...I think I'm almost there!

Chris Nakagaki said...

That essentially means it was unable to find the service tag. I'd attempt to run the script up to this comment: ## Gets ServiceTag Info from Dell.

See what $ESXinfo returns. Meaning, make sure Asset Tag and Service Tag fields are populated correctly.

Next you can test the XML pull like so:
[xml]$AssetInfo = Get-DellAssetInformation "TypeYourServiceTagNumberHere"

Then you can call $AssetInfo and walk it.