Skip to content

Commit

Permalink
Age of device function to NInja
Browse files Browse the repository at this point in the history
  • Loading branch information
jayrodksmith authored Mar 18, 2024
1 parent 46d9079 commit c377ad2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Private/Helpers/Get-AgeOfDevice.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function Get-AgeOfDevice {
<#
.SYNOPSIS
Function to Get estimated age of the device
.DESCRIPTION
This function will Get estimated age of the device
.EXAMPLE
Get-AgeOfDevice -StartDate ""
.PARAMETER StartDate
StartDate, must be unix format
#>
param(
[Parameter(Mandatory = $false)]
$StartDate
)

# Assuming $startDate contains the UNIX timestamp as previously mentioned
if($startdate -eq $null){
$startdate = (Ninja-Property-Get $ninjawarrantystart)
}
$startDateUnixTimestamp = $StartDate
if ($startDateUnixTimestamp) {
# Convert the UNIX timestamp to a DateTime object
# PowerShell treats UNIX timestamp in seconds, and it needs to be converted to DateTime from the epoch (1970-01-01)
$startDate = [DateTimeOffset]::FromUnixTimeSeconds($startDateUnixTimestamp).DateTime

# Getting today's date
$endDate = Get-Date

# Calculating the difference between the two dates
$timeSpan = $endDate - $startDate

# Calculating the total number of years as a decimal
$yearsDecimal = $timeSpan.TotalDays / 365.25 # Including leap years in the calculation

# Rounding to two decimal places for a more precise years figure
$yearsRounded = [math]::Round($yearsDecimal, 2)

# Optionally, you might want to output the years figure to the console for verification
Write-Verbose "Device Age: $yearsRounded years"
return $yearsRounded
} else {
return $false
}
}
21 changes: 21 additions & 0 deletions Private/RMM/Write-WarrantyNinjaRMM.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ function Write-WarrantyNinjaRMM {
}
$WarrantyNinjaRMM = Get-WarrantyNinjaRMM
if($WarrantyNinjaRMM -eq $true -and ($ForceUpdate -eq $false)){
# Set Age of device if start date exists
if($ninjadeviceage){
$calculatedageofdevice = Get-AgeOfDevice
if ($calculatedageofdevice -ne $false){
$Currentdeviceage = Ninja-Property-Get $ninjadeviceage
if($Currentdeviceage -ne $calculatedageofdevice){
Ninja-Property-Set $ninjadeviceage $calculatedageofdevice
}
}
}
return "Warranty details already in NinjaRMM"
} else {
if($Warrantystart){
Expand All @@ -59,6 +69,17 @@ function Write-WarrantyNinjaRMM {
if($WarrantyExpiryutc){Ninja-Property-Set $ninjawarrantyexpiry $WarrantyExpiryutc}
if($WarrantyStatus){Ninja-Property-Set $ninjawarrantystatus $WarrantyStatus}
if($Invoicenumber){Ninja-Property-Set $ninjainvoicenumber $Invoicenumber}

# Set Age of device if start date exists
if($ninjadeviceage){
$calculatedageofdevice = Get-AgeOfDevice
if ($calculatedageofdevice -ne $false){
$Currentdeviceage = Ninja-Property-Get $ninjadeviceage
if($Currentdeviceage -ne $calculatedageofdevice){
Ninja-Property-Set $ninjadeviceage $calculatedageofdevice
}
}
}
return "Warranty details saved to NinjaRMM"
}
}
5 changes: 5 additions & 0 deletions Public/Get-Warranty.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ function Get-Warranty {
[Parameter(Mandatory = $false, ParameterSetName = 'CentralNinja')]
[String]$ninjainvoicenumber = 'invoicenumber',

[Parameter(Mandatory = $false, ParameterSetName = 'Default')]
[Parameter(Mandatory = $false, ParameterSetName = 'CentralNinja')]
[String]$ninjadeviceage = 'deviceage',

[Parameter(Mandatory = $false, ParameterSetName = 'CentralNinja')]
[String]$HpSystemSKU

Expand All @@ -87,6 +91,7 @@ function Get-Warranty {
Set-Variable ninjawarrantyexpiry -Value $ninjawarrantyexpiry -Scope Global -option ReadOnly -Force
Set-Variable ninjawarrantystatus -Value $ninjawarrantystatus -Scope Global -option ReadOnly -Force
Set-Variable ninjainvoicenumber -Value $ninjainvoicenumber -Scope Global -option ReadOnly -Force
Set-Variable ninjadeviceage -Value $ninjadeviceage -Scope Global -option ReadOnly -Force
}
if ($ForceUpdate -eq $true) {
Set-Variable ForceUpdate -Value $ForceUpdate -Scope Global -option ReadOnly -Force
Expand Down

0 comments on commit c377ad2

Please sign in to comment.