Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

The variable '$defaultNSXConnection' cannot be retrieved because it has not been set #616

Open
dcvlehr opened this issue Feb 4, 2020 · 15 comments

Comments

@dcvlehr
Copy link

dcvlehr commented Feb 4, 2020

When attempting to connect to the NSX manager, I get this error.

The variable '$defaultNSXConnection' cannot be retrieved because it has not been set.
At C:\Program Files\WindowsPowerShell\Modules\PowerNSX\3.0.1174\PowerNSX.psm1:25305 char:41

  •         [PSCustomObject]$Connection=$defaultNSXConnection
    
  •                                     ~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (defaultNSXConnection:String) [], RuntimeException
    • FullyQualifiedErrorId : VariableIsUndefined
@alagoutte
Copy link
Contributor

What command do you are using ?

@dcvlehr
Copy link
Author

dcvlehr commented Feb 5, 2020

Connect-NsxServer -vCenterServer ServerName.com

I've tried by IP address as well. Docs said -vCenterServer was prefered. I also have the powercli module installed, and the connect-NsxServer cmdlet will also try to use an existing vSphere connection and automatically attempts to hit the correct NSX manager IP without ever referencing it

@alagoutte
Copy link
Contributor

Strange
what release of PowerShell ? do you have try a older release ?

@dcvlehr
Copy link
Author

dcvlehr commented Feb 5, 2020

Name                           Value                                                                                                                           
----                           -----                                                                                                                           
PSVersion                      5.1.17134.858                                                                                                                   
PSEdition                      Desktop                                                                                                                         
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                         
BuildVersion                   10.0.17134.858                                                                                                                  
CLRVersion                     4.0.30319.42000                                                                                                                 
WSManStackVersion              3.0                                                                                                                             
PSRemotingProtocolVersion      2.3                                                                                                                             
SerializationVersion           1.1.0.1 

@dcvlehr
Copy link
Author

dcvlehr commented Feb 5, 2020

Now I'm getting this error after closing my powershell ISE and reopening and trying again. Strange, because I connect to Vsphere just fine and get run Get-VM.

Connection to NSX server 10.x.x.x failed : Invoke-NsxRestMethod : The NSX API response received indicates a failure. 403 : Forbidden : Response Body: 
At C:\Program Files\WindowsPowerShell\Modules\PowerNSX\3.0.1174\PowerNSX.psm1:4939 char:13
+             Throw "Connection to NSX server $NsxServer failed : $_"
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (Connection to N...Response Body: :String) [], RuntimeException
    + FullyQualifiedErrorId : Connection to NSX server 10.x.x.x failed : Invoke-NsxRestMethod : The NSX API response received indicates a failure. 403 : 
    Forbidden : Response Body: 

None of this makes sense because i can do postman API calls with my same credentials to this nsx manager and return things.

@alagoutte
Copy link
Contributor

What NSX Release ?

you try to connect directly to NSX Manager ?

@dcvlehr
Copy link
Author

dcvlehr commented Feb 6, 2020

Version    Name                                Repository           Description                                                                                
-------    ----                                ----------           -----------                                                                                
3.0.1174   PowerNSX                            PSGallery            PowerNSX is a PowerShell module that abstracts the VMware NSX API to a set of easily use...

I've tried Connect-NsxServer -NsxServer 10.X.X.X -Credential $cred

Still won't work.

@dcvlehr
Copy link
Author

dcvlehr commented Feb 6, 2020

I am now able, without PowerNSX at all, to connect to my NSX manager via pure powershell invoke-restmethod

#logon credentials
$username = 'me'
$password = Get-Content "C:\Users\myuser\Desktop\vcenterpassword.txt" | ConvertTo-SecureString
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password

#format credentials
$auth = [System.Convert]::ToBase64String([System.Text.ASCIIEncoding]::ASCII.GetBytes($Credential.UserName+':'+$Credential.GetNetworkCredential().Password))
$head = @{'Authorization' = "Basic $auth"}

#API URI to conenct to
$URI = "https://10.x.x.x/api/2.0/services/securitytags/tag/securitytag-13"

#send API call
$request = Invoke-RestMethod -Uri $URI -Method Get -Headers $head
$request

This returns a security tag just as it did when using Postman. Still have no idea why powerNSX doesn't work. Was really hoping to abstract some of this.

@alagoutte
Copy link
Contributor

Version    Name                                Repository           Description                                                                                
-------    ----                                ----------           -----------                                                                                
3.0.1174   PowerNSX                            PSGallery            PowerNSX is a PowerShell module that abstracts the VMware NSX API to a set of easily use...

I've tried Connect-NsxServer -NsxServer 10.X.X.X -Credential $cred

Still won't work.

i ask the NSX Manager release

@alagoutte
Copy link
Contributor

and for your issue about SSL/TLS it is coming from need to disable certificate check (it is set automatically when launch connect-nsx...)

@dcvlehr
Copy link
Author

dcvlehr commented Feb 13, 2020

NSX Manager Version: | 6.4.5 Build 13282012

I can get around the TLS issue easy enough. Just wish I could actually use powernsx >.<

@alagoutte
Copy link
Contributor

You are using the vCenter name when connect ? (and the administrator@vsphere.local) account ? (not the NSX Manager)

@dcvlehr
Copy link
Author

dcvlehr commented Feb 13, 2020

So that worked with those credentials...how am I able to make any API call I want outside of powernsx with my admin credentials, but using powernsx requires this specific account?

@alagoutte
Copy link
Contributor

you can the use admin account of NSX Manager ? or a another account on vSphere ?

@dcvlehr
Copy link
Author

dcvlehr commented Feb 14, 2020

I have been making API calls to NSX directly with my personal admin account. I was finally able to do it with the power nsx module using the adminstrator@vsphere.local

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants