vmware-archive / powernsx

PowerShell module that abstracts the VMware NSX-v API to a set of easily used PowerShell functions
173 stars 90 forks source link

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

Open dcvlehr opened 4 years ago

dcvlehr commented 4 years ago

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

alagoutte commented 4 years ago

What command do you are using ?

dcvlehr commented 4 years ago
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 commented 4 years ago

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

dcvlehr commented 4 years ago
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 commented 4 years ago

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 commented 4 years ago

What NSX Release ?

you try to connect directly to NSX Manager ?

dcvlehr commented 4 years ago
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 commented 4 years ago

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 commented 4 years ago
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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

dcvlehr commented 4 years ago

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 commented 4 years ago

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

dcvlehr commented 4 years ago

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