Microsoft Virtualization Discussions

How to Capture Authentication Successes and Failures - PowerShell

JAL
8,773 Views

All,

I wrote a PS script to create a NetApp LUN based on a Volume snapshot and, it works great.  That is, if you authenticate successfully.

Here is the line of code to attach to a filer:

                Connect-NaController $Filer -credential (Get-Credential) -HTTPS

 

What I realized during testing is, even if you give credentials to the Windows Popup that appears, the script will continiue to run, even though is does not accomplish anything based on the authentication failure.  What I'd like to do, is have a way to capture both successful and unsuccessful authentication attempts and use a Function to either send a message to the user that their attempt failed or, continue to run the script since the logon attempt was good.

 

Any help will be greatly appreciated.

Thanks,

 

-joe

1 ACCEPTED SOLUTION

Shashanka
8,532 Views

Please use an error action to do what you expect to:

 

try
{
    Connect-NaController $Filer -credential (Get-Credential) -HTTPS -ErrorAction Stop
}

catch

{

     . . . . . 

}

 

You can also use an error variable to catch the error as an alternative :

 

Connect-NaController $Filer -credential (Get-Credential) -HTTPS -ErrorAction SilentlyContinue -ErrorVariable err

 

Now use $err  to print the error message

View solution in original post

5 REPLIES 5

asulliva
8,761 Views

Hi Joe,

 

The simplest way is to simply test for success on the connect command...

 

 

if (!Connect-NcController ...) {
    Write-Error "Unable to connect to controller!"
    exit
}

 

 

 

Aside from that, the two easiest ways are to use a try/catch block or test for the connection...

 

 

try {
    Connect-NcController ....
} catch {
    Throw "Unable to connect to controller!."
}
Connect-NcController ....

if (!$global:CurrentNcController) {
   Write-Error "No controller connection is present!"
   exit
}

Hope that helps!

 

Andrew

 

If this post resolved your issue, please help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

JAL
8,693 Views

Andrew, thanks for the response.  I'm hoping to get to this, this week.  I'll let you know how it works out.

 

-joe

JAL
8,546 Views

So, finally was able to get back to this script but, I'm still having issues trying to capture the error and how to deal with it.  Yea, I know I'm a rookie at PS.

It seems that since the PS script is making a request to an external source, I can't or, am having a problem capturing that error.  So, as in my first post, if I run this line:

 

            Connect-NaController $Filer -credential (Get-Credential) -HTTPS     And put in bad or wrong credentials, I get the following error.

 

      Connect-NaController : Incorrect credentials for NaFiler001.
At C:\Users\UserName\Downloads\PowerShell Stuff\Scripts\Tests\NA_Logon_Test.ps1:18 char:2
+     Connect-NaController $Filer -credential (Get-Credential) -HTTPS
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidResult: nafiler001:NaController) [Connect-NaController], NaAuthException
    + FullyQualifiedErrorId : HttpConnectionFailed,DataONTAP.PowerShell.SDK.ConnectNaController

 

I even tried this but, even with this next example, I get the exact error as above in Italic.

 

 try
{
    Connect-NaController $Filer -credential (Get-Credential) -HTTPS
}
catch
{
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        Write-Host "Authentication failed due to $ErrorMessage. The error was $FailedItem"
        Break
}

                   

 

I realize I'm missing something here but, hopefully someone can get me down the correct path.

Thanks,

 

Joe

 

Shashanka
8,533 Views

Please use an error action to do what you expect to:

 

try
{
    Connect-NaController $Filer -credential (Get-Credential) -HTTPS -ErrorAction Stop
}

catch

{

     . . . . . 

}

 

You can also use an error variable to catch the error as an alternative :

 

Connect-NaController $Filer -credential (Get-Credential) -HTTPS -ErrorAction SilentlyContinue -ErrorVariable err

 

Now use $err  to print the error message

JAL
8,516 Views

Shashanka,

Thanks for the reply. That was exactly what I needed and works perfect for what I was trying to achieve.

 

Joe

Public