Guest User

Dell Warranty Check

a guest
Aug 30th, 2023
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 7.15 KB | Software | 0 0
  1.  
  2. Add-Type -AssemblyName System.Windows.Forms
  3.  
  4. # Initialize an array to hold the combined data
  5. $csvData = New-Object System.Collections.ArrayList
  6. $allWarrantyInfo = @()  # Array to hold all warranty information
  7.  
  8. # Function to extract information from the Info field
  9. function ParseInfoField {
  10.     param (
  11.         [string]$Info
  12.     )
  13.  
  14.     # Splitting additional details at semicolon (;)
  15.     $Details = $Info -split ';'
  16.     $DetailsHash = @{}
  17.     foreach ($detail in $Details) {
  18.         $key, $value = $detail -split '=', 2
  19.         $DetailsHash[$key] = $value
  20.     }
  21.     return $DetailsHash
  22. }
  23.  
  24. # Function to query computers from the selected OU and display the results
  25. function QueryComputersAndDisplayResults {
  26.     param (
  27.         [string]$selectedOU
  28.     )
  29.  
  30.  
  31.  
  32.   # Get all computers from the selected OU in Active Directory
  33.     $computers = Get-ADComputer -Filter * -Properties * -SearchBase $selectedOU
  34.  
  35.     # Process each computer object and extract desired information
  36.     $result = foreach ($computer in $computers) {
  37.         # Parsing the Info field
  38.         $DetailsHash = ParseInfoField -Info $computer.info
  39.  
  40.          # Extract the OU portion between "OU=Notebooks" and "OU=FORSCOM" from distinguishedName
  41.         $ouExtracted = ""
  42.         $startIndex = $computer.distinguishedName.IndexOf("OU=Notebooks")
  43.         $endIndex = $computer.distinguishedName.IndexOf(",OU=Fake")
  44.         if ($startIndex -ne -1 -and $endIndex -ne -1) {
  45.             $startIndex += 16  # Move the start index past "OU=Notebooks,"
  46.             $ouExtracted = $computer.distinguishedName.Substring($startIndex, $endIndex - $startIndex)
  47.         }
  48.  
  49.         # Creating a custom object with required properties
  50.         $ComputerData = [PSCustomObject]@{
  51.             Hostname = $computer.Name
  52.             MakeModel = $DetailsHash['Sys']
  53.             SN = $DetailsHash['SN']
  54.             TPM = $DetailsHash['TPM']
  55.             Unit = $ouExtracted  # Add the extracted OU portion to the object
  56.             }
  57.         # Add the computer data to the $csvData array
  58.         $csvData.Add($ComputerData)
  59. }
  60.  
  61.    }
  62.  
  63. # Function to get Dell warranty information
  64. function Get-DellWarrantyInfo {
  65.     Param(
  66.         [Parameter(Mandatory = $true)]
  67.         $ServiceTags,
  68.         [Parameter(Mandatory = $true)]
  69.         $ApiKey,
  70.         [Parameter(Mandatory = $true)]
  71.         $KeySecret
  72.     )
  73.     [String]$servicetags = $ServiceTags -join ", "
  74.  
  75.     $AuthURI = "https://apigtwb2c.us.dell.com/auth/oauth/v2/token"
  76.     $OAuth = "$ApiKey`:$KeySecret"
  77.     $Bytes = [System.Text.Encoding]::ASCII.GetBytes($OAuth)
  78.     $EncodedOAuth = [Convert]::ToBase64String($Bytes)
  79.     $Headers = @{ }
  80.     $Headers.Add("authorization", "Basic $EncodedOAuth")
  81.     $Authbody = 'grant_type=client_credentials'
  82.     [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  83.     Try {
  84.         $AuthResult = Invoke-RESTMethod -Method Post -Uri $AuthURI -Body $AuthBody -Headers $Headers
  85.         $Global:token = $AuthResult.access_token
  86.     }
  87.     Catch {
  88.         $ErrorMessage = $Error[0]
  89.         Write-Error $ErrorMessage
  90.         BREAK      
  91.     }
  92.     Write-Host "Access Token is: $token`n"
  93.  
  94.     $headers = @{"Accept" = "application/json" }
  95.     $headers.Add("Authorization", "Bearer $token")
  96.  
  97.     $params = @{ }
  98.     $params = @{servicetags = $servicetags; Method = "GET" }
  99.  
  100.     $response = Invoke-RestMethod -Uri "https://apigtwb2c.us.dell.com/PROD/sbil/eapi/v5/asset-entitlements" -Headers $headers -Body $params -Method Get -ContentType "application/json"
  101.  
  102.     $warrantyInfoArray = @()
  103.  
  104.     foreach ($Record in $response) {
  105.         $servicetag = $Record.servicetag
  106.         $Json = $Record | ConvertTo-Json
  107.         $Record = $Json | ConvertFrom-Json
  108.         $Device = $Record.productLineDescription
  109.         $EndDate = ($Record.entitlements | Select -Last 1).endDate
  110.         $Support = ($Record.entitlements | Select -Last 1).serviceLevelDescription
  111.         $EndDate = $EndDate | Get-Date -f "MM-dd-y"
  112.         $today = Get-Date
  113.  
  114.         $warrantyInfo = [PSCustomObject]@{
  115.             ServiceTag = $servicetag
  116.             Device = $Device
  117.             EndDate = $EndDate
  118.         }
  119.         $warrantyInfoArray += $warrantyInfo
  120.     }
  121.  
  122.     return $warrantyInfoArray
  123. }
  124.  
  125. # Extract service tag values from array
  126. $serviceTagsFromCSV = $csvData.SN -join ", "
  127.  
  128. # Define the list of OUs
  129. $OUsToQuery = @(
  130.     "OU=Notebooks,OU=Section,OU=Fake,DC=fake,DC=com",
  131.     "OU=Notebooks,OU=Section2,OU=Fake,DC=fake,DC=com",
  132.     "OU=Notebooks,OU=Section3,OU=Fake,DC=fake,DC=com"
  133. )
  134.  
  135. # Create the Form
  136. $form = New-Object Windows.Forms.Form
  137. $form.Text = "Computer Query App"
  138. $form.Width = 400
  139. $form.Height = 200
  140. $form.FormBorderStyle = "FixedSingle"
  141. $form.MaximizeBox = $false
  142.  
  143. # Create the OU selection dropdown
  144. $dropdownOU = New-Object Windows.Forms.ComboBox
  145. $dropdownOU.Location = New-Object Drawing.Point 20, 30
  146. $dropdownOU.Width = 350
  147. $OUsToQuery | ForEach-Object { $dropdownOU.Items.Add($_) }
  148. $form.Controls.Add($dropdownOU)
  149.  
  150. # Create the "Query" button
  151. $btnQuery = New-Object Windows.Forms.Button
  152. $btnQuery.Text = "Query"
  153. $btnQuery.Location = New-Object Drawing.Point 20, 80
  154. $btnQuery.Add_Click({
  155.  
  156.     $selectedOU = $dropdownOU.SelectedItem
  157.     QueryComputersAndDisplayResults -selectedOU $selectedOU
  158.  
  159.     # Replace these values with your API key and key secret
  160.     $ApiKey = "Paste Key"
  161.     $KeySecret = "Paste Secret"
  162.     # Extract service tag values from array
  163. $serviceTagsFromCSV = $csvData.SN -split ','
  164.  
  165. # Define the batch size
  166. $batchSize = 100
  167.  
  168. # Create an array to hold all warranty information
  169. $allWarrantyInfo = @()
  170.  
  171. # Process service tags in batches
  172. for ($i = 0; $i -lt $serviceTagsFromCSV.Length; $i += $batchSize) {
  173.     $batchServiceTags = $serviceTagsFromCSV[$i..($i + $batchSize - 1)] | Where-Object { $_ -ne '' }
  174.  
  175.     # Call the function with the extracted service tag batch
  176.     $warrantyInfoBatch = Get-DellWarrantyInfo -ServiceTags $batchServiceTags -ApiKey $ApiKey -KeySecret $KeySecret
  177.  
  178.     # Add the warranty information from the batch to the array
  179.     $allWarrantyInfo += $warrantyInfoBatch
  180. }
  181.  
  182. # Combine computer data with warranty information and save to CSV
  183. $combinedData = foreach ($computerData in $csvData) {
  184.     $warrantyInfo = $allWarrantyInfo | Where-Object { $_.ServiceTag -eq $computerData.SN }
  185.     if ($warrantyInfo) {
  186.         $computerData | Select-Object *, @{
  187.             Name = 'Model'
  188.             Expression = { $warrantyInfo.Device }
  189.         }, @{
  190.             Name = 'WarrantyExpiration'
  191.             Expression = { $warrantyInfo.EndDate }
  192.         }
  193.     } else {
  194.         $computerData
  195.     }
  196. }
  197.  
  198. # Save Data to CSV
  199. $saveFileDialog = New-Object Windows.Forms.SaveFileDialog
  200. $saveFileDialog.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*"
  201. $saveFileDialog.Title = "Save the results to CSV file"
  202. $saveFileDialog.ShowDialog() | Out-Null
  203.  
  204. if ($saveFileDialog.FileName) {
  205.     $combinedData | Export-Csv -Path $saveFileDialog.FileName -NoTypeInformation
  206.     [System.Windows.Forms.MessageBox]::Show("Results exported to $($saveFileDialog.FileName)", "Export Successful", "OK", "Information")
  207. }
  208. })
  209. $form.Controls.Add($btnQuery)
  210.  
  211. # Show the form
  212. $form.ShowDialog()
Add Comment
Please, Sign In to add comment