Powershell Sort issue

I have a powershell script to query AD and get all computers that do not have the word "Server" in the operating System, it pings the computer to see if it's on line if it's on line and the manufacturer is Dell it get the warranty information and a few other things.  

The script works great, but my problem is they have renewed the warranty on some of the machines so a query of the extended warranties with prosupport will turn up more then one value for these machine.

So my challenge is to add another section to the script that will sort the csv file by unique serial number, with the most current warranty information if it has 2 extended warranties or edit the script so that only the most current warranty information is used.   I have attached a copy of my output file in hopes that would clear up what I mean

Basically I want to run the script 3 to 4 times a day for a few weeks to make sure I get all the machines and only keep the most current information


My Powershell script AD_Audit.ps1
import-module ActiveDirectory
 
("Name" + "," + "SerialNumber" + "," + "WarrantyExpiration" + "," + "Description" + "," + "UserName" + "," + "OS" + "," + "Mode") | Out-File D:\Audit\presort.csv -append

ForEach ($DNSHostName in (Get-ADComputer -Filter { OperatingSystem -NotLike "*Server*"} | Select-Object DNSHostName | Foreach { $_.DNSHostName})) {
$compname = $DNSHostName 
Write-Host "Processing $compname"
$ping = gwmi Win32_PingStatus -filter "Address='$compname'"
if($ping.StatusCode -ne 0) {$compname | out-file -FilePath D:\Audit\No_response.txt -append; continue}

$CompSN = gwmi -computer $compname Win32_BIOS | Select-Object SerialNumber | ForEach { $_.SerialNumber}
$CompOS = gwmi -computer $compname Win32_OperatingSystem | Select-Object Caption | ForEach {$_.Caption}
$CompUSR = gwmi -computer $compname Win32_ComputerSystem | Select-Object UserName | ForEach {$_.UserName}
$CompNam = gwmi -computer $compname Win32_ComputerSystem |Select-Object Name | ForEach {$_.Name}
$CompMan = gwmi -computer $compname Win32_ComputerSystem |Select-Object Manufacturer -filter {Manfacturer -Like "*Dell*"}| ForEach {$_.Manufacturer}
$CompMod = gwmi -computer $compname Win32_ComputerSystem |Select-Object Model | ForEach {$_.Model}
$CompDesc = gwmi -Computer $compname Win32_OperatingSystem | Select-Object Description | ForEach {$_.Description}
$apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
$url = "https://api.dell.com/support/v2/assetinfo/warranty/tags?svctags=${CompSN}&apikey=${apiKey}"
$req = Invoke-RestMethod -URI $url -Method GET
$CompWar = $req.getassetwarrantyresponse.getassetwarrantyresult.response.dellasset.warranties.warranty | Where-Object { ($_.EntitlementType -eq "EXTENDED") -and $_.ServiceLevelCode -eq "ND"}
$CompWarExp = $CompWar | Select-Object @{label="EndDate";Expression={$_.EndDate.ToString().SubString(0,10)}} | ForEach {$_.EndDate}
 
($CompNam + "," + $CompSN + "," + $CompWarExp + "," + $CompDesc + "," + $CompUSR + "," + $CompOS + "," + $CompMod) | Out-File D:\Audit\presort.csv -append
}

Open in new window


Here is the output presort.csv
LVL 2
erobbyAsked:
Who is Participating?
 
Chris DentPowerShell DeveloperCommented:
What would possibly be nice is if you sorted these based on the EndDate:
$CompWar = $req.getassetwarrantyresponse.getassetwarrantyresult.response.dellasset.warranties.warranty | Where-Object { ($_.EntitlementType -eq "EXTENDED") -and $_.ServiceLevelCode -eq "ND"}
# This should get the most recent
$CompWarExp = $CompWar | ForEach-Object { Get-Date ($_.EndDate.ToString().SubString(0,10)) } | Sort-Object -Descending | Select-Object -First 1

Open in new window

There's a lot that can be done to neaten up what you have above, for example using Export-Csv would be much neater than spinning your own CSV. Interested?

Chris
0
 
RobSampsonCommented:
>> There's a lot that can be done to neaten up what you have above

Not to mention you make an individual WMI call for each property you receive, even from the same class. That is unnecessary.

Rob.
0
 
erobbyAuthor Commented:
Chris,

I tried that in a separate statement, but your suggestions looks great

Rob,

Yeah still learning powershell, but I get your point.

Will try both suggestion and get back
0
 
erobbyAuthor Commented:
Chris,

Thanks that did exactly what I needed it to do.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.