4 Replies Latest reply on Aug 7, 2013 4:00 PM by HerrDoktor

    Exporting Items to CSV via SWIS

    HerrDoktor

      Hi Folks,

      I want to export Data to a CSV to compare this Data with another import CSV.

       

      I already have the "wrapping" around but I can not export the Data. Is there any Powershell-Expert out there to help?

       

      #Load SWIS Snapin
      if (!(Get-PSSnapin | where {$_.Name -eq "SwisSnapin"})) {
          Add-PSSnapin "SwisSnapin"
      }
      # CSV definition
      $ExportFile = 'D:\_NCM_Scripts\Import\ExprtNCM.csv'
      
      #Define Solarwinds System
      $hostname = "localhost"
      $username = "admin"
      $password = "admin"
      $cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
      
      #Connect to the System
      $target = Connect-Swis -v2 -Credential $cred -Hostname $hostname
      
      #Get all URIs of Nodes in the System
      $UriBasket = Get-SwisData $target "SELECT Uri FROM Orion.Nodes"
      
      #Loop through all URIs and get Node Properties
      foreach($UriEntry in $UriBasket){
          $CustomProps = $UriEntry+'/CustomProperties'
          $NodeProps = Get-SwisObject $target $UriEntry
        $NodeCustomProps = Get-SwisObject $target $CustomProps
      
      write-host $NodeProps.Caption
      }
      
      
      
      

       

      I would like to get $NodeProps.Caption etc into a CSV File, I can already access them by the Variables..I just don't know how to build my CSV File in Powershell.

      As you can see I can already access the Data I want in the write-host statement

       

      Thanks for any help

        • Re: Exporting Items to CSV via SWIS
          Bohumil Klimek

          PowerShell has build-in cmdlet for that purpose: "Export-Csv".

           

          I hope, that help you, if not, please let me know.

          1 of 1 people found this helpful
            • Re: Exporting Items to CSV via SWIS
              HerrDoktor

              I could not get it working with Export CSV, here's what I Came up with:

               

              ### Export all nodes from DB into CSV File ####

               

              #Get all URIs of Nodes in the System

              $UriBasket = Get-SwisData $target "SELECT Uri FROM Orion.Nodes"

               

              #Open Streamwriter

              $outfile = New-Object System.IO.Streamwriter($ExportFile)

              $outfile.WriteLine($Header)

               

               

              #Loop through all URIs and get Node Properties write to $outfile

              foreach($UriEntry in $UriBasket){

                  $CustomProps = $UriEntry+'/CustomProperties'

                  $NodeProps = Get-SwisObject $target $UriEntry

                  $NodeCustomProps = Get-SwisObject $target $CustomProps

                  $outfile.WriteLine($NodeProps.Caption+','+$NodeCustomProps.xxxxx+','+$NodeCustomProps.xxxxxxxxx+','+$NodeCustomProps.xxxxxxxxx+','+$NodeProps.IPAddress+','+$NodeCustomProps.xxxxxxx+','+$NodeProps.Community+','+$NodeCustomProps.xxxxxxxxx+','+

                $NodeCustomProps.xxxxxxxxxx+','+$NodeCustomProps.xxxxxxxxx+','+$NodeCustomProps.xxxxxxxxx+','+$NodeCustomProps.xxxxxxxxx+','+$NodeCustomProps.xxxxxxxxx)

                

              }

               

               

              #Write File to disk and close Streamwriter  

              $outfile.Flush()

              $outfile.Close()

                • Re: Exporting Items to CSV via SWIS
                  tdanner

                  That should work as long as none of your custom property values contain a comma, but here's what Bohumil was referring to:

                   

                  Get-SwisData $target "SELECT N.Caption, N.CustomProperties.xxxx, N.CustomProperties.xxxxx FROM Orion.Nodes N" | Export-Csv $ExportFile

                  1 of 1 people found this helpful