Posts By Subject

Wednesday, March 4, 2015

Get model information for Dell PCs via Powershell

I was recently tasked to get inventory information of all of the project staff's computers. Since there wasn't any inventory software running in the desktop environment, I had to use good ole' sneakernet to get all of the service tags. Once I had them all, I promptly decided that there was no way I was manually typing all of this in and getting the required info from Dell. This lead me to stumble on a script posted here. The script is a modified version of an older one and it works very well. The best thing is that it makes exporting to CSV a cinch. However, the script doesn't allow one to get the model information. Unfortunately, I didn't write that down when I collected the service tags, so modifying the script is my only choice. After inspection of the script, it turns out that it uses a pipe to the Select-Object -ExpandProperty command to get the warranty information. However, Dell's server doesn't put the model in that particular object's property. The correct property to expand is AssetHeaderData, which will give you the model and ship date, among other things. After finishing my task, I figured I'd make it a whole lot easier for anyone else looking ot do the same thing and already have the required service tags. If you are trying to look up PCs via computer names, there are other scripts out there that do so via WMI or AD. This script only works on your PC or any service tags you provide it:

[CmdletBinding()]
param(
    [parameter(Mandatory=$false)]
    [string]$ServiceTag,
    [parameter(Mandatory=$false)]
    [string]$ComputerName,
    [parameter(Mandatory=$false)]
    [switch]$ExportCSV,
    [parameter(Mandatory=$false)]
    [string]$ImportFile
)

function Get-DellWarrantyInfo {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$False,Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [alias("SerialNumber")]
        [string[]]$GetServiceTag
    )
    Process {
        if ($ServiceTag) {
            if ($ServiceTag.Length -ne 7) {
                Write-Warning "The specified service tag wasn't entered correctly"
                break
            }
        }
    $WebProxy = New-WebServiceProxy -Uri "http://xserv.dell.com/services/AssetService.asmx?WSDL" -UseDefaultCredential
    $WebProxy.Url = "http://xserv.dell.com/services/AssetService.asmx"
    $WarrantyInformation = $WebProxy.GetAssetInformation(([guid]::NewGuid()).Guid, "Dell Warranty", $GetServiceTag)
    $WarrantyInformation | Select-Object -ExpandProperty AssetHeaderData
    return $WarrantyInformation
    }
}

if ($ServiceTag) {
    if (($ComputerName) -OR ($ExportCSV) -OR ($ImportFile)) {
        Write-Warning "You can't combine the ServiceTag parameter with other parameters"
    }
    else {
        $WarrantyObject = Get-DellWarrantyInfo -GetServiceTag $ServiceTag | Select-Object @{Label="ServiceTag";Expression={$ServiceTag}},SystemModel,SystemShipDate
        $WarrantyObject[0,1] #Remove [0,1] to get everything
    }
}

if ($ComputerName) {
    if (($ServiceTag) -OR ($ExportCSV) -OR ($ImportFile)) {
        Write-Warning "You can't combine the ComputerName parameter with other parameters"
    }
    else {
        [string]$SerialNumber = (Get-WmiObject -Namespace "root\cimv2" -Class Win32_SystemEnclosure -ComputerName $ComputerName).SerialNumber
        $WarrantyObject = Get-DellWarrantyInfo -GetServiceTag $SerialNumber | Select-Object @{Label="ComputerName";Expression={$ComputerName}},SystemModel,SystemShipDate
        $WarrantyObject[0,1] #Remove [0,1] to get everything
    }
}

if (($ImportFile)) {
    if (($ServiceTag) -OR ($ComputerName)) {
        Write-Warning "You can't combine the ImportFile parameter with ServiceTag or ComputerName"
    }
    else {
        if (!(Test-Path -Path $ImportFile)) {
            Write-Warning "File not found"
            break
        }
        elseif (!$ImportFile.EndsWith(".txt")) {
            Write-Warning "You can only specify a .txt file"
            break
        }
        else {
            if (!$ExportCSV) {
                $GetServiceTagFromFile = Get-Content -Path $ImportFile
                foreach ($ServiceTags in $GetServiceTagFromFile) {
                    $WarrantyObject = Get-DellWarrantyInfo -GetServiceTag $ServiceTags | Select-Object ServiceTag,SystemModel,SystemShipDate
                    $WarrantyObject[0,1] #Remove [0,1] to get everything
                }
            }
            elseif ($ExportCSV) {
                $GetServiceTagFromFile = Get-Content -Path $ImportFile
                $ExportPath = Read-Host "Enter a path to export the results"
                $ExportFileName = "WarrantyInfo.csv"
                foreach ($ServiceTags in $GetServiceTagFromFile) {
                    $WarrantyObject = Get-DellWarrantyInfo -GetServiceTag $ServiceTags | Select-Object ServiceTag,SystemModel,SystemShipDate
                    if (!(Test-Path -Path $ExportPath)) {
                        Write-Warning "Path not found"
                        break
                    }
                    else {
                        $FullExportPath = Join-Path -Path $ExportPath -ChildPath $ExportFileName
                        $WarrantyObject[0,1] | Export-Csv -Path $FullExportPath -Delimiter "," -NoTypeInformation -Append #Remove [0,1] to get everything
                    }
                }
            (Get-Content $FullExportPath) | ForEach-Object { $_ -replace '"', "" } | Out-File $FullExportPath
            Write-Output "File successfully exported to $FullExportPath"
            }
        }
    }
}

Again, all I did was modify the expanded property in the function and modified the output to put out the model and ship date. All credit goes to the original authors of the script/script revisions. Later down the road I might play around with the For-Each command and get it to do both warranty and model. Until next time.

No comments:

Post a Comment