3 Replies Latest reply on Mar 13, 2017 3:41 PM by Michael Halpin

    Deleting Interfaces

    nick_scott

      I am creating a basic script that will ultimately automatically remove loopback, mgmt., null interfaces and others once a week.  The script below does return Uri's as intended however when I add the remove object statement it throws an error I do not understand

       

      $ErrorActionPreference = 'Stop'

       

      #Add the snapin

       

      Add-PSSnapin SwisSnapin

       

      #create a connection to solarwinds

       

      $hostname = '10.X.X.X'
      $username = 'admin'
      $password = 'XXXXX'

       

      #swis = Connect-Swis -Hostanme $hostname -Trusted

       

      $swis = Connect-Swis -Hostname $hostname -Username $username -Password $password

       

      #Query Data

       

      $query = Get-SwisData $swis "Select uri FROM Orion.NPM.interfaces WHERE InterfaceName = 'lo'"

       

      # This returns the following Uri's

      swis://XXXX.my.domain.net/Orion/Orion.Nodes/NodeID=123/Interfaces/InterfaceID=321

      swis://XXXX.my.domain.net/Orion/Orion.Nodes/NodeID=456/Interfaces/InterfaceID=654

      swis://XXXX.my.domain.net/Orion/Orion.Nodes/NodeID=789/Interfaces/InterfaceID=987

       

      # When adding the final remove object line...

       

      $query | ForEach-Object { Remove-SwisObject $swis $query }

       

      I get the error below

       

      ForEach-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter

      'Uri'. Specified method is not supported.

      At C:\Users\nsd000.QACMA\Documents\PowerShell Scripts\Do not Modify\Delete Loopbacks MGMT and Null

      Interfaces.ps1:22 char:10

      + $query | ForEach-Object { Remove-SwisObject $swis $query }

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

          + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], ParameterBindingException

          + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ForEachObjectComma

         nd

       

      This format works well when I use the same logic on the script below.  The only difference I can see is that the script above is pulling from Orion.NPM.Interfaces and the one below is Orion.Volumes

       

      $ErrorActionPreference = 'Stop'

      #Add the snapin
      Add-PSSnapin SwisSnapin

      #create a connection to solarwinds

      $hostname = '10.xXX'

      $username = 'admin'

      $password = 'XXXXX'

      #swis = Connect-Swis -Hostanme $hostname -Trusted
      $swis = Connect-Swis -Hostname $hostname -Username $username -Password $password

      #Query Data

      $query = Get-SwisData $swis "Select uri FROM Orion.volumes WHERE VolumeDescription = 'Cached Memory' AND VolumePercentUsed ='100'"

      # Remove Objects 

      $query | ForEach-Object { Remove-SwisObject $swis $query }

        • Re: Deleting Interfaces
          Michael Halpin

          Hey @nick_scott,

           

          when the $query returns a number of objects, it'll store them as an array

           

          $query.gettype()
          
          
          IsPublic IsSerial Name                                     BaseType                                                                                                                           
          -------- -------- ----                                     --------                                                                                                                           
          True     True     Object[]                                 System.Array   
          

           

           

          Each element in the array however is a string

           

          $query[0].gettype()
          
          
          IsPublic IsSerial Name                                     BaseType                                                                                                                           
          -------- -------- ----                                     --------                                                                                                                           
          True     True     String                                   System.Object    
          

           

           

          So the easiest thing to do is just to do a foreach

           

          foreach ($q in $query){
            write-output "This is a string: $q"
          }
          This is a string: swis://OrionVM./Orion/Orion.Nodes/NodeID=1/Volumes/VolumeID=3
          This is a string: swis://OrionVM./Orion/Orion.Nodes/NodeID=1/Volumes/VolumeID=1
          This is a string: swis://OrionVM./Orion/Orion.Nodes/NodeID=1/Volumes/VolumeID=2
          
          1 of 1 people found this helpful
            • Re: Deleting Interfaces
              nick_scott

              Hey Mike, thanks for the quick reply.  So by "manually" adding the additional text we essentially change the Uri value to a string ?  I played around with the #Remove Object line quite a bit and was able to remove all values found in the query using the last line below

               

              $ErrorActionPreference = 'Stop'

               

              #Add the snapin
              Add-PSSnapin SwisSnapin

               

              #create a connection to solarwinds

              $hostname = X
              $username = X
              $password = X

               

              #swis = Connect-Swis -Hostanme $hostname -Trusted
              $swis = Connect-Swis -Hostname $hostname -Username $username -Password $password

               

              #Query Data

              $query = Get-SwisData $swis "Select uri FROM Orion.NPM.interfaces WHERE InterfaceType ='24'"

               

              # Convert Uri values to strings https://thwack.solarwinds.com/thread/110500

              foreach ($q in $query){ 
              write-output "This is a string: $q" 
              }

               

              # Remove Objects

              $query | Remove-SwisObject $swis

               

              ---------------------------------------------------------------------------------------------------------------------------------------------------------------------

              Anyone see any major issues with the script above ?

               

                • Re: Deleting Interfaces
                  Michael Halpin

                  Hi Nick,

                   

                  Sorry, I missed something important in your first post. In this new code, using the foreach loop, the below would have done.

                  $query | ForEach-Object { Remove-SwisObject $swis $query }

                   

                  is not the same as

                   

                  $query | Remove-SwisObject $swis

                   

                  Basically, in the original code you post the foreach loop isn't really parsing each object in the pipleline, the second "$query" is called each time, rather than the individual array members. (I haven't tested, but I think $_ would have done instead of $query)

                   

                  In this new code, using the foreach loop, the below would have done. (I muddied the waters with write-output as an example )

                   

                  foreach ($q in $query){ 
                    $q | Remove-SwisObject $swis
                  }

                   

                  But ultimately, $query | Remove-SwisObject $swis will be more efficient because of how the remove-SwisObject cmdlet does a bulk update, rather than individually cycling through the array like the example I suggested

                  1 of 1 people found this helpful