5 Replies Latest reply on May 6, 2016 2:47 AM by zyontist

    SDK/PowerShell - Group Creation Script Issue

    zackm


      Trying to use the following script (based on the 'Groups' example in the SDK) to add groups based on Nodes.Location and a CustomProperty !=

       

      if (! (Get-PSSnapin | where {$_.Name -eq "SwisSnapin"})) {
          Add-PSSnapin "SwisSnapin"
      }
      # Connect to SWIS
      $hostname = "localhost"
      $username = "username here"
      $password = "password here" | ConvertTo-SecureString -asPlainText -Force
      $cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $password 
      $swis = Connect-Swis -host $hostname -cred $cred
      #Set the locatoin of the csv
      Set-Location 'C:\Users\username here\Desktop'
      # read in csv file
      $importFile="./MakeGroups.csv"
      $importList=IMPORT-CSV $importFile
      #iterate through each entry in csv file
      FOREACH($import in $importList)
      {
          $SiteCode = $import.Location
          $members = @(
        @{ Name = "$SiteCode"; Definition = "filter:/Orion.Nodes[Location='$SiteCode' AND CustomProperties.Device_Type != 'Router']" }
      )
      $groupId = (Invoke-SwisVerb $swis "Orion.Container" "CreateContainer" @(
          # group name
          "$SiteCode",
          # owner, must be 'Core'
          "Core",
          # refresh frequency
          60,
          # Status rollup mode:
          # 0 = Mixed status shows warning
          # 1 = Show worst status
          # 2 = Show best status
          0,
          # group description
          "Group created by the PowerShell sample script.",
          # polling enabled/disabled = true/false (in lowercase)
          "true",
          # group members
          ([xml]@(
             "<ArrayOfMemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'>",
             [string]($members |% {
               "<MemberDefinitionInfo><Name>$($_.Name)</Name><Definition>$($_.Definition)</Definition></MemberDefinitionInfo>"
               }
             ),
             "</ArrayOfMemberDefinitionInfo>"
          )).DocumentElement
        )).InnerText
           
      }
      

       

       

      Keep getting this error:

       

      Invoke-SwisVerb : Entity Orion.Nodes does not contain requested property A

      At C:\Users\username here\Desktop\MakeGroups.ps1:32 char:14

      +  $groupId = (Invoke-SwisVerb $swis "Orion.Container" "CreateContainer" @(

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

          + CategoryInfo          : InvalidOperation: (:) [Invoke-SwisVerb], FaultException`1

          + FullyQualifiedErrorId : SwisError,SwisPowerShell.InvokeSwisVerb

       

       

      Looking in the DB, the script is creating the group under 'Containers', but there is not an entry being created under 'ContainerMemberDefinitions'

       

      All help is appreciated

        • Re: SDK/PowerShell - Group Creation Script Issue
          derhally

          Do any of the values in your CSV have quotes in them?

           

          This script worked for me

           

          if (! (Get-PSSnapin | where {$_.Name -eq "SwisSnapin"})) {  
              Add-PSSnapin "SwisSnapin"  
          }  
          # Connect to SWIS  
          $hostname = "localhost"  
          $swis = Connect-Swis -host $hostname  
          
          
          $SiteCode = "Dev";  
          $members = @( @{ Name = "$SiteCode"; Definition = "filter:/Orion.Nodes[Location='Washington' AND CustomProperties.Department='$SiteCode']" })
          
          
            $groupId = (Invoke-SwisVerb $swis "Orion.Container" "CreateContainer" @(  
              # group name  
              "$SiteCode",  
              # owner, must be 'Core'  
              "Core",  
              # refresh frequency  
              60,  
              # Status rollup mode:  
              # 0 = Mixed status shows warning  
              # 1 = Show worst status  
              # 2 = Show best status  
              0,  
              # group description  
              "Group created by the PowerShell sample script.",  
              # polling enabled/disabled = true/false (in lowercase)  
              "true",  
              # group members  
              ([xml]@(  
                 "<ArrayOfMemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'>",  
                 [string]($members |% {  
                   "<MemberDefinitionInfo><Name>$($_.Name)</Name><Definition>$($_.Definition)</Definition></MemberDefinitionInfo>"  
                   }  
                 ),  
                 "</ArrayOfMemberDefinitionInfo>"  
              )).DocumentElement  
            )).InnerText  
          
            • Re: Re: SDK/PowerShell - Group Creation Script Issue
              JesperVestergaard

              Hi

               

              I wanted to do something like this, but did not get the point about the csv file (as for me, all my relevant data is in solarwinds databases)

              Honestly, i´m no programmer (by far) but find it interesting how to save time, and have consistent data - Lazy network engineer mindset i guess

               

              I had some help, and ended up with this - And is working perfect for me .

              Next step is to create groups for my sites, where category is NOT WAN, and then figure out how to script dependancies between the two groups for the site - wan and lan

               

               

              And ... a feature request - Build this into the application instead

              Albeit i love this scripting (over manual labor) i´d appriciate the feature natively, and not customized, if i can

               

               

               

              $cities = Get-SwisData $swis "SELECT DISTINCT City FROM Orion.NodesCustomProperties WHERE City IS NOT NULL"
              
              foreach($city in $cities)
              {
              
              $members = @(
                @{ Name = "$city WAN nodes"; Definition = "filter:/Orion.Nodes[Contains(CustomProperties.Category,'WAN') AND CustomProperties.City='$city']" }
              )
              
              $groupId = (Invoke-SwisVerb $swis "Orion.Container" "CreateContainer" @(
                  # group name
                  "$city WAN",
                  # owner, must be 'Core'
                  "Core",
                  # refresh frequency
                  60,
                  # Status rollup mode:
                  # 0 = Mixed status shows warning
                  # 1 = Show worst status
                  # 2 = Show best status
                  0,
                  # group description
                  "$city WAN nodes, selected based on custom property value nodes city",
                  # polling enabled/disabled = true/false (in lowercase)
                  "true",
                  # group members
                  ([xml]@(
                     "<ArrayOfMemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'>",
                     [string]($members |% {
                       "<MemberDefinitionInfo><Name>$($_.Name)</Name><Definition>$($_.Definition)</Definition></MemberDefinitionInfo>"
                       }
                     ),
                     "</ArrayOfMemberDefinitionInfo>"
                  )).DocumentElement
                )).InnerText
              
              }
              
                • Re: SDK/PowerShell - Group Creation Script Issue
                  zyontist

                  Hi I know this is an old thread but I am not a Powershell expert and trying to do something like this where it is automatically creating groups dependant on site information of newly discovered sites.

                   

                  Our naming convention is like this:

                   

                  <Company Name>_<Site Name>_<Area code>_<Site code>_<Device Type>_<primary or backup>

                   

                  We are looking to make up groups and sub groups as follows:

                   

                  Main Group

                   

                  <Site Code> <Area Code> <Site Name>

                   

                  2 Sub groups for each site

                   

                  <Site Code> WAN devices

                  <Site Code> LAN devices

                   

                  So my question is this:

                   

                  Is it possible using a Powershell script to create groups and sub groups and using the codes in the naming convention to create those and work out the group members? We have criteria to differentiate WAn/LAN devices per site as at the moment we are doing it manually and there are 100's of sites so looking to see if it can be done automatically to speed it up?

                   

                  Kind regards,

                   

                  Z.