3 Replies Latest reply on Oct 19, 2015 4:57 AM by madhavan

    groups not nesting

    datwiddler

      I have a Powershell script that runs nightly, and has been doing so successfully for a while until recently.

       

      When the script runs it creates groups, and then also does nesting of subgroups and nodes.

       

      When it run now, it won't nest the groups properly under the one 'master group' I have created.

       

      Looking at the database tables and comparing it to a second instance of NPM that was a clean install, I see there are indexes on the new instance database that aren't in the one I'm working.

       

      I've opened a support ticket to cover my bases, but want to ask here if any one else has had experience with this.

        • Re: groups not nesting
          tdanner

          Indexes should not affect behavior, only performance. Can you share the code of your script?

            • Re: groups not nesting
              datwiddler

              Don't see an where to upload an attach a file so I'll just paste here...

               

              Most of it is based on examples from the SDK, Everything else are pieces I've done.

               

              I admit it isn't the best and there is alot more error correcting and verification to be done. Still learning powershell.

               

              Any pointers would be greatly appriceated.

               

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              # Helper functions

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              # IP2GUID

              function ip2guid($ipString) {

                $ip = [System.Net.IPAddress]::Parse($ipString)

                $src = $ip.GetAddressBytes();

                $data = new-object byte[] 16

                $src.CopyTo($data, $data.Length - $src.Length)

               

                $dest = new-object byte[] 16

                [Array]::Copy($data, 12, $dest, 0, 4)

                [Array]::Copy($data, 10, $dest, 4, 2)

                [Array]::Copy($data, 8, $dest, 6, 2)

                [Array]::Copy($data, 6, $dest, 8, 2)

                [Array]::Copy($data, 0, $dest, 10, 6)

               

                return (New-Object Guid (,$dest)).ToString()

              }

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              #DNS lookup

              function dnslookup($dnsIPstring) {

               

                   $result = $null

                   $currentEAP = $ErrorActionPreference

                   $ErrorActionPreference = "silentlycontinue"

               

                   #Use the DNS Static .Net class for the reverse lookup

                   # details on this method found here: http://msdn.microsoft.com/en-us/library/ms143997.aspx

                   $result = [System.Net.Dns]::gethostentry($dnsIPstring)

                   $ErrorActionPreference = $currentEAP

                  

                   If ($result) {

                      $DNSlookup += [string]$result.HostName

                   }

                   Else {

                      $DNSlookup += "$IP - No HostNameFound"

                   }

                

                  $DNSlookup

              }

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              # Connect to SWIS

              $username = "username"

              $password = "password"

              $swis = Connect-Swis -UserName $username -Password $password -Hostname localhost -Verbose

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              # Move yesterdays import file to the import-archive folder

               

              $check4import = Test-Path C:\SWautomation\SW_Import.txt

               

              If ($check4import -eq $true) {

               

                 try {

                

                 Set-Location C:\SWautomation

               

                 $yesterday = (get-date).AddDays(-1).ToString("yyy-MM-dd_HH-MM-ss")

                 $yesterday

                 $oldimportfilename = "SW_Import.txt"

                 $importfilename = "SW_Import-"

                 $importfileext = ".txt"

                

                 $newimportfilename = $importfilename + $yesterday + $importfileext

               

                 Rename-Item $oldimportfilename $newimportfilename

                

                 Move-Item $newimportfilename -Destination "C:\SWautomation\import-archive\$newimportfilename" -ErrorAction stop

               

                 } catch {

               

                 $body =  "Caught an exception:`r`n`r`n"

                 $body += "Exception Type: $($_.Exception.GetType().FullName)`r`n`r`n"

                 $body += "Exception Message: $($_.Exception.Message)`r`n`r`n"

               

                 Send-MailMessage -From customerNMS@ -to sys.eng@ -Body $body -BodyAsHtml -Subject "Could Not move Old import file to Archive location" -SmtpServer mysmtp.server.com

                 

                 break

                 

                 }

                 }

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              # Move old log file

               

              $check4report = Test-Path C:\SWautomation\manage-import-report.txt

               

              If ($check4report -eq $true) {

               

              $reportname = "manage-import-report"

              $reportext = ".txt"

               

              $report = $reportname + $reportext

               

              $oldreport = $reportname + $yesterday + $reportext

               

              Rename-Item $report $oldreport

               

              Move-Item $oldreport -Destination "C:\SWautomation\import-archive\$oldreport"

               

              }

               

               

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              # Download CSV from CRM

               

              #Set Username and password

              $User = "username"

              $PWord = ConvertTo-SecureString –String "my_password" –AsPlainText -Force

              $Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $PWord

               

              try {

               

                  Get-SCPFile -LocalFile c:\SWautomation\SW_Import.txt -RemoteFile "SW_Import.txt" -ComputerName our_crm_server -Credential (Get-Credential $Credential) -ErrorAction Stop

               

              } catch {

               

               

              $body =  "Caught an exception:`r`n`r`n"

              $body += "Exception Type: $($_.Exception.GetType().FullName)`r`n`r`n"

              $body += "Exception Message: $($_.Exception.Message)`r`n`r`n"

               

              Send-MailMessage -From customerNMS@ -to sys.eng@ -Body $body -BodyAsHtml -Subject "SW_Import.txt DOWNLOAD FAILED!!!" -SmtpServer mysmtp.server.com

              break

              }

               

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              # CSV Import

               

              # Trim leading and trailing spaces, export the file wit the same delimiter.

               

              $csvcleanup = Import-Csv 'SW_Import.txt' -Delimiter ";" -Encoding ASCII

              $csvcleanup | Foreach-Object {  

                      $_.PSObject.Properties | Foreach-Object { $_.Value = $_.Value.Trim() }

                      $_.PSObject.Properties | Foreach-Object { $_.Value = $_.Value.TrimEnd(",") }

                       

                  }

                  $csvcleanup | Export-Csv 'SW_Import.txt' -Delimiter ";" -NoTypeInformation -Encoding ASCII

                 

               

              #Import our nice new, clean CSV file

              $CSVcompletedIMPORT = Import-csv 'SW_Import.txt' -Delimiter ";" -Encoding ASCII | where-object {$_.Status -like "Completed*" } | Sort-Object {[int] $_.ConnectionID}

              # $CSVcompletedIMPORT.count

              # $CSVcompletedIMPORT = Import-csv 'SW_Import.txt' -Delimiter ";" -Encoding ASCII | where-object {$_.Reseller_1 -eq "ACI Staff" -and $_.Status -le "Completed%" } | Sort-Object {[int] $_.ConnectionID}

              $CSVcancelledIMPORT = Import-csv 'SW_Import.txt' -Delimiter ";" -Encoding ASCII | where-object {$_.Status -eq "Cancelled" } | Sort-Object {[int] $_.ConnectionID}

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              #Set Some intial vars for counters, used in the final email report once the script completes

               

              $scriptstart = Get-Date

               

              $newmonitorcount =0

              $ipaddresscount =0

              $nochangecount =0

              $errorcount =0

               

              $newgroupcount =0

               

              $newsubgroupcountmaster =0

              $newsubgroupcountlevel1 =0

              $newsubgroupcountlevel2 =0

              $newsubgroupcountlevel3 =0

              $newsubgroupcountlevel4 =0

              $newsubgroupcountlevel5 =0

              $nodesaddedtogroupcount =0

              $groupalreadyinsolarwinds =0

               

              $nodesdeletedcount =0

              $groupsdeletedcount =0

               

              $unmananagednodecount = 0

              $unmananagednodecount2 = 0

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              # Node - Add, Update IP, Delete(still needs to be done)

               

               

              # Loop through each import line...

              foreach ($createcustomer in $CSVcompletedIMPORT){

               

                $newcsid = $createcustomer.ConnectionID

                $newcsip = $createcustomer.MonitorIP

               

                $SWconnectionID = Get-SwisData $swis "Select Nodes.CustomProperties.ConnectionID from Orion.Nodes where Nodes.CustomProperties.ConnectionID = @newcsid" @{newcsid=$newcsid}

                $SWipAddress = Get-SwisData $swis "Select IPAddress from Orion.Nodes where Nodes.CustomProperties.ConnectionID = @newcsid" @{newcsid=$newcsid}

               

                If($createcustomer.Monitor_Check -ne 'NO') {

               

                # Check if the ConnectionID is in Solarwinds, if not, add the Node to Solarwinds to be monitored

                If($SWconnectionID -ne $createcustomer.ConnectionID) {

               

                      $ipGuid = ip2guid($createcustomer.MonitorIP)

               

                      $CustomerDNSlookup = dnslookup($createcustomer.MonitorIP)

                     

                      $newNodeProps = @{

                      EntityType="Orion.Nodes";

                      IPAddress=$createcustomer.MonitorIP;

                      IPAddressGUID=$ipGuid;

                      DynamicIP=$False;

                      DNS=$createcustomer.MonitorIP;

                      Caption=$createcustomer.Location_Name;

                      EngineID=3;

                      Status=1;

                      UnManaged=$False;

                      Allow64BitCounters=$True;

                      SysObjectID="";

                      MachineType="Unknown";

                      External=$False;

                      SysName="";

                      NodeDescription="";

                      Location=$createcustomer.Address;

                      Contact="";

                      IOSImage="";

                      IOSVersion="";

                      Vendor="Unknown";

                      VendorIcon="Unknown.gif";

                      CPULoad="-2";

                      MemoryUsed="-2";

                      PercentMemoryUsed="-2";

                      TotalMemory="0";

               

                      # We only want ICMP

                      ObjectSubType="ICMP";

               

                      # polling timing (optional) - pull from standards table

                      RediscoveryInterval=0; # minutes (5..525600=1 year)

                      PollInterval=300; # seconds (1..1200)

                      StatCollection=5; # minutes (1..600)

                      Community="";

                      BufferNoMemThisHour="0";

                      BufferNoMemToday="0";

                      BufferSmMissThisHour="0";

                      BufferSmMissToday="0";

                      BufferMdMissThisHour="0";

                      BufferMdMissToday="0";

                      BufferBgMissThisHour="0";

                      BufferBgMissToday="0";

                      BufferLgMissThisHour="0";

                      BufferLgMissToday="0";

                      BufferHgMissThisHour="0";

                      BufferHgMissToday="0";

               

                      }

               

                      # Custom Fields we've added to track each Connection

                      $customPROPS = @{

                      DSLPhoneNumber=$createcustomer.DSLPhoneNumber;

                      Circuit_Type=$createcustomer.Circuit_Type;

                      Address=$createcustomer.Address;

                      City=$createcustomer.City;

                      PostalCode=$createcustomer.PostalCode;

                      Province=$createcustomer.Province;

                      Carrier=$createcustomer.Vendor;

                      ActivationDate=$createcustomer.ActivationDate;

                      ConnectionID=$createcustomer.ConnectionID;

                

                      }

               

                      $newNodeUri = New-SwisObject $swis -EntityType "Orion.Nodes" -Properties $newNodeProps

                      $nodeProps = Get-SwisObject $swis -Uri $newNodeUri

                      [System.String]$sqlCommand=("Insert into NodesCustomProperties (NodeID) VALUES (" + $nodeProps.nodeid + ")") 

                      Invoke-SwisVerb $swis "Orion.Reporting" "ExecuteSQL" $sqlCommand |Out-Null 

                      $customPROPSuri = "$newNodeUri/CustomProperties";

                      $addcustomPROPS = Set-SwisObject $swis  -Uri $customPROPSuri -Properties $customPROPS

               

                      $poller = @{

                      NetObject="N:"+$nodeProps["NodeID"];

                      NetObjectType="N";

                      NetObjectID=$nodeProps["NodeID"];

                      }

               

                      # Attach Native ICMP Poller to the Node

                      $poller["PollerType"]="N.Status.ICMP.Native";

                      $pollerUri = New-SwisObject $swis -EntityType "Orion.Pollers" -Properties $poller

               

                      # Attach Native ICMP Responstime Poller to the Node

                      $poller["PollerType"]="N.ResponseTime.ICMP.Native";

                      $pollerUri = New-SwisObject $swis -EntityType "Orion.Pollers" -Properties $poller

               

                      "`r`nAdded ConnectionID: $newcsid with IP: $newcsip" | Add-Content  update-log.txt -PassThru | Write-Host

                      #$customPROPSuri

                      #$createcustomer.DSLPhoneNumber;

                  

                      $newmonitorcount ++

                  

                   #ConnectionID is in Solarwinds, but the IP address was updated in Manage

                   }ElseIf(($SWconnectionID -eq $createcustomer.ConnectionID) -and ($SWipAddress -ne $createcustomer.MonitorIP)) {

               

                   # Look up the Node ID using the Manage ID from the CSV File

                   $SWnodeID = Get-SwisData $swis "Select NodeID from Orion.Nodes where Nodes.CustomProperties.ConnectionID = @newcsid" @{newcsid=$newcsid}

               

                   #Pass the the NodeID the the SWIS URI

                   $ipURI = "swis://localhost/Orion/Orion.Nodes/NodeID=$SWnodeID"

               

                   # Create an Array with the New IP addres from the CSV

                   $updateIPproperties = @{

                   IPAddress=$newcsip;

                   }

                  

                   # Using the established connection to Solarwinds set the for the Node collected above.

                   Set-SwisObject $swis -URI $ipURI -Properties $updateIPproperties -Verbose

                  

                   "`r`nIPAddress has change, updating ConnectionID: $newcsid IP from $SWipAddress to $newcsip" | Add-Content  update-log.txt -PassThru | Write-Host

                  

                   $ipaddresscount ++

                  

                   # ConnectionID is in Solarwinds and IP has not changed. Do nothing other then report that the connection already is being monitored.

                   }Elseif(($SWconnectionID -eq $createcustomer.ConnectionID) -and ($SWipAddress -eq $createcustomer.MonitorIP)) {   

                  

                   # "ConnectionID: $newcsid with IP: $newcsip Already being montiored" | Add-Content  update-log.txt -PassThru | Write-Host

                  

                   $nochangecount ++

                  

                  # Something REALLY went wrong... time to check the CSV file.     

                  }Else{

               

                  "ERROR! -  $createcustomer.ConnectionID - $createcustomer.MonitorIP "   | Add-Content  update-log.txt -PassThru | Write-Host

                 

                  $errorcount ++

                 }

                 

                  }

                }

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              # Create Groups

               

               

              foreach ($nmsgroup in $CSVcompletedIMPORT) {

               

                $grprslr1 = $nmsgroup.Reseller_1

                $grprslr1id = $nmsgroup.Reseller_1ID

                $grprslr2 = $nmsgroup.Reseller_2

                $grprslr2id = $nmsgroup.Reseller_2ID

                $grprslr3 = $nmsgroup.Reseller_3

                $grprslr3id = $nmsgroup.Reseller_3ID

                $grprslr4 = $nmsgroup.Reseller_4

                $grprslr4id = $nmsgroup.Reseller_4ID

                $grprslr5 = $nmsgroup.Reseller_5

                $grprslr5id = $nmsgroup.Reseller_5ID

               

                $tgrprslr1 = $grprslr1.Trim()

                $tgrprslr1id = $grprslr1id.Trim()

                $tgrprslr2 = $grprslr2.Trim()

                $tgrprslr2id = $grprslr2id.Trim()

                $tgrprslr3 = $grprslr3.Trim()

                $tgrprslr3id = $grprslr3id.Trim()

                $tgrprslr4 = $grprslr4.Trim()

                $tgrprslr4id = $grprslr4id.Trim()

                $tgrprslr5 = $grprslr5.Trim()

                $tgrprslr5id = $grprslr5id.Trim()

               

                # Multi array (Reseller 'x' Name, Reseller 'x' ID)

                $grpa = @(($tgrprslr1,$tgrprslr1id),($tgrprslr2,$tgrprslr2id),($tgrprslr3,$tgrprslr3id),($tgrprslr4,$tgrprslr4id),($tgrprslr5,$tgrprslr5id))

               

                # If Monitor Check field is No, don't even process it

                if($nmsgroup.Monitor_Check -ne 'NO') {

               

                foreach ($reseller in $grpa) {

               

                $resellerid0 = $reseller[0]

                $resellerid1 = $reseller[1]

               

                if($reseller -ne 'NONE') {   

               

                      $check4container = Get-SwisData $swis "select container.customproperties.CustomerID FROM Orion.Container WHERE container.customproperties.CustomerID = @containerid" @{containerid=$resellerid1}

                      #"Query result :$check4container"

               

                          if($check4container -ne $resellerid1) {

               

                          $customGRPprops = @{ CustomerID="$resellerid1" }

                         

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

                          # group name

                          "$resellerid0",

                          "Core",

                          60,

                          0,

                          "$resellerid0",

                          "true",

                          # group members

                          ([xml]@(

                              "<ArrayOfMemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'>",

                              " ",

                              "</ArrayOfMemberDefinitionInfo>"

                          )).DocumentElement

                              )).InnerText

               

                          $newgroupuri = ((Get-SwisData $swis "SELECT Uri FROM Orion.Container WHERE ContainerID=@id" @{id=$groupId}) + "/CustomProperties" )

               

                          Set-SwisObject $swis -URI $newgroupuri -Properties $customGRPprops -Verbose

               

                          "`r`nCreated Group: $resellerid0 CustomerID: $resellerid1" | Add-Content  update-log.txt -PassThru | Write-Host

               

                          $newgroupcount++

               

                          }else{

               

                          "Group $resellerid0 already in Solarwinds" | Add-Content  update-log.txt -PassThru | Write-Host

               

                          $groupalreadyinsolarwinds++

               

                          }

               

                      }

              }

                }

                 

              }

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              # Attach Sub Groups to Master Groups.

               

              foreach ($nmsubgroup in $CSVcompletedIMPORT) {

               

                # Assign new varaibles to Colums from the CSV file

                $subgrp1 = $nmsubgroup.Reseller_1

                $subgrp1id = $nmsubgroup.Reseller_1ID

                $subgrp2 = $nmsubgroup.Reseller_2

                $subgrp2id = $nmsubgroup.Reseller_2ID

                $subgrp3 = $nmsubgroup.Reseller_3

                $subgrp3id = $nmsubgroup.Reseller_3ID

                $subgrp4 = $nmsubgroup.Reseller_4

                $subgrp4id = $nmsubgroup.Reseller_4ID

                $subgrp5 = $nmsubgroup.Reseller_5

                $subgrp5id = $nmsubgroup.Reseller_5ID

               

                # Trim leading and trailing spaces and characters from variables above.

                $tsubgrp1 = $subgrp1.Trim()

                $tsubgrp1id = $subgrp1id.Trim()

                $tsubgrp2 = $subgrp2.Trim()

                $tsubgrp2id = $subgrp2id.Trim()

                $tsubgrp3 = $subgrp3.Trim()

                $tsubgrp3id = $subgrp3id.Trim()

                $tsubgrp4 = $subgrp4.Trim()

                $tsubgrp4id = $subgrp4id.Trim()

                $tsubgrp5 = $subgrp5.Trim()

                $tsubgrp5id = $subgrp5id.Trim()

               

                # create a new array of the trim variables

                $grpb = @(($tsubgrp1,$tsubgrp1id),($tsubgrp2,$tsubgrp2id),($tsubgrp3,$tsubgrp3id),($tsubgrp4,$tsubgrp4id),($tsubgrp5,$tsubgrp5id))

               

                # If Monitor Check field is No, don't even process it

                if($nmbsubgroup.Monitor_Check -ne 'NO') {

               

                #loop through each variable that is NOT 'NONE' and add the group as a sub group.

                  foreach ($subgrp in $grpb) {

               

                  if($subgrp -ne 'NONE') {

               

                  $subgrpid0 = $subgrp[0]

                  $subgrpid1 = $subgrp[1]

               

                  $getgroupuri = Get-SwisData $swis "select uri FROM Orion.Container WHERE container.customproperties.CustomerID=@groupname" @{groupname=$subgrpid1}

                  #$getgroupuri

                  $getgroupmemberuri = Get-SwisData $swis "select memberuri from orion.containermembers Where memberuri=@nodeuri" @{nodeuri=$getgroupuri}

                  #$getgroupmemberuri

               

                      if ($getgroupmemberuri -eq $null) {

                 

                          if($subgrpid1 -eq $tsubgrp1id) {

               

                              $mastergroupid = Get-SwisData $swis "select containerid from Orion.Container where container.customproperties.CustomerID=394;"

                              $subgroupUri = Get-SwisData $swis "SELECT Uri FROM Orion.Container WHERE container.customproperties.CustomerID=@id" @{id=$tsubgrp1id}

               

                              #"MasterID : $mastergroupid"

                              #"suburi : $subgroupUri"

               

                              Invoke-SwisVerb $swis "Orion.Container" "AddDefinition" @(

                              # group ID

                              $mastergroupid,

                              # group member to add

                              ([xml]"

                              <MemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'>

                              <Name></Name>

                              <Definition>$subgroupUri</Definition>

                              </MemberDefinitionInfo>"

                              ).DocumentElement

                              ) | Out-Null

               

                             

                              "`r`nAdding Level1 Group : $subgrpid0 to Master Group : Accelerated Connections Inc." | Add-Content  update-log.txt -PassThru | Write-Host

                             

                              $newsubgroupcountlevel1++

                             

                              }

                          if($subgrpid1 -eq $tsubgrp2id) {

               

                              $mastergroupid = Get-SwisData $swis "select containerid from Orion.Container WHERE container.customproperties.CustomerID=@sid" @{sid=$tsubgrp1id}

                              $subgroupUri = Get-SwisData $swis "SELECT Uri FROM Orion.Container WHERE container.customproperties.CustomerID=@id" @{id=$tsubgrp2id}

               

                              #"MasterID : $mastergroupid"

                              #"suburi : $subgroupUri"

               

                              Invoke-SwisVerb $swis "Orion.Container" "AddDefinition" @(

                              # group ID

                              $mastergroupid,

                              # group member to add

                              ([xml]"

                              <MemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'>

                              <Name></Name>

                              <Definition>$subgroupUri</Definition>

                              </MemberDefinitionInfo>"

                              ).DocumentElement

                              ) | Out-Null

               

                              

                              "`r`nAdding Level2 Group : $subgrpid0 to Level1 Group : $tsubgrp1" | Add-Content  update-log.txt -PassThru | Write-Host

               

                              $newsubgroupcountlevel2++

                             

                              }

                          if($subgrpid1 -eq $tsubgrp3id) {

               

                              $mastergroupid = Get-SwisData $swis "select containerid from Orion.Container where container.customproperties.CustomerID=@sid" @{sid=$tsubgrp2id}

                              $subgroupUri = Get-SwisData $swis "SELECT Uri FROM Orion.Container WHERE container.customproperties.CustomerID=@id" @{id=$tsubgrp3id}

               

                              #"MasterID : $mastergroupid"

                              #"suburi : $subgroupUri"

               

                              Invoke-SwisVerb $swis "Orion.Container" "AddDefinition" @(

                              # group ID

                              $mastergroupid,

                              # group member to add

                              ([xml]"

                              <MemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'>

                              <Name></Name>

                              <Definition>$subgroupUri</Definition>

                              </MemberDefinitionInfo>"

                              ).DocumentElement

                              ) | Out-Null

               

                             

                              "`r`nAdding Level3 Group : $subgrpid0 to Level2 Group : $tsubgrp2" | Add-Content  update-log.txt -PassThru | Write-Host

               

                              $newsubgroupcountlevel3++

                             

                              }

                          if($subgrpid1 -eq $tsubgrp4id) {

               

                              $mastergroupid = Get-SwisData $swis "select containerid from Orion.Container where container.customproperties.CustomerID=@sid" @{sid=$tsubgrp3id}

                              $subgroupUri = Get-SwisData $swis "SELECT Uri FROM Orion.Container WHERE container.customproperties.CustomerID=@id" @{id=$tsubgrp4id}

               

                              #"MasterID : $mastergroupid"

                              #"suburi : $subgroupUri"

               

                              Invoke-SwisVerb $swis "Orion.Container" "AddDefinition" @(

                              # group ID

                              $mastergroupid,

                              # group member to add

                              ([xml]"

                              <MemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'>

                              <Name></Name>

                              <Definition>$subgroupUri</Definition>

                              </MemberDefinitionInfo>"

                              ).DocumentElement

                              ) | Out-Null

               

                             

                              "`r`nAdding Level4 Group : $subgrpid0 to Level3 Group : $tsubgrp3 " | Add-Content  update-log.txt -PassThru | Write-Host

               

                              $newsubgroupcountlevel4++

                             

                              }

               

                          if($subgrpid1 -eq $tsubgrp5id) {

               

                              $mastergroupid = Get-SwisData $swis "select containerid from Orion.Container where container.customproperties.CustomerID=@sid" @{sid=$tsubgrp4id}

                              $subgroupUri = Get-SwisData $swis "SELECT Uri FROM Orion.Container WHERE container.customproperties.CustomerID=@id" @{id=$tsubgrp5id}

               

                              #"MasterID : $mastergroupid"

                              #"suburi : $subgroupUri"

               

                              Invoke-SwisVerb $swis "Orion.Container" "AddDefinition" @(

                              # group ID

                              $mastergroupid,

                              # group member to add

                              ([xml]"

                              <MemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'>

                              <Name></Name>

                              <Definition>$subgroupUri</Definition>

                              </MemberDefinitionInfo>"

                              ).DocumentElement

                              ) | Out-Null

               

                             

                              "`r`nAdding Level5 Group : $subgrpid0 to Level4 Group : $tsubgrp4 " | Add-Content  update-log.txt -PassThru | Write-Host

               

                              $newsubgroupcountlevel5++

                             

                              }

                          } else {

               

                          "Group : $subgrpid0 already attached to another group"

               

                          }

                     

                      }

                     

                     

               

                  }

              }

              }

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              # Attach Nodes to Home Group

               

              :outer foreach ($nmsaddnode in $CSVcompletedIMPORT) {

               

                # Assign new varaibles to Colums from the CSV file

                $nodegrp1 = $nmsaddnode.Reseller_1

                $nodegrp1id = $nmsaddnode.Reseller_1ID

                $nodegrp2 = $nmsaddnode.Reseller_2

                $nodegrp2id = $nmsaddnode.Reseller_2ID

                $nodegrp3 = $nmsaddnode.Reseller_3

                $nodegrp3id = $nmsaddnode.Reseller_3ID

                $nodegrp4 = $nmsaddnode.Reseller_4

                $nodegrp4id = $nmsaddnode.Reseller_4ID

                $nodegrp5 = $nmsaddnode.Reseller_5

                $nodegrp5id = $nmsaddnode.Reseller_5ID

               

                # Trim leading and trail characters and white space

                $tnodegrp1 = $nodegrp1.Trim()

                $tnodegrp1id = $nodegrp1id.Trim()

                $tnodegrp2 = $nodegrp2.Trim()

                $tnodegrp2id = $nodegrp2id.Trim()

                $tnodegrp3 = $nodegrp3.Trim()

                $tnodegrp3id = $nodegrp3id.Trim()

                $tnodegrp4 = $nodegrp4.Trim()

                $tnodegrp4id = $nodegrp4id.Trim()

                $tnodegrp5 = $nodegrp5.Trim()

                $tnodegrp5id = $nodegrp5id.Trim()

               

                # create new array of the trimmed variables

                $grpb = @(($tnodegrp1,$tnodegrp1id),($tnodegrp2,$tnodegrp2id),($tnodegrp3,$tnodegrp3id),($tnodegrp4,$tnodegrp4id),($tnodegrp5,$tnodegrp5id))

               

                #reverse the variables in the array for easier processing.

                [array]::Reverse($grpb)

               

                If($nmsaddnode.Monitor_Check -ne 'NO') {

               

                #loop through each variable and assign a node to the group.

                  foreach ($addnode in $grpb) {

               

                      $addnodeid0 = $addnode[0]

                      $addnodeid1 = $addnode[1]

               

                      # $cleanaddnode = NormalizeName($addnode)

                     

                      $nmsconnectionid = $nmsaddnode.ConnectionID

                      $nmslocation = $nmsaddnode.Location_Name

               

                      if ($nmslocation -match "&") {

               

                          $nmslocation -replace "&", "and"

               

                          $nmslocationclean = $nmslocation -replace "&", "and"

               

                          #$nmslocation

               

                       }

                     

                      $getnodeuri = Get-SwisData $swis "SELECT uri FROM Orion.Nodes WHERE Nodes.CustomProperties.ConnectionID=@connectionid" @{connectionid=$nmsconnectionid}

                      # $getnodeuri

                      $getcontainermemberuri = Get-SwisData $swis "select memberuri from orion.containermembers Where memberuri=@nodeuri" @{nodeuri=$getnodeuri}

                      # $getcontainermemberuri

               

                      # if ($getcontainermemberuri -eq $null) {

               

               

                      if($addnodeid1 -ne 'NONE') {

               

                         

                          $getnodegroup = Get-SwisData $swis "SELECT containerid FROM Orion.Container WHERE container.customproperties.CustomerID=@groupname" @{groupname=$addnodeid1}

                          #$getnodegroup

                         

               

                          #$addnode

                          #$getnodeuri

                          #$getnodegroup

                          $nmsconnectionid = $nmsaddnode.ConnectionID

                          $nmslocation = $nmsaddnode.Location_Name

                          #"====================="

                                   

                         

                          Invoke-SwisVerb $swis "Orion.Container" "AddDefinition" @(

                          # group ID

                          $getnodegroup,

                          # group member to add

                          ([xml]"

                          <MemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'>

                          <Name>$nmslocationclean</Name>

                          <Definition>$getnodeuri</Definition>

                          </MemberDefinitionInfo>"

                          ).DocumentElement

                            ) | Out-Null

               

                          # "`r`nAdded Node : $nmslocation with ConnectionID: $nmsconnectionid to Group: $addnodeid0" | Add-Content  update-log.txt -PassThru | Write-Host

                          "`r`nAdded Node : $nmslocation with ConnectionID: $nmsconnectionid to Group: $addnodeid0"

               

                          $nodesaddedtogroupcount++

                         

                         

                          break : outer

                         

                          }

               

                      }

               

                  }

                  }

              # }

               

               

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              # Unmanage connections

              foreach ($toberemoved in $CSVcancelledIMPORT) {

               

                  $node2remove = $toberemoved.ConnectionID

                 

                  $canSWconnectionID = Get-SwisData $swis "Select Unmanaged from Orion.Nodes where Nodes.CustomProperties.ConnectionID = @removeconid" @{removeconid=$node2remove}

               

                  #$canSWconnectionID

               

                  if ($canSWconnectionID -eq $true) {

               

                  "`r`nConnectionID : $node2remove has already been removed" | Add-Content  update-log.txt -PassThru | Write-Host  

               

                  }elseif ($canSWconnectionID -eq $false) {

               

                  $cancelledNodeID = Get-SwisData $swis "Select NodeID from orion.nodes where Nodes.CustomProperties.ConnectionID = @removeuri" @{removeuri=$node2remove}

                 

                  # Remove-SwisObject $swis -URI $cancelledURI

               

                  $unmanagefrom = Get-Date

                  $unmanageto = (Get-Date).AddYears(99)

               

                  Invoke-SwisVerb $swis Orion.Nodes Unmanage @("N:$cancelledNodeID",$unmanagefrom,$unmanageto,"false")

               

                  "`r`nUnmanaging ConnectionID: $node2remove" | Add-Content  update-log.txt -PassThru | Write-Host

               

                  $unmananagednodecount++

               

                  }else{

               

                  "Uh-oh...`r`nConnectionID : $node2remove has already been removed" | Add-Content  update-log.txt -PassThru | Write-Host  

              }

              }

               

               

              <# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              # Clean up Connections that are in SW but not in the Import file.

               

              $allSWconnectionID = Get-SwisData $swis "Select Nodes.CustomProperties.ConnectionID from Orion.Nodes"

               

               

              $diffcleanup = Compare-Object $allSWconnectionID $CSVcompletedIMPORT.ConnectionID

               

              <#$diffcleanup.InputObject #>

               

              #>

              <#

               

              foreach ($extranode in $diffcleanup) {

               

                  $node2stoppolling = $extranode.InputObject

                 

                  $removeSWconnectionID = Get-SwisData $swis "Select Unmanaged from Orion.Nodes where Nodes.CustomProperties.ConnectionID = @removeconid" @{removeconid=$node2stoppolling}

               

                  #$canSWconnectionID

               

                  if ($removeSWconnectionID -eq $true) {

               

                  #"`r`nConnectionID : $node2remove has already been removed" | Add-Content  update-log.txt -PassThru | Write-Host  

               

                  }elseif ($removeSWconnectionID -eq $false) {

               

                  $removedNodeID = Get-SwisData $swis "Select NodeID from orion.nodes where Nodes.CustomProperties.ConnectionID = @removeuri" @{removeuri=$node2stoppolling}

                 

                  # Remove-SwisObject $swis -URI $cancelledURI

               

                  $unmanagefrom = Get-Date

                  $unmanageto = (Get-Date).AddYears(99)

               

                  Invoke-SwisVerb $swis Orion.Nodes Unmanage @("N:$removedNodeID",$unmanagefrom,$unmanageto,"false")

               

                  "`r`nUnmanaging ConnectionID that should not be monitored: $node2stoppolling" | Add-Content  update-log.txt -PassThru | Write-Host

               

                  $unmananagednodecount2++

               

                  }else{

               

                  "Uh-oh..."

              }

              }

               

              #>

               

               

              # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

              # Report Builder

              # Take the output update-log.txt and report-header.txt, combine and email once the script run is complete.

               

              $scriptend = get-date

               

              "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" | Add-Content  report-header.txt -PassThru | Write-Host

              "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" | Add-Content  report-header.txt -PassThru | Write-Host

              " NEED TO ADD FUNCTION TO DELETE UNMANAGED CONNECTIONS AFTER 60 DAYS" | Add-Content  report-header.txt -PassThru | Write-Host

              "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" | Add-Content  report-header.txt -PassThru | Write-Host

              "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" | Add-Content  report-header.txt -PassThru | Write-Host

              "   " | Add-Content  report-header.txt -PassThru | Write-Host

              "   " | Add-Content  report-header.txt -PassThru | Write-Host

              "Script Start : $scriptstart" | Add-Content  report-header.txt -PassThru | Write-Host

              "Script Finish : $scriptend" | Add-Content  report-header.txt -PassThru | Write-Host

              "=-=-=-=-=-=-=-=-=-=-=-=-=-=" | Add-Content  report-header.txt -PassThru | Write-Host

              "Errors : $errorcount" | Add-Content  report-header.txt -PassThru | Write-Host

              "Nodes Added : $newmonitorcount" | Add-Content  report-header.txt -PassThru | Write-Host

              "Nodes Added to a Group $nodesaddedtogroupcount" | Add-Content  report-header.txt -PassThru | Write-Host

              "Nodes with Changed IP : $ipaddresscount" | Add-Content  report-header.txt -PassThru | Write-Host

              "Nodes with no change : $nochangecount" | Add-Content  report-header.txt -PassThru | Write-Host

              "=-=-=-=-=-=-=-=-=-=-=-=-=-=" | Add-Content  report-header.txt -PassThru | Write-Host

              "Nodes Unmanaged : $unmananagednodecount" | Add-Content  report-header.txt -PassThru | Write-Host

              "Nodes We don't need to monitor : $unmananagednodecount2" | Add-Content  report-header.txt -PassThru | Write-Host

              "=-=-=-=-=-=-=-=-=-=-=-=-=-=" | Add-Content  report-header.txt -PassThru | Write-Host

              "New Groups Created : $newgroupcount " | Add-Content  report-header.txt -PassThru | Write-Host

              "Level 1 Sub groups added :  $newsubgroupcountlevel1 " | Add-Content  report-header.txt -PassThru | Write-Host

              "Level 2 Sub groups added :  $newsubgroupcountlevel2 " | Add-Content  report-header.txt -PassThru | Write-Host

              "Level 3 Sub groups added :  $newsubgroupcountlevel3 " | Add-Content  report-header.txt -PassThru | Write-Host

              "Level 4 Sub groups added :  $newsubgroupcountlevel4 " | Add-Content  report-header.txt -PassThru | Write-Host

              "Level 5 Sub groups added :  $newsubgroupcountlevel5 " | Add-Content  report-header.txt -PassThru | Write-Host

              "   " | Add-Content  report-header.txt -PassThru | Write-Host

              "   " | Add-Content  report-header.txt -PassThru | Write-Host

              "   " | Add-Content  report-header.txt -PassThru | Write-Host

               

              Get-Content "report-header.txt","update-log.txt" | Add-Content manage-import-report.txt

               

              Remove-Item "report-header.txt"

              Remove-Item "update-log.txt"

               

              $reportbody = (get-content manage-import-report.txt | out-string)

               

              Send-MailMessage -From customerNMS@ -to sys.eng@ -Body $reportbody -Subject "Mange Import Report" -SmtpServer mysmtp.server.com

                • Re: groups not nesting
                  madhavan

                  Hi,

                   

                  I'm not able to simulate this script to check where the issue is as there are lot of file dependencies, but looking at the script I see that there is an issue when fetching the mastergroupid in section "# Attach Sub Groups to Master Groups." It is hardcoded to 394.

                   

                  $mastergroupid = Get-SwisData $swis "select containerid from Orion.Container where container.customproperties.CustomerID=394;"

                   

                  Check to see if this is correct. Looking at the script, I think this value should be fetched based on the group name and not hard coded.