Maxime Palmieri (Customer) asked a question.

Contest Entry: Importing numerous machines at once from a File

Problem : Having to import numerous machines at once from a CSV File

 

We once were given by a user a CSV file (with these headers Name;fqdn;ip) containing a list of more than 50 machines to monitor and to add to WhatsUp. A collegue of mine add the task to create all the entries based on that CSV file. Since we didn’t have credentials or snmp (we only had to add Ping Monitor), we could’t use the WhatsUp ability to scan the network and have the machine names. I didn’t wanted him to create everything manually…

 

To solve his issue, we proceeded like this :

 

Since the code does not contain any customers or sensitive informations, I can share it here. Everything was written in PowerShell since this is my favorite scripting language 😋

It's a simple use case but it was usefull to us.

  1. #Documentation API http://localhost:9644/swagger/ui/
  2. $url = "http://localhost:9644/api/v1"
  3. $c = get-credential
  4. $filepath = "C:\file.csv"
  5. $tocopy = 9295 #id of the machine we wan't to copy
  6.  
  7. #region AUTHENTIFICATION_TOKEN
  8. $req_auth = @{
  9. Method = "POST"
  10. Uri = "$url/token"
  11. Body = "grant_type=password&username=$($c.Username)&password=$($c.GetNetworkCredential().Password)"
  12. }
  13. $token = Invoke-RestMethod @req_auth
  14. $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
  15. $headers.Add("Authorization", "Bearer $($token.access_token)")
  16. #endregion
  17.  
  18. #region RERIEVE_TEMPLATE
  19. $req_device = @{
  20. Method = "GET"
  21. Uri = "$url/devices/$tocopy/config/template "
  22. Headers = $headers
  23. }
  24. $device = Invoke-RestMethod @req_device
  25. $template = $device.data.templates
  26. #endregion
  27.  
  28. #region RETRIEVE_CSV
  29. $machines = Get-Content $filepath | ConvertFrom-Csv -Delimiter ";"
  30. #endregion
  31.  
  32. #region CREATION
  33. # We exclude the name of the machine that we used has a template
  34. ForEach ($machine in $machines.Where( { $_.'Name' -ne $template.displayName })) {
  35. #the convertto-json is used to convert the object, if not we have readonly properties
  36. $new_device = $template | ConvertTo-Json -Depth 4 | ConvertFrom-Json
  37. $new_device.PSObject.Properties.Remove("Templateid")
  38. $new_device.displayName = $machine.'Name'
  39. $new_device.interfaces[0].networkName = $machine.'fqdn'
  40. $new_device.interfaces[0].networkAddress = $machine.'IP'
  41.  
  42. $body = @"
  43. {
  44. "options":["all"],
  45. "templates":[
  46. $($new_device | ConvertTo-Json -Depth 4)]
  47. }
  48. "@
  49.  
  50. $req_new = @{
  51. Method = 'Patch'
  52. Uri = "$url/devices/-/config/template"
  53. Headers = $headers
  54. ContentType = 'application/json'
  55. Body = $body
  56. }
  57. Invoke-RestMethod @req_new
  58. }
  59. #endregion

 


  • Nichol Goldstein (Progress)

    Thank you for entering! You should have received a participation badge for your effort, and it will show on your profile page.

     

    Good luck, and have a wonderful day!

    Selected as Best
  • Nichol Goldstein (Progress)

    Thank you for entering! You should have received a participation badge for your effort, and it will show on your profile page.

     

    Good luck, and have a wonderful day!

    Selected as Best
  • Mick Smith (Customer)

    @Maxime Palmieri, are you able to provide any assistence on this code, i've tried and i cannot get it working, returning the following error

     

    Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named 'Where'.

    At C:\Users\user\Documents\WUG import.ps1:35 char:26

    + ... $machine in $machines.Where( { $_.'Name' -ne $template.displayName }) ...

    +                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

       + CategoryInfo         : InvalidOperation: (Where:String) [], RuntimeException

       + FullyQualifiedErrorId : MethodNotFound