PoshCode Logo PowerShell Code Repository

Get-NaNfsExport by glnsize 35 months ago (modification of post by glnsize view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/923"></script>download | new post

Function that will retrieve all the NFS Exports from a NetApp SAN. Example usage of thee Ontap SDK 3.5 avaliable here http://communities.netapp.com/docs/DOC-1365….

  1. #  Glenn Sizemore ~ www . Get-Admin . Com
  2. #  Example Powershell function to get the NFS Exports from a NetApp Filer
  3. #  First you'll need to download the OnTap SDK 3.5 : http://communities.netapp.com/docs/DOC-1365
  4. #  within the download your interested in .\manage-ontap-sdk-3.5\lib\DotNet\ManageOntap.dll
  5. #  Next load the library...
  6. #  $Path = 'C:\Users\glnsize\Downloads\manage-ontap-sdk-3.5\lib\DotNet\ManageOntap.dll'
  7. #  [Reflection.Assembly]::LoadFile($Path)
  8. #
  9. #  Almost there next step create a NaServer connection object
  10. #  Here we are connecting to the NetApp Filer TOASTER1, and setting the api to V1.8
  11. #  $NaServer = New-Object NetApp.Manage.NaServer("TOASTER1",1,8)
  12. #  Call the setAdminUser Method and supply some credentials
  13. #  $NaServer.SetAdminUser('root', 'password')
  14. #
  15. #  Now we're ready to go simply call the function passing your NAServer object as a parameter.
  16. #  Get-NaNfsExport -Server $NaServer
  17. #
  18. #  Get-NaNfsExport -Server $NaServer -Path /vol/vol0
  19.  
  20. Function Get-NaNfsExport {
  21.     Param(
  22.         [NetApp.Manage.NaServer]
  23.         $Server,
  24.         [String]
  25.         $Path
  26.     )
  27.     Begin {
  28.         $out = @()
  29.     }
  30.     Process {
  31.         trap [NetApp.Manage.NaAuthException] {
  32.             # Example trap to catch bad credentials
  33.             Write-Error "Bad login/password".
  34.             break
  35.         }
  36.         #generate a new naelement request
  37.         $NaOut = New-Object NetApp.Manage.NaElement("nfs-exportfs-list-rules")
  38.              
  39.         # $NaServer.InvokeElem($NaOut) -> retrieve the results of the $NaOut request
  40.         # ..($NaOut).GetChildByName("rules") -> select the rules element from results
  41.         # ..GetChildByName("rules").getchildren() -> get any child elements under rules.
  42.         $NaResults = $Server.InvokeElem($NaOut).GetChildByName("rules").getchildren()    
  43.        
  44.         #ForEach NFS Rule returned, serialize the output into a PSObject.
  45.         foreach ($NaElement in $NaResults) {
  46.             $NaNFS = "" | Select-Object Path, ActualPath, ReadOnly, ReadWrite, Root, Security
  47.             $NaNFS.Path = $NaElement.GetChildContent("pathname")
  48.            
  49.             # This is where the OnTap SDK can get a little nuts...
  50.             # if you perfer XML then simply try $NaElement.ToPrettyString('')
  51.             switch ($NaElement) {
  52.                 # if Read-Only is present
  53.                 {$_.GetChildByName("read-only")}
  54.                         {      
  55.                         # Get all child elements
  56.                                 $ReadOnly = ($_.GetChildByName("read-only")).getchildren()
  57.                         #Foreach elm in read-only
  58.                                 foreach ($read in $ReadOnly) {
  59.                             # [bool] if exists "all-hosts"
  60.                                         If ($read.GetChildContent("all-hosts")) {
  61.                                                 $roList = 'All-Hosts'
  62.                                         }
  63.                             # Else get the name of the export!
  64.                                         Elseif ($read.GetChildContent("name")) {
  65.                                                 $roList += $read.GetChildContent("name")
  66.                                         }
  67.                                 }
  68.                         # add our new list to the output
  69.                                 $NaNFS.ReadOnly = $roList
  70.                         }
  71.                 # if Read-write is present
  72.                 {$_.GetChildByName("read-write")}
  73.                         {      
  74.                         $ReadWrite = ($_.GetChildByName("read-write")).getchildren()
  75.                                 foreach ($write in $ReadWrite) {
  76.                                         If ($write.GetChildContent("all-hosts")) {
  77.                                                 $rwList = 'All-Hosts'
  78.                                         }
  79.                                         Elseif ($r.GetChildContent("name")) {
  80.                                                 $rwList += $write.GetChildContent("name")
  81.                                         }
  82.                                 }
  83.                                 $NaNFS.ReadWrite = $rwList
  84.                         }
  85.                 # if root is present
  86.                 {$_.GetChildByName("root")}
  87.                         {
  88.                                 $Root = ($_.GetChildByName("root")).getchildren()
  89.                                 foreach ($r in $Root) {
  90.                                         If ($r.GetChildContent("all-hosts")) {
  91.                                                 $rrList = 'All-Hosts'
  92.                                         }
  93.                                         Elseif ($r.GetChildContent("name")) {
  94.                                                 $rrList += $r.GetChildContent("name")
  95.                                         }
  96.                                 }
  97.                                 $NaNFS.Root = $rrList
  98.                         }
  99.                 {$_.GetChildByName("sec-flavor")}
  100.                         {
  101.                                 $Security = ($_.GetChildByName("sec-flavor")).getchildren()
  102.                                 foreach ($s in $Security) {
  103.                                         if ($r.GetChildContent("flavor")) {
  104.                                                 $SecList += $r.GetChildContent("flavor")
  105.                                         }
  106.                                 }
  107.                                 $NaNFS.Security = $SecList
  108.                         }
  109.                 {$_.GetChildByName("actual-pathname")}
  110.                     {
  111.                    
  112.                         $NaNFS.ActualPath = $_.GetChildByName("actual-pathname")
  113.                     }
  114.                 # needed for mixed vol reporting...
  115.                 {!$_.GetChildByName("actual-pathname")}
  116.                     {
  117.                    
  118.                         $NaNFS.ActualPath = $_.GetChildContent("pathname")
  119.                     }
  120.             }
  121.             $out += $NaNFS
  122.         }
  123.     }
  124.     End {
  125.         If ($Path) {
  126.             return $out | ?{$_.Path -match $Patch}
  127.         }
  128.         else {
  129.             return $out
  130.         }
  131.     }
  132. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me