ccarvell

Powershell - export NAS/Emailgtw/url_response to csv for excel

Discussion created by ccarvell on Feb 3, 2011

I created this bit of code today to export the contents of the NAS,emailgtw,url_response to a csv for easy viewing.

 

This is anything but perfect, but it gets the job done here!  Basicly, it expects that the config files are located next to the .ps1 file (powershell script).  You will also need the NimbusAPI.DLL file next to the .ps1 file.  Great for starter learning.

 

Code Below:

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

[Reflection.Assembly]::LoadFile("C:\Scripts\Powershell\Nimbus\NAS\NimbusAPI.dll")
$path = "C:\Scripts\Powershell\Nimbus\nas"

$selectNode_NAS = "/configuration/section/*/section"
$selectNode_urlResponse = "/configuration/section/section"
$selectNode_emailgtw = "/configuration/section/section"

Clear

Function GetColumnList([Xml.XmlNodeList]$xmlnodelist)
{
 $colnames = New-Object system.Collections.ArrayList
 $void = $colnames.clear()
 foreach ($cnode in $xnode)
 {
  foreach ($valset in $cnode.key)
  {
   [string]$colName = $valset.name
   if ($colName -ne "" -or $colName -ne $null)
   {
    if ($colnames.contains($colName) -eq $false)
    {
     $void = $colnames.add($colName)
    }
   }
  }
 }
 $void = $colnames.sort()
 return $colnames
}

$f = New-Object Nimsoft.NimBUS.Configuration.FileConfig("$path\nas.cfg")
[Xml.XmlDocument]$xdoc = $f.getinxml()
[Xml.XmlNodeList]$xnode = $xdoc.SelectNodes($selectNode_NAS)
[System.Collections.ArrayList]$vals = New-Object system.collections.arraylist
$vals = GetColumnlist -xmlnodelist $xnode
$dt = New-Object system.Data.DataTable("Results")
$void = $dt.Columns.Add("rulename")
foreach($colName in $vals)
{
 $void = $dt.Columns.Add($colName)
}

foreach ($cnode in $xnode)
{
 $dr = $dt.newrow()
 $dr.RuleName = $cnode.name
 foreach ($valset in $cnode.key)
 {
  [string]$colName = $valset.name
  if ($colName -ne $null -and $colname -ne "")
  {
   Write-Host $colname
   $dr[$colname] = $valset.value
  }
 }
 $dt.Rows.Add($dr)
}
$dt | Export-Csv "$path\NAS.CSV"

Outcomes