I am having an issue in many production scripts that are taking advantage of the 'Connect-NcController -Transient:$true' functionality to not set the global variable when connecting to a controller, and instead assign that connection to my own custom variable.
It all works fine, except the first time I try to do ANYTHING with that connection it will fail with a generic error coming from the module commandlets. The same action will work fine the second and every other time after that first failure.
Get-NcAggr : Object reference not set to an instance of an object.
At line:35 char:27
+ $ncAggr = Get-NcAggr -Controller $ncController
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-NcAggr], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,DataONTAP.C.PowerShell.SDK.Cmdlets.Aggr.GetNcAggr
Here is a simple script to reproduce the issue:
# Set this to $true to use the $global:CurrentNcController in the Get-NcAggr command
# Set to $false to use the controller stored in a variable as an object $ncController
$useNcGlobal = $false
# update to your actual controller
$controllerIP = "xxx.xxx.xxx.xxx"
Import-Module NetApp.ONTAP
$global:CurrentNcController = $null
try {
$ncCredential = Get-Credential -Message "Add Admin password" -UserName admin
if ( -not ($useNcGlobal) ) {
# Store controller in variable and use transient to NOT set the global
$ncController = Connect-NcController -Name $controllerIP -Credential $ncCredential -Transient:$true -ErrorAction Stop
Write-Host "Controller varaible '`$ncController' type is '$($nccontroller.GetType())'"
}
else {
# Store controller in the global
Connect-NcController -Name $controllerIP -Credential $ncCredential -ErrorAction Stop
Write-Host "Controller varaible '`$global:CurrentNcController' type is '$($global:CurrentNcController.GetType())'"
}
[int]$i = 0
$ints = 1..10
foreach ($int in $ints) {
try {
$i++
if ( $nccontroller ) {
$ncAggr = Get-NcAggr -Controller $ncController
}
else {
$ncAggr = Get-NcAggr
}
Write-Host "This time, '$i' and subsequent times will always succeed"
if ( $int -eq $($ints | Select-Object -Last 1) ) {
$ncAggr
}
}
catch {
$error[0]
Write-Host "This time, '$i' always fails when using -controller and passing in a controller variable object"
}
}
}
catch {
$error[0]
Write-Host "Uncaught error"
}