Avatar of erobby
erobby
Flag for United States of America asked on

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
PowershellScripting LanguagesActive DirectoryMicrosoft ExcelDell

Avatar of undefined
Last Comment
erobby

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chris Dent

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
RobSampson

>> 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.
erobby

ASKER
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
erobby

ASKER
Chris,

Thanks that did exactly what I needed it to do.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck