Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add-Type -AssemblyName System.Windows.Forms
- # Initialize an array to hold the combined data
- $csvData = New-Object System.Collections.ArrayList
- $allWarrantyInfo = @() # Array to hold all warranty information
- # Function to extract information from the Info field
- function ParseInfoField {
- param (
- [string]$Info
- )
- # Splitting additional details at semicolon (;)
- $Details = $Info -split ';'
- $DetailsHash = @{}
- foreach ($detail in $Details) {
- $key, $value = $detail -split '=', 2
- $DetailsHash[$key] = $value
- }
- return $DetailsHash
- }
- # Function to query computers from the selected OU and display the results
- function QueryComputersAndDisplayResults {
- param (
- [string]$selectedOU
- )
- # Get all computers from the selected OU in Active Directory
- $computers = Get-ADComputer -Filter * -Properties * -SearchBase $selectedOU
- # Process each computer object and extract desired information
- $result = foreach ($computer in $computers) {
- # Parsing the Info field
- $DetailsHash = ParseInfoField -Info $computer.info
- # Extract the OU portion between "OU=Notebooks" and "OU=FORSCOM" from distinguishedName
- $ouExtracted = ""
- $startIndex = $computer.distinguishedName.IndexOf("OU=Notebooks")
- $endIndex = $computer.distinguishedName.IndexOf(",OU=Fake")
- if ($startIndex -ne -1 -and $endIndex -ne -1) {
- $startIndex += 16 # Move the start index past "OU=Notebooks,"
- $ouExtracted = $computer.distinguishedName.Substring($startIndex, $endIndex - $startIndex)
- }
- # Creating a custom object with required properties
- $ComputerData = [PSCustomObject]@{
- Hostname = $computer.Name
- MakeModel = $DetailsHash['Sys']
- SN = $DetailsHash['SN']
- TPM = $DetailsHash['TPM']
- Unit = $ouExtracted # Add the extracted OU portion to the object
- }
- # Add the computer data to the $csvData array
- $csvData.Add($ComputerData)
- }
- }
- # Function to get Dell warranty information
- function Get-DellWarrantyInfo {
- Param(
- [Parameter(Mandatory = $true)]
- $ServiceTags,
- [Parameter(Mandatory = $true)]
- $ApiKey,
- [Parameter(Mandatory = $true)]
- $KeySecret
- )
- [String]$servicetags = $ServiceTags -join ", "
- $AuthURI = "https://apigtwb2c.us.dell.com/auth/oauth/v2/token"
- $OAuth = "$ApiKey`:$KeySecret"
- $Bytes = [System.Text.Encoding]::ASCII.GetBytes($OAuth)
- $EncodedOAuth = [Convert]::ToBase64String($Bytes)
- $Headers = @{ }
- $Headers.Add("authorization", "Basic $EncodedOAuth")
- $Authbody = 'grant_type=client_credentials'
- [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
- Try {
- $AuthResult = Invoke-RESTMethod -Method Post -Uri $AuthURI -Body $AuthBody -Headers $Headers
- $Global:token = $AuthResult.access_token
- }
- Catch {
- $ErrorMessage = $Error[0]
- Write-Error $ErrorMessage
- BREAK
- }
- Write-Host "Access Token is: $token`n"
- $headers = @{"Accept" = "application/json" }
- $headers.Add("Authorization", "Bearer $token")
- $params = @{ }
- $params = @{servicetags = $servicetags; Method = "GET" }
- $response = Invoke-RestMethod -Uri "https://apigtwb2c.us.dell.com/PROD/sbil/eapi/v5/asset-entitlements" -Headers $headers -Body $params -Method Get -ContentType "application/json"
- $warrantyInfoArray = @()
- foreach ($Record in $response) {
- $servicetag = $Record.servicetag
- $Json = $Record | ConvertTo-Json
- $Record = $Json | ConvertFrom-Json
- $Device = $Record.productLineDescription
- $EndDate = ($Record.entitlements | Select -Last 1).endDate
- $Support = ($Record.entitlements | Select -Last 1).serviceLevelDescription
- $EndDate = $EndDate | Get-Date -f "MM-dd-y"
- $today = Get-Date
- $warrantyInfo = [PSCustomObject]@{
- ServiceTag = $servicetag
- Device = $Device
- EndDate = $EndDate
- }
- $warrantyInfoArray += $warrantyInfo
- }
- return $warrantyInfoArray
- }
- # Extract service tag values from array
- $serviceTagsFromCSV = $csvData.SN -join ", "
- # Define the list of OUs
- $OUsToQuery = @(
- "OU=Notebooks,OU=Section,OU=Fake,DC=fake,DC=com",
- "OU=Notebooks,OU=Section2,OU=Fake,DC=fake,DC=com",
- "OU=Notebooks,OU=Section3,OU=Fake,DC=fake,DC=com"
- )
- # Create the Form
- $form = New-Object Windows.Forms.Form
- $form.Text = "Computer Query App"
- $form.Width = 400
- $form.Height = 200
- $form.FormBorderStyle = "FixedSingle"
- $form.MaximizeBox = $false
- # Create the OU selection dropdown
- $dropdownOU = New-Object Windows.Forms.ComboBox
- $dropdownOU.Location = New-Object Drawing.Point 20, 30
- $dropdownOU.Width = 350
- $OUsToQuery | ForEach-Object { $dropdownOU.Items.Add($_) }
- $form.Controls.Add($dropdownOU)
- # Create the "Query" button
- $btnQuery = New-Object Windows.Forms.Button
- $btnQuery.Text = "Query"
- $btnQuery.Location = New-Object Drawing.Point 20, 80
- $btnQuery.Add_Click({
- $selectedOU = $dropdownOU.SelectedItem
- QueryComputersAndDisplayResults -selectedOU $selectedOU
- # Replace these values with your API key and key secret
- $ApiKey = "Paste Key"
- $KeySecret = "Paste Secret"
- # Extract service tag values from array
- $serviceTagsFromCSV = $csvData.SN -split ','
- # Define the batch size
- $batchSize = 100
- # Create an array to hold all warranty information
- $allWarrantyInfo = @()
- # Process service tags in batches
- for ($i = 0; $i -lt $serviceTagsFromCSV.Length; $i += $batchSize) {
- $batchServiceTags = $serviceTagsFromCSV[$i..($i + $batchSize - 1)] | Where-Object { $_ -ne '' }
- # Call the function with the extracted service tag batch
- $warrantyInfoBatch = Get-DellWarrantyInfo -ServiceTags $batchServiceTags -ApiKey $ApiKey -KeySecret $KeySecret
- # Add the warranty information from the batch to the array
- $allWarrantyInfo += $warrantyInfoBatch
- }
- # Combine computer data with warranty information and save to CSV
- $combinedData = foreach ($computerData in $csvData) {
- $warrantyInfo = $allWarrantyInfo | Where-Object { $_.ServiceTag -eq $computerData.SN }
- if ($warrantyInfo) {
- $computerData | Select-Object *, @{
- Name = 'Model'
- Expression = { $warrantyInfo.Device }
- }, @{
- Name = 'WarrantyExpiration'
- Expression = { $warrantyInfo.EndDate }
- }
- } else {
- $computerData
- }
- }
- # Save Data to CSV
- $saveFileDialog = New-Object Windows.Forms.SaveFileDialog
- $saveFileDialog.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*"
- $saveFileDialog.Title = "Save the results to CSV file"
- $saveFileDialog.ShowDialog() | Out-Null
- if ($saveFileDialog.FileName) {
- $combinedData | Export-Csv -Path $saveFileDialog.FileName -NoTypeInformation
- [System.Windows.Forms.MessageBox]::Show("Results exported to $($saveFileDialog.FileName)", "Export Successful", "OK", "Information")
- }
- })
- $form.Controls.Add($btnQuery)
- # Show the form
- $form.ShowDialog()
Add Comment
Please, Sign In to add comment