finance-asset-report/Asset-Data.ps1

87 lines
6.0 KiB
PowerShell
Raw Permalink Normal View History

2024-03-22 09:37:32 -06:00
<# Script will grab asset info from InvGate and Extracted Invoice Details #>
<#
AssetStatus values:
2 - Active
8 - Loaner
9 - Stock
3 - Deleted
#>
$API_Secret = 'wmFuMl5mEOsn7drmv606vyCbN6c1Z6BIZYjVa5EfdPWRqzVJ4KVXkn28gbXf5KqUxTHZgQsnRnPfnYT88zOCyDu4VlZlIGRcE8jGMOGP5kuAowpVKeMl2JYK1JczCFW1'
$API_ClientID = 'rk5ZnbKVRR0LtJw0BJovkHprkmAgghjSVPCJK4DC'
$AssetMgrURI = "https://mpeengine.is.cloud.invgate.net:443/public-api"
$Locations = '{"links":{"first":"https://mpeengine.is.cloud.invgate.net/public-api/locations/?access_token=zTlzGAHXwJL1BZsQPMlGHmRjb77ASN&page=1","last":"https://mpeengine.is.cloud.invgate.net/public-api/locations/?access_token=zTlzGAHXwJL1BZsQPMlGHmRjb77ASN&page=1","next":null,"prev":null},"data":[{"type":"Location","id":"24","attributes":{"name":"AWS","full_path":"Cloud > AWS","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":{"type":"Location","id":"23"}},"configuration_item":{"data":null}}},{"type":"Location","id":"25","attributes":{"name":"Azure","full_path":"Cloud > Azure","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":{"type":"Location","id":"23"}},"configuration_item":{"data":null}}},{"type":"Location","id":"28","attributes":{"name":"Calgary","full_path":"Calgary","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":null},"configuration_item":{"data":null}}},{"type":"Location","id":"23","attributes":{"name":"Cloud","full_path":"Cloud","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":null},"configuration_item":{"data":null}}},{"type":"Location","id":"32","attributes":{"name":"Edmonton","full_path":"Edmonton","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":null},"configuration_item":{"data":null}}},{"type":"Location","id":"36","attributes":{"name":"Grande Prairie","full_path":"Grande Prairie","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":null},"configuration_item":{"data":null}}},{"type":"Location","id":"27","attributes":{"name":"Lethbridge","full_path":"Lethbridge","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":null},"configuration_item":{"data":null}}},{"type":"Location","id":"47","attributes":{"name":"Lethbridge North","full_path":"Lethbridge North","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":null},"configuration_item":{"data":null}}},{"type":"Location","id":"53","attributes":{"name":"Medicine Hat","full_path":"Medicine Hat","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":null},"configuration_item":{"data":null}}},{"type":"Location","id":"51","attributes":{"name":"Red Deer","full_path":"Red Deer","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":null},"configuration_item":{"data":null}}},{"type":"Location","id":"48","attributes":{"name":"Regina","full_path":"Regina","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":null},"configuration_item":{"data":null}}},{"type":"Location","id":"50","attributes":{"name":"Saskatoon","full_path":"Saskatoon","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":null},"configuration_item":{"data":null}}},{"type":"Location","id":"49","attributes":{"name":"Saskatoon","full_path":"Saskatoon","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":null},"configuration_item":{"data":null}}},{"type":"Location","id":"54","attributes":{"name":"Vancouver","full_path":"Vancouver","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":null},"configuration_item":{"data":null}}},{"type":"Location","id":"52","attributes":{"name":"Winnipeg","full_path":"Winnipeg","description":null,"content_type":{"id":109}},"relationships":{"parent":{"data":null},"configuration_item":{"data":null}}}],"meta":{"pagination":{"page":1,"pages":1,"count":15}}}' | ConvertFrom-Json
$locations_table = @()
$assets = @()
foreach ($location in $Locations.data) {
$temp = [PSCustomObject]@{
ID = $location.id
Name = $location.attributes.name
}
$locations_table += $temp
}
function Auth {
param (
$API_Secret_Auth, $API_ClientID_Auth
)
$auth_Header = @{
'Content-Type' = "application/x-www-form-urlencoded"
}
$auth_Body = "grant_type=client_credentials&client_id=$API_ClientID_Auth&client_secret=$API_Secret_Auth"
$AuthURI = 'https://mpeengine.is.cloud.invgate.net:443/oauth2/token/'
$resp = Invoke-WebRequest $AuthURI -Method POST -Headers $auth_Header -Body $auth_Body
return ($resp.Content | ConvertFrom-Json)
}
# Get bearer token if it doesn't exist or is expired.
if (!($b_token) -or $b_token.expires_in -lt (get-Date)) {
$b_token = Auth -API_Secret_Auth $API_Secret -API_ClientID_Auth $API_ClientID
$b_token.expires_in = (Get-Date).AddSeconds($b_token.expires_in) # Change to date format.
}
$req_Headers = @{
Authorization = "Bearer " + $b_token.access_token
'Content-Type' = 'application/vnd.api+json'
}
# Find all active, Loaner, or Stock assets.
$queryString = '?status_ids=2,8,9'
$RequestURI = $AssetMgrURI + "/v2/assets-lite/" + $queryString
$Response = Invoke-WebRequest -Uri $RequestURI -Headers $req_Headers
do {
$assets += ($Response.Content | ConvertFrom-Json).results
$RequestURI = ($Response.Content | ConvertFrom-Json).next
$Response = Invoke-WebRequest -Uri $RequestURI -Headers $req_Headers
} while ($RequestURI)
# Get Locations
# $queryString = '?access_token=' + $b_token.access_token #V1 API's need token in the URL instead of header...
# $RequestURI = $AssetMgrURI + "/locations/" + $queryString
# $Response = Invoke-WebRequest -Uri $RequestURI -Headers $req_Headers
foreach ($asset in $assets) {
$matched = $locations_table | where ID -eq $asset.location
$asset.location = $matched.Name
}
$ExportData = $assets | select id, name, serial, location, Manufacturer, model, "InvoiceNumber"
$ExportData | Export-Csv .\AssetList.csv
# Invoke-WebRequest -Uri $RequestURI -method GET -ContentType application/json