This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

Exporting Items to CSV via SWIS

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

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

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

  • 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()

  • 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

  • Hi,

    I tried to Pipe the SQL Query to the Export-CSV cmdlet but the CSV was always empty Or I could not access all Custom properties wit the SQL Query, so I came up with the StreamWriter.

    Thanks for the clarification I will try this as well.

    Kind Regards!