Microsoft Virtualization Discussions

New filer object enhancement

drdabbles
2,704 Views

This is probably an API enhancement, because it doesn't work with the VisualStudio API either...but...

I'd like to suggest an enhancement to the NaController class, but really to all classes in the NetApp toolkit. When using New-Object -Type NetApp.Ontapi.Filer.NaController, users will receive an error. This is caused by the NaController having no constructors that take zero arguments. This is mostly a problem when using PowerShell scripts that pass variables by reference to functions. To pass a variable by reference, you first have to define its type. There's no way to do this with the NaController class without passing an IP or hostname.

My specific case looks like this:

function ConnectSanController {

          param (

                    [ValidateNotNullOrEmpty()][string]$controllername,

                    [ref]$sancontroller,

                    [ref]$ex

          )

          PrintDebug("Entering ConnectSanController")

          Try {

                    [NetApp.Ontapi.Filer.NaController]$sancontroller.Value = Connect-NaController -name $controllername.Value -ErrorAction Stop

                    $ex.Value = $null

          } Catch [System.Exception] {

                    PrintMessage("ERROR: Could not connect to SAN controller!")

                    PrintMessage($_.exception.gettype().fullname)

                    PrintMessage($_.exception.message)

                    PrintMessage($_.exception.ToString())

                    $sancontroller.Value = $null

                    $ex.Value = $_.exception

          }

          PrintDebug("Exiting ConnectSanController")

}

ConnectSanController -ControllerName $controller -SanController [NetApp.Ontapi.Filer.NaController]$MainSanController -ex [System.Exception]$MainException

if ($MainSanController -eq $null) {

          PrintMessage($MainException)

          QuitScript -reason SANERROR

}

Obvisouly I don't need to use a function to connect to the controller, but it makes my script collection much more re-usable and makes my individual scripts more sensible when looking at the actual body of the script. Plus, it's just good programming practice to define variable types when defining variables...even if the language in use doesn't require it. Right?

2 REPLIES 2

timothyn
2,704 Views

Hi DrDabbles,

Well you've certainly shown me something new.  I suspect that [ref] is in PowerShell more for interoperability with other languages (COM, C#) rather than as a PowerShell idiom.  I personally would rather see functions that write objects to the pipeline and throw exceptions.  If you need to return multiple values, you might consider a hashtable or custom object (maybe built with Select-Object, New-Object).

But I'm not going to tell you not to use it.  Instead, I think you just need to declare your variables explicitly like this:

function ConnectSanController {

    param (

        [ValidateNotNullOrEmpty()][string]$controllername,

        [ref]$sancontroller,

        [ref]$ex

    )

    write-host "Entering ConnectSanController"

    Try {

        [NetApp.Ontapi.Filer.NaController]$sancontroller.Value = Connect-NaController -name $controllername -ErrorAction Stop

        $ex.Value = $null

    } Catch [System.Exception] {

        write-host "ERROR: Could not connect to SAN controller!"

        write-host $_.exception.ToString()

        $sancontroller.Value = $null

        $ex.Value = $_.exception

    }

    write-host "Exiting ConnectSanController"

}

$MainSanController = [NetApp.Ontapi.Filer.NaController]$null

$MainException = [System.Exception]$null

$controller = "10.61.169.28"

ConnectSanController -ControllerName $controller -SanController ([ref]$MainSanController) -ex ([ref]$MainException)

if ($MainSanController -eq $null) {

    write-host $MainException

    #QuitScript -reason SANERROR

} else {

    Write-Host $MainSanController.GetType()

}

Thanks for a good Friday afternoon brain-teaser!

timothyn
2,704 Views

Also... be careful statically typing DataONTAP PowerShell Toolkit objects.  We reserve the right to change them, and often do as new DataONTAP releases come out.

Public