Tuesday, July 9, 2019

Dashboard with Powershell

Building a Dashboard

Goal
Show Status of production assets grouped in kits.


Using our asset management system's data and PowerShell

Asset Management System
https://www.ezofficeinventory.com/developers
Universal Dashboard for PowerShell
https://docs.universaldashboard.io/

Setup
Update PowerShell
Find-Module -Name PowerShellGet | Install-Module
Install UniversalDashboard.Community Version 2.3.2
Install-Module UniversalDashboard.Community -MaximumVersion 2.3.2

Folder Structure
C:\Support\scripts\dashboard
          - data   # XML data stored here
          - pages # Dashboard pages
          - modules # modules that pull and format data


TLS Error - Older versions of Windows may need TLS1.2 enabled.
[System.Net.ServicePointManager]::SecurityProtocol = `
[System.Net.SecurityProtocolType]::Tls12;


Get Assets  - get-ezoffice-assets.ps1

$token="mykeyguid"
$baseuri="https://mydomain.ezofficeinventory.com"
$method="/assets.api"
$senduri=$baseuri+$method

$pages=0

$assets=Invoke-RestMethod -H @{"token" = $token} -Method GET -URI $senduri
$pages=$assets.total_pages
$allassets=@()
$curpage=1
While($curpage -le $pages){
# $curpage
$senduri=$baseuri+$method+"?page="+$curpage
$assets=Invoke-RestMethod -H @{"token" = $token} -Method GET -URI $senduri
$curpage++
$allassets = $allassets + $assets.assets
}

#$allassets.count
$allassets |Export-Clixml -Path C:\support\scripts\dashboard\data\assets.xml

Get Group Data - get-ezoffice-groups.ps1

$token="mykeyguid"
$baseuri="https://mydomain.ezofficeinventory.com"
$method="/assets/classification_view.api"
$senduri=$baseuri+$method

$pages=0
$groups =@{}
$groups=Invoke-RestMethod -H @{"token" = $token} -Method GET -URI $senduri


$pages=$groups.total_pages
#$pages
$allgroups=@()
$curpage=1
While($curpage -le $pages){

$senduri=$baseuri+$method+"?page="+$curpage
$groups=Invoke-RestMethod -H @{"token" = $token} -Method GET -URI $senduri
$curpage++
$allgroups = $allgroups + $groups.groups.group

}

$allgroups |Export-Clixml -Path C:\support\scripts\dashboard\data\groups.xml



Build display data with data from EZoffice Inventory - get-ezoffice-bundles.ps1

$token="mykeyguid"
$baseuri="https://mydomain.ezofficeinventory.com"
$method="/bundles.api"
$senduri=$baseuri+$method
###############################
### Get All bundles from EzOfficeInventory
###############################
$pages=0
$bundles =@{}
$bundles=Invoke-RestMethod -H @{"token" = $token} -Method GET -URI $senduri
$pages=$bundles.total_pages
$allbundles=@()
$curpage=1
While($curpage -le $pages){
$senduri=$baseuri+$method+"?page="+$curpage
$bundles=Invoke-RestMethod -H @{"token" = $token} -Method GET -URI $senduri
$curpage++
$allbundles = $allbundles + $bundles.bundles
}
####### Save Bundles to XML file
$allbundles|Export-Clixml -Path C:\support\scripts\dashboard\data\bundles.xml


###############################
### Load Group and Asset data
###############################
$assetDataPath = "C:\support\scripts\dashboard\data\assets.xml"
$groupsDataPath = "C:\support\scripts\dashboard\data\groups.xml"
$locationsDataPath = "C:\support\scripts\dashboard\data\locations.xml"
#
$assets = @{}
$groups = @{}
$locations = @{}
$ghash=@{} #groups hash table
#
$assets = import-Clixml -Path $assetDataPath
$groups = import-Clixml -Path $groupsDataPath
$groups|Where-Object{$ghash.add($_.id,$_.name)} # Convert goups to hash table
$locations = import-Clixml -Path $locationsDataPath

###############################


###############################
# Build Bundle Items XML
###############################

$bundleData = @{}
[System.Collections.ArrayList]$bundleItems = @{}
$allbundles|ForEach-Object{
$method="/bundles/" + $_.id + ".api"
$senduri=$baseuri+$method
$bundleData=Invoke-RestMethod -H @{"token" = $token} -Method GET -URI $senduri
#$KitAssets = @{}
$bundleData.bundle.line_item_labels.GetEnumerator()|ForEach-Object {
$TassetID=$_.Replace("Asset #","").split(' ')[0]
$ta=@{}
$ta = $assets | Where-Object {$_.sequence_num -eq $TassetID}

$KitAsset = [pscustomobject]@{
Name = $ta| Select-Object -ExpandProperty name
Identifier = $ta| Select-Object -ExpandProperty identifier
State = $ta| Select-Object -ExpandProperty state
User = $ta| Select-Object -ExpandProperty assigned_to_user_name
AssetID = $TassetID
Description = $ta| Select-Object -ExpandProperty description
}

$Items = [pscustomobject]@{
bundleID = $bundleData.bundle.id
assetID = $_.Replace("Asset #","").split(' ')[0]
Asset = $KitAsset#$a1 +"/"+ $a3 +"/"+ $a2
}
$bundleItems.add($Items)|Out-Null
}

}
$bundleItems|Export-Clixml -Path C:\support\scripts\dashboard\data\bundleItems.xml


###############################
# Build Kits Display Data XML
###############################
$KitsDisplay = Foreach ($row in $allbundles)
{
[pscustomobject]@{
KitName = $row.name
State = $locations | Where-Object {$_.id -eq $row.location_id} |Select-Object -ExpandProperty state
Assets = $bundleItems| Where-Object {$_.bundleID -eq $row.id} | Select-Object -ExpandProperty Asset
}
}
$KitsDisplay |Export-Clixml -Path C:\support\scripts\dashboard\data\KitsDisplay.xml


Kits Dashboard page - kits.ps1

#
#
# Kits Page
#
New-UDPage -AutoRefresh -RefreshInterval 60 -Name 'Kits' -Icon cloud -Content {

###############################
### Load Group and Asset data
###############################
$KitsData = "C:\support\scripts\dashboard\data\KitsDisplay.xml"
$Global:kitdata =$null
$Global:kitname =$null
#
$kits= @{}

$kits = import-Clixml -Path $KitsData


###############################

$myBGColor=@{
available = "white" 
maintenance = "orange"
}

$TblStyle = "
<style>
TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#778899}
TD{border-width: 1px;padding: 3px;border-style: solid;border-color: black;}

</style>
"


New-UDRow {

    New-UDColumn {
New-UDCard -Content {"Nevada"} -BackgroundColor blue -FontColor white

$kits |Where-Object {$_.State -eq 'NV'} |ForEach-Object {
$Tbl=$TblStyle
#tr:nth-child(odd) { background-color:#d3d3d3;}
#tr:nth-child(even) { background-color:white;}
$Tbl=$Tbl+"<table> <tr><th>"+ $_.KitName + "</th></tr>"
$_.Assets|ForEach-Object {
$Thref = "https://mydomain.ezofficeinventory.com/assets/"+$_.AssetID
$Ttitle = $_.Description # Displayed in with mouseover hover
$Tbl=$Tbl+ '<tr bgcolor="'+$myBGColor[$_.State]+'"><td> <a href="'+$Thref+'" title="'+$Ttitle+'" target="_blank">' + $_.Name + '</td></tr>'
}
$Tbl=$Tbl+ "</table>"

New-UDHtml -Markup $Tbl
}

} ### - End UDColumn
New-UDColumn {
         New-UDCard -Content {"Arizona"} -BackgroundColor blue -FontColor white
    
$kits |Where-Object {$_.State -eq 'AZ'} |ForEach-Object {
$Tbl=$TblStyle
#tr:nth-child(odd) { background-color:#d3d3d3;}
#tr:nth-child(even) { background-color:white;}
$Tbl=$Tbl+"<table> <tr><th>"+ $_.KitName + "</th></tr>"
$_.Assets|ForEach-Object {
$Thref = "https://mydomain.ezofficeinventory.com/assets/"+$_.AssetID
$Ttitle = $_.Description
$Tbl=$Tbl+ '<tr bgcolor="'+$myBGColor[$_.State]+'"><td> <a href="'+$Thref+'" title="'+$Ttitle+'" target="_blank">' + $_.Name + '</td></tr>' }
$Tbl=$Tbl+ "</table>"

New-UDHtml -Markup $Tbl
}


}### - End UDColumn
    New-UDColumn {
         New-UDCard -Content {"Washington"} -BackgroundColor blue -FontColor white
    
$kits |Where-Object {$_.State -eq 'WA'} |ForEach-Object {
$Tbl=$TblStyle
#tr:nth-child(odd) { background-color:#d3d3d3;}
#tr:nth-child(even) { background-color:white;}
$Tbl=$Tbl+"<table> <tr><th>"+ $_.KitName + "</th></tr>"
$_.Assets|ForEach-Object {
$Thref = "https://mydomain.ezofficeinventory.com/assets/"+$_.AssetID
$Ttitle = $_.Description
$Tbl=$Tbl+ '<tr bgcolor="'+$myBGColor[$_.State]+'"><td> <a href="'+$Thref+'" title="'+$Ttitle+'" target="_blank">' + $_.Name + '</td></tr>' }
$Tbl=$Tbl+ "</table>"

New-UDHtml -Markup $Tbl
}


} ### - End UDColumn
New-UDColumn {
         New-UDCard -Content {"Utah"} -BackgroundColor blue -FontColor white
    
$kits |Where-Object {$_.State -eq 'UT'} |ForEach-Object {
$Tbl=$TblStyle
#tr:nth-child(odd) { background-color:#d3d3d3;}
#tr:nth-child(even) { background-color:white;}
$Tbl=$Tbl+"<table> <tr><th>"+ $_.KitName + "</th></tr>"
$_.Assets|ForEach-Object {
$Thref = "https://mydomain.ezofficeinventory.com/assets/"+$_.AssetID
$Ttitle = $_.Description
$Tbl=$Tbl+ '<tr bgcolor="'+$myBGColor[$_.State]+'"><td> <a href="'+$Thref+'" title="'+$Ttitle+'" target="_blank">' + $_.Name + '</td></tr>' }
$Tbl=$Tbl+ "</table>"

New-UDHtml -Markup $Tbl
}


} ### - End UDColumn
########## - End UDRow
}

}






No comments:

Post a Comment