Forum Discussion

dan_03_140075's avatar
dan_03_140075
Icon for Nimbostratus rankNimbostratus
Apr 03, 2014

Passing credential to New-Object iControl.Interfaces

Hello, big fan of powershell and icontrol. The F5 DevCentral has been awesome to get up and running. I have been using $cred = (get-credential) Initialize-F5.iControl -HostName $g_bigip -credentials $cred;

 

To do my authentication and init. Problem is I dont know how to disconnect that init? I found this topic 9https://devcentral.f5.com/questions/how-to-disconnect-icontrol-session) and asked my question there too. Its old so maybe things have changed. From there I was able to use $ic = New-Object iControl.Interfaces $ic.initialize("hostname", "username", "password"); But dont know if get-credentials and variable can be used here? I have tried $cred = (get-credential) $uid=$cred.username $pw=$cred.password $bigip="lsg-cag-uat1" $ic.initialize($bigip,$uid,$pw) and $ic.initialize("$bigip","$uid","$pw") getting error in both cases You cannot call a method on a null-valued expression. At line:1 char:1 + $ic.initialize("$bigip","$uid","$pw") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

 

Thanks

 

4 Replies

  • Been away from that code for a while now. I'll pull it up and give it a look for the specific calls to initialize. May I ask why you are attempting to do an de-init? All of the iControl calls are separate HTTP requests. All the init does is to store the credentials in the iControl.Interfaces object for future method calls. If you want to reuse the object, you can just call initialize again and give it a new set of credentials. Or, you could assign the object the value of $null. I'll check the code for the initialize and get back to you...
  • I think your problem is with the Get-Credential call. That will return a PSCredential object with UserName (String) and Password (SecureString) members. You are treating the SecureString as a regular string when passing that in. Odds are the error occurs there. Try converting the password into a clear text string before calling initialize.

    $creds = Get-Credential;
    $user = $creds.Username;
    $pass = $creds.GetNetworkCredential().Password;
    

    Here's an article on dissecting the SecureString

    http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/26/decrypt-powershell-secure-string-password.aspx

    Hope this helps...

    -Joe

  • $user = $cred.Username; $pass = $cred.GetNetworkCredential().Password; $g_bigip = "dev1" $ic = New-Object iControl.Interfaces $ic.initialize($g_bigip, $user, $pass); True $ic.SystemSystemInfo.get_uptime(); 4475412 $ic = $null $ic.SystemSystemInfo.get_uptime(); You cannot call a method on a null-valued expression. At line:1 char:1 + $ic.SystemSystemInfo.get_uptime(); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull $cred = (get-credential) $global:g_bigip = "dev1" $global:success = Initialize-F5.iControl -HostName $global:g_bigip -credentials $cred; (Get-F5.iControl).SystemSystemInfo.get_uptime(); 4475650 $global:success = $null (Get-F5.iControl).SystemSystemInfo.get_uptime(); 4475663
  • Its possible to run an icontrol command on the wrong bigip. Think its pretty easy to forget which bigip you are connected to. I have developed a menu driven script that actually helps to mitigate this to a large degree and there are other methods (change ps prompt to reflect bigip name?), but I would like to use the menu system and once a actions/tasks is completed, disconnect. Go back to menu selections and re-init as need be. At the mo my script does this and its only if you break out of it, that you have an "unknown" open init to a bigip still...