Skip to content

Complex Code Example

Chris Simon edited this page Aug 12, 2025 · 1 revision

The Example below, will connect to N-Central, fetch a list of all devices using a specified filter, fetch data from the Device Asset and assembles it into a new array ready to be exported to Excel using the ImportExcel Powershell Module

import-module -Name PS-NCentral-RESTAPI
Import-Module -Name ImportExcel
connect-Ncentral -JwtToken <MyJWTToken> -BaseUrl <MyBaseURL>
$Devices = Get-NCentralDevices -OrgUnitID 375 -FilterID 205070758 -All
$DeviceArray = @()
ForEach ($Device in $Devices) {
    Write-Progress -Activity "Fetching device $($Devices.IndexOf($Device)) of $($Devices.Count)" -PercentComplete ($Devices.IndexOf($Device)/$($Devices.Count)*100)
    if (($Device.longName -match "Primair") -or ($Device.longName -match "Secundair")) {
        Continue
    }
    $DeviceAsset = Get-NcentralDeviceAssets -DeviceID $Device.deviceId
    $DeviceData = New-Object PSObject
    $DeviceData | Add-Member -type NoteProperty -Name 'Site' -Value $Device.siteName
    $DeviceData | Add-Member -type NoteProperty -Name 'Device Class' -Value $Device.deviceClass
    $DeviceData | Add-Member -type NoteProperty -Name 'Device' -Value $Device.longName
    $DeviceData | Add-Member -type NoteProperty -Name 'Network Address' -Value $Device.uri
    $DeviceData | Add-Member -type NoteProperty -Name 'Make/Model' -Value $DeviceAsset.computersystem.model
    $DeviceData | Add-Member -type NoteProperty -Name 'Serial Number' -Value $DeviceAsset.computersystem.serialnumber
    $DeviceData | Add-Member -type NoteProperty -Name 'OS and Service Pack' -value $DeviceAsset.os.reportedos
    $DeviceArray += $DeviceData
}
Export-Excel -Path "C:\temp\HW_Inventory.xlsx" -InputObject $DeviceArray -AutoSize -FreezeTopRow -AutoFilter -NoNumberConversion * -TableStyle Medium10

Access Groups

Active Issues

API-Service

Authentication

Custom Properties

Device Filters

Device Tasks

Devices

Job Statuses

Maintenance Windows

Organisation Units

PSA

Registration Tokens

Scheduled Tasks

User Roles

Users

Clone this wiki locally