Managing device prefixes and suffixes en masse

Device Prefixes and Suffixes

Prefixes and suffixes in device names are often used for custom grouping requirements. Sometimes there can be a need to batch update devices to add, alter or remove a prefix or suffix.

While it has been technically possible to do this via Change Tracker’s API, this method is complex and puts the responsibility of API session and credential management on the user.

API Client Library

The new open source Change Tracker API client library makes tasks like this a breeze to configure and even automate while handling tricky things like sessions and credentials.

After establishing a session to the Hub (see the brief instructions in the link above), the following script, using the Get-NctDevices and the Update-NctDevice functions, was used to trim a particular prefix and suffix from devices that had them:

$prefix = "Test ("
$suffix = ")"

$devices = Get-NctDevices -name "$prefix*$suffix" 

foreach ($device in $devices) {
    Write-Output "Trimming '$prefix' prefix and '$suffix' suffix from name for: $($device.Name)"

    $newName = $device.Name.TrimStart($prefix).TrimEnd($suffix)

    Update-NctDevice -device $device -newName $newName
}

The functions try to make things as easy as possible by returning PowerShell objects, making it easy to combine their output with native PowerShell commands as can be seen in the line: $device.Name.TrimStart($prefix).TrimEnd($suffix)

Output

Trimming ‘Test (’ prefix and ‘)’ suffix from name for: Test (prod-linux-01)
Trimming ‘Test (’ prefix and ‘)’ suffix from name for: Test (prod-linux-02)
Trimming ‘Test (’ prefix and ‘)’ suffix from name for: Test (prod-linux-03)
Trimming ‘Test (’ prefix and ‘)’ suffix from name for: Test (prod-linux-04)

Conclusion

This is just one example of what it possible with an API client library like this. The combination of functions that return native objects and a scripting language like PowerShell enable the user to implement almost any custom process.

We’d love to know what you have or want to automate with the API client library!

1 Like