Last week figured out how to script vSphere related stuff with Python. This week it is time for something new… how can we “script” vSphere stuff using PowerShell and how can we script NSX related stuff in particular.
So this is a list of steps (if you are really getting started) that you need to take to get started!
- Install Windows
- Install vSphere PowerShell
- Install the DEVELOPMENT VERSION of NSX PowerShell add-on (manually)
- Connect to vCenter Server (trough PowerShell)
- Connect to NSX Manager (trough PowerShell)
- View available NSX PowerShell commands
- Verify if connection is working correctly with some “Get Commands”
Install Windows
In order to work with PowerShell this one is pretty obvious I guess … but as this is like a “beginners” guide I have to mention it. Once you installed Windows (I tested it on Windows 2012 R2) the you will have access to the PowerShell command Line.
Install vSphere PowerShell
Before I stared I tried to retrieve some information that was already available on the internet an I found this useful blog post.
Now before you can “use” vSphere PowerShell you need to install it first.
The latest version can be downloaded here. The installation is fairly simple its just click Next, Next and Next and accept the license agreement that nobody ever reads and you have installed vSphere PowerShell. From now on you can do “things” with your current vSphere environment. For now we are not going to go deeper into this because our goal is to do “things” with our NSX environment.
Install the DEVELOPMENT VERSION of NSX PowerShell add–on 〈manually〉
You probably noticed the title said “Install the DEVELOPMENT VERSION of NSX PowerShell add-on”. This is because there is also a RELEASED version available here.
The reason why I specifically want the DEVELOPMENT version is because this DEVELOPMENT version supports more commands so you can do more then the RELEASED version. The DEVELOPMENT version can be downloaded here.
Some useful links in the process of installing NSX PowerShell where this and this link.
Because I was too fast and dod not READ properly I initially installed the “automatic” NSX PowerShell version which installs the RELEASE version. And I needed to have the DEVELOPMENT version that has more capabilities.
So I download the powernsx.psm1 file (development one) here. I actually just opened the RAW version and copy/pasted the tekst into Notepad++ and saved it as “powernsx.psm1” and saved it in the C:\ root. Because I still have not figured out how this GitHub stuff works… And I needed to uninstall the RELEASED version and install the DEVELOPMENT version.
PowerCLI C:\> remove-module powernsxPowerCLI C:\> import-module powernsx.psm1When you IMPORT the module make sure you are “sitting” in the same directory as where you are installing it from. My powernsx.psm1 file / module was just in C:\ (the root).
Connect to vCenter Server 〈trough PowerShell〉
First we need to connect to the vCenter Server and when we do this the script asks to put in a username / password with enough rights to perform the needed actions. We use the vCenter Server admin credentials for this.
PowerCLI C:\> Connect-VIServer
cmdlet Connect-VIServer at command pipeline position 1Supply values for the following parameters:Server[0]: dc1-pod11-vcsa-01Server[1]:WARNING: There were one or more problems with the server certificate for theserver dc1-pod11-vcsa-01:443:
* The X509 chain could not be built up to the root certificate.
* The certificate's CN name does not match the passed value.
Certificate: [Subject] C=US, CN=dc1-pod11-vcsa-01.pod11.local
[Issuer] OU=VMware, O=dc1-pod11-vcsa-01.pod11.local, S=California, C=US, DC=local,DC=pod11, CN=CA
[Serial Number] 00C2E3BF920EF358D1
[Not Before] 6/16/2016 9:06:16 AM
[Not After] 6/11/2026 9:06:15 AM
[Thumbprint] B6674ACB4EA7DE528B410977193EF5153F9B2A72
The server certificate is not valid.
WARNING: THE DEFAULT BEHAVIOR UPON INVALID SERVER CERTIFICATE WILL CHANGE IN AFUTURE RELEASE. To ensure scripts are not affected by the change, useSet-PowerCLIConfiguration to set a value for the InvalidCertificateActionoption.
Name Port User---- ---- ----dc1-pod11-vcsa-01 443 POD11.LOCAL\Administrator
PowerCLI C:\>Connect to NSX Manager 〈trough PowerShell〉
Then we need to connect to the NSX Manager and when we do this the script asks to put in a username / password with enough rights to perform the needed actions. We use the NSX Manager admin credentials for this.
PowerCLI C:\> Connect-NsxServer
cmdlet Connect-NsxServer at command pipeline position 1Supply values for the following parameters:Server: 192.11.220.110Credential The property 'VersionInfo' cannot be found on this object. Verify that the property exists. At C:\PowerNSX.psm1:2874 char:81 + ... ion" -value "$($response.VersionInfo.majorVersion).$($response.VersionInfo.minor ... + 22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)[[User:Iwan Hoogendoorn|Iwan Hoogendoorn]] ([[User talk:Iwan Hoogendoorn|talk]]) 22:06, 31 July 2016 (CEST) + CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException + FullyQualifiedErrorId : PropertyNotFoundStrict
The property 'VersionInfo' cannot be found on this object. Verify that the property exists. At C:\PowerNSX.psm1:2874 char:119 + ... ajorVersion).$($response.VersionInfo.minorVersion).$($response.VersionInf o.patch ... + 22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)[[User:Iwan Hoogendoorn|Iwan Hoogendoorn]] ([[User talk:Iwan Hoogendoorn|talk]]) 22:06, 31 July 2016 (CEST) + CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException + FullyQualifiedErrorId : PropertyNotFoundStrict
The property 'VersionInfo' cannot be found on this object. Verify that the property exists. At C:\PowerNSX.psm1:2874 char:157 + ... inorVersion).$($response.VersionInfo.patchVersion)" -force + 22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)[[User:Iwan Hoogendoorn|Iwan Hoogendoorn]] ([[User talk:Iwan Hoogendoorn|talk]]) 22:06, 31 July 2016 (CEST) + CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException + FullyQualifiedErrorId : PropertyNotFoundStrict
The property 'VersionInfo' cannot be found on this object. Verify that the property exists. At C:\PowerNSX.psm1:2875 char:85 + ... ber" -value "$($response.VersionInfo.BuildNumber)" + 22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)22:06, 31 July 2016 (CEST)[[User:Iwan Hoogendoorn|Iwan Hoogendoorn]] ([[User talk:Iwan Hoogendoorn|talk]]) + CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException + FullyQualifiedErrorId : PropertyNotFoundStrict
Using existing PowerCLI connection to 192.11.220.100
Version : ..BuildNumber :Credential : System.Management.Automation.PSCredentialServer : 192.11.220.110Port : 443Protocol : httpsValidateCertificate : FalseVIConnection : dc1-pod11-vcsa-01.pod11.localDebugLogging : FalseDebugLogFile : C:\Users\ADMINI~1\AppData\Local\Temp\PowerNSXLog-admin@19 2.11.220.110-2016_07_28_04_01_02.logI am getting some errors in the above output and this is because I was connecting to an NSX Manager version 6.2.3 and apparently this is not a fully supported version… but hey it eventually worked in the end (even with these errors).
View available NSX PowerShell commands
Now we have connected to both the vCenter Server and the NSX Manager we can start looking at the list of “available” commands that we can use with PowerNSX.
PowerCLI C:\> Get-Command -Module PowerNSX
CommandType Name ModuleName---- ---- ----------Function Add-NsxEdgeInterfaceAddress PowerNSXFunction Add-NsxLoadBalancerPoolMember PowerNSXFunction Add-NsxLoadBalancerVip PowerNSXFunction Add-NsxSecurityGroupMember PowerNSXFunction Add-XmlElement PowerNSXFunction Check-PowerCliAsemblies PowerNSXFunction Clear-NsxEdgeInterface PowerNSXFunction Connect-NsxServer PowerNSXFunction Disconnect-NsxServer PowerNSXFunction Format-XML PowerNSXFunction Get-FeatureStatus PowerNSXFunction Get-NsxBackingDVSwitch PowerNSXFunction Get-NsxBackingPortGroup PowerNSXFunction Get-NsxCliDfwAddrSet PowerNSXFunction Get-NsxCliDfwFilter PowerNSXFunction Get-NsxCliDfwRule PowerNSXFunction Get-NsxClusterStatus PowerNSXFunction Get-NsxController PowerNSXFunction Get-NsxEdge PowerNSXFunction Get-NsxEdgeBgp PowerNSXFunction Get-NsxEdgeBgpNeighbour PowerNSXFunction Get-NsxEdgeCertificate PowerNSXFunction Get-NsxEdgeCsr PowerNSXFunction Get-NsxEdgeInterface PowerNSXFunction Get-NsxEdgeInterfaceAddress PowerNSXFunction Get-NsxEdgeNat PowerNSXFunction Get-NsxEdgeNatRule PowerNSXFunction Get-NsxEdgeOspf PowerNSXFunction Get-NsxEdgeOspfArea PowerNSXFunction Get-NsxEdgeOspfInterface PowerNSXFunction Get-NsxEdgePrefix PowerNSXFunction Get-NsxEdgeRedistributionRule PowerNSXFunction Get-NsxEdgeRouting PowerNSXFunction Get-NsxEdgeStaticRoute PowerNSXFunction Get-NsxEdgeSubInterface PowerNSXFunction Get-NsxFirewallRule PowerNSXFunction Get-NsxFirewallSection PowerNSXFunction Get-NsxHostUvsmLogging PowerNSXFunction Get-NsxIpPool PowerNSXFunction Get-NsxIpSet PowerNSXFunction Get-NsxLoadBalancer PowerNSXFunction Get-NsxLoadBalancerApplicationProfile PowerNSXFunction Get-NsxLoadBalancerMonitor PowerNSXFunction Get-NsxLoadBalancerPool PowerNSXFunction Get-NsxLoadBalancerPoolMember PowerNSXFunction Get-NsxLoadBalancerVip PowerNSXFunction Get-NsxLogicalRouter PowerNSXFunction Get-NsxLogicalRouterBgp PowerNSXFunction Get-NsxLogicalRouterBgpNeighbour PowerNSXFunction Get-NsxLogicalRouterInterface PowerNSXFunction Get-NsxLogicalRouterOspf PowerNSXFunction Get-NsxLogicalRouterOspfArea PowerNSXFunction Get-NsxLogicalRouterOspfInterface PowerNSXFunction Get-NsxLogicalRouterPrefix PowerNSXFunction Get-NsxLogicalRouterRedistributionRule PowerNSXFunction Get-NsxLogicalRouterRouting PowerNSXFunction Get-NsxLogicalRouterStaticRoute PowerNSXFunction Get-NsxLogicalSwitch PowerNSXFunction Get-NsxMacSet PowerNSXFunction Get-NsxSecurityGroup PowerNSXFunction Get-NsxSecurityGroupEffectiveMembers PowerNSXFunction Get-NsxSecurityPolicy PowerNSXFunction Get-NsxSecurityTag PowerNSXFunction Get-NsxSecurityTagAssignment PowerNSXFunction Get-NsxSegmentIdRange PowerNSXFunction Get-NsxService PowerNSXFunction Get-NsxSpoofguardNic PowerNSXFunction Get-NsxSpoofguardPolicy PowerNSXFunction Get-NsxSslVpn PowerNSXFunction Get-NsxSslVpnAuthServer PowerNSXFunction Get-NsxSslVpnClientInstallationPackage PowerNSXFunction Get-NsxSslVpnIpPool PowerNSXFunction Get-NsxSslVpnPrivateNetwork PowerNSXFunction Get-NsxSslVpnUser PowerNSXFunction Get-NsxTransportZone PowerNSXFunction Get-NsxVdsContext PowerNSXFunction Get-PowerNsxVersion PowerNSXFunction Grant-NsxSpoofguardNicApproval PowerNSXFunction Install-NsxCluster PowerNSXFunction Invoke-NsxCli PowerNSXFunction Invoke-NsxRestMethod PowerNSXFunction Invoke-NsxWebRequest PowerNSXFunction New-NsxAddressSpec PowerNSXFunction New-NsxAppliedToListNode PowerNSXFunction New-NsxClusterVxlanConfig PowerNSXFunction New-NsxController PowerNSXFunction New-NsxEdge PowerNSXFunction New-NsxEdgeBgpNeighbour PowerNSXFunction New-NsxEdgeCsr PowerNSXFunction New-NsxEdgeInterfaceSpec PowerNSXFunction New-NsxEdgeNatRule PowerNSXFunction New-NsxEdgeOspfArea PowerNSXFunction New-NsxEdgeOspfInterface PowerNSXFunction New-NsxEdgePrefix PowerNSXFunction New-NsxEdgeRedistributionRule PowerNSXFunction New-NsxEdgeSelfSignedCertificate PowerNSXFunction New-NsxEdgeStaticRoute PowerNSXFunction New-NsxEdgeSubInterface PowerNSXFunction New-NsxEdgeSubInterfaceSpec PowerNSXFunction New-NsxFirewallRule PowerNSXFunction New-NsxFirewallSection PowerNSXFunction New-NsxIpPool PowerNSXFunction New-NsxIpSet PowerNSXFunction New-NsxLoadBalancerApplicationProfile PowerNSXFunction New-NsxLoadBalancerMemberSpec PowerNSXFunction New-NsxLoadBalancerMonitor PowerNSXFunction New-NsxLoadBalancerPool PowerNSXFunction New-NsxLogicalRouter PowerNSXFunction New-NsxLogicalRouterBgpNeighbour PowerNSXFunction New-NsxLogicalRouterInterface PowerNSXFunction New-NsxLogicalRouterInterfaceSpec PowerNSXFunction New-NsxLogicalRouterOspfArea PowerNSXFunction New-NsxLogicalRouterOspfInterface PowerNSXFunction New-NsxLogicalRouterPrefix PowerNSXFunction New-NsxLogicalRouterRedistributionRule PowerNSXFunction New-NsxLogicalRouterStaticRoute PowerNSXFunction New-NsxLogicalSwitch PowerNSXFunction New-NsxMacSet PowerNSXFunction New-NsxManager PowerNSXFunction New-NsxSecurityGroup PowerNSXFunction New-NsxSecurityTag PowerNSXFunction New-NsxSecurityTagAssignment PowerNSXFunction New-NsxSegmentIdRange PowerNSXFunction New-NsxService PowerNSXFunction New-NsxSourceDestNode PowerNSXFunction New-NsxSpoofguardPolicy PowerNSXFunction New-NsxSslVpnAuthServer PowerNSXFunction New-NsxSslVpnClientInstallationPackage PowerNSXFunction New-NsxSslVpnIpPool PowerNSXFunction New-NsxSslVpnPrivateNetwork PowerNSXFunction New-NsxSslVpnUser PowerNSXFunction New-NsxTransportZone PowerNSXFunction New-NsxVdsContext PowerNSXFunction Parse-CentralCliResponse PowerNSXFunction PrivateAdd-NsxEdgeVnicAddressGroup PowerNSXFunction Publish-NsxSpoofguardPolicy PowerNSXFunction Redeploy-NsxEdge PowerNSXFunction Remove-NsxCluster PowerNSXFunction Remove-NsxClusterVxlanConfig PowerNSXFunction Remove-NsxEdge PowerNSXFunction Remove-NsxEdgeBgpNeighbour PowerNSXFunction Remove-NsxEdgeCertificate PowerNSXFunction Remove-NsxEdgeCsr PowerNSXFunction Remove-NsxEdgeInterfaceAddress PowerNSXFunction Remove-NsxEdgeNatRule PowerNSXFunction Remove-NsxEdgeOspfArea PowerNSXFunction Remove-NsxEdgeOspfInterface PowerNSXFunction Remove-NsxEdgePrefix PowerNSXFunction Remove-NsxEdgeRedistributionRule PowerNSXFunction Remove-NsxEdgeStaticRoute PowerNSXFunction Remove-NsxEdgeSubInterface PowerNSXFunction Remove-NsxFirewallRule PowerNSXFunction Remove-NsxFirewallSection PowerNSXFunction Remove-NsxIpSet PowerNSXFunction Remove-NsxLoadBalancerApplicationProfile PowerNSXFunction Remove-NsxLoadBalancerMonitor PowerNSXFunction Remove-NsxLoadBalancerPool PowerNSXFunction Remove-NsxLoadBalancerPoolMember PowerNSXFunction Remove-NsxLoadBalancerVip PowerNSXFunction Remove-NsxLogicalRouter PowerNSXFunction Remove-NsxLogicalRouterBgpNeighbour PowerNSXFunction Remove-NsxLogicalRouterInterface PowerNSXFunction Remove-NsxLogicalRouterOspfArea PowerNSXFunction Remove-NsxLogicalRouterOspfInterface PowerNSXFunction Remove-NsxLogicalRouterPrefix PowerNSXFunction Remove-NsxLogicalRouterRedistributionRule PowerNSXFunction Remove-NsxLogicalRouterStaticRoute PowerNSXFunction Remove-NsxLogicalSwitch PowerNSXFunction Remove-NsxMacSet PowerNSXFunction Remove-NsxSecurityGroup PowerNSXFunction Remove-NsxSecurityGroupMember PowerNSXFunction Remove-NsxSecurityPolicy PowerNSXFunction Remove-NsxSecurityTag PowerNSXFunction Remove-NsxSecurityTagAssignment PowerNSXFunction Remove-NsxSegmentIdRange PowerNSXFunction Remove-NsxService PowerNSXFunction Remove-NsxSpoofguardPolicy PowerNSXFunction Remove-NsxSslVpnClientInstallationPackage PowerNSXFunction Remove-NsxSslVpnIpPool PowerNSXFunction Remove-NsxSslVpnPrivateNetwork PowerNSXFunction Remove-NsxSslVpnUser PowerNSXFunction Remove-NsxTransportZone PowerNSXFunction Remove-NsxVdsContext PowerNSXFunction Revoke-NsxSpoofguardNicApproval PowerNSXFunction Set-NsxEdge PowerNSXFunction Set-NsxEdgeBgp PowerNSXFunction Set-NsxEdgeInterface PowerNSXFunction Set-NsxEdgeNat PowerNSXFunction Set-NsxEdgeOspf PowerNSXFunction Set-NsxEdgeRouting PowerNSXFunction Set-NsxHostUvsmLogging PowerNSXFunction Set-NsxLoadBalancer PowerNSXFunction Set-NsxLogicalRouterBgp PowerNSXFunction Set-NsxLogicalRouterInterface PowerNSXFunction Set-NsxLogicalRouterOspf PowerNSXFunction Set-NsxLogicalRouterRouting PowerNSXFunction Set-NsxManager PowerNSXFunction Set-NsxSslVpn PowerNSXFunction Test-WebServerSSL PowerNSXFunction Update-PowerNsx PowerNSXFunction Validate-AddressGroupSpec PowerNSXFunction Validate-DistributedSwitch PowerNSXFunction Validate-Edge PowerNSXFunction Validate-EdgeBgpNeighbour PowerNSXFunction Validate-EdgeCertificate PowerNSXFunction Validate-EdgeCsr PowerNSXFunction Validate-EdgeInterface PowerNSXFunction Validate-EdgeInterfaceAddress PowerNSXFunction Validate-EdgeInterfaceSpec PowerNSXFunction Validate-EdgeNat PowerNSXFunction Validate-EdgeNatRule PowerNSXFunction Validate-EdgeOspfArea PowerNSXFunction Validate-EdgeOspfInterface PowerNSXFunction Validate-EdgePrefix PowerNSXFunction Validate-EdgeRedistributionRule PowerNSXFunction Validate-EdgeRouting PowerNSXFunction Validate-EdgeSslVpn PowerNSXFunction Validate-EdgeSslVpnClientPackage PowerNSXFunction Validate-EdgeSslVpnIpPool PowerNSXFunction Validate-EdgeSslVpnPrivateNetwork PowerNSXFunction Validate-EdgeSslVpnUser PowerNSXFunction Validate-EdgeStaticRoute PowerNSXFunction Validate-EdgeSubInterface PowerNSXFunction Validate-FirewallAppliedTo PowerNSXFunction Validate-FirewallRuleSourceDest PowerNSXFunction Validate-IpPool PowerNSXFunction Validate-LoadBalancer PowerNSXFunction Validate-LoadBalancerApplicationProfile PowerNSXFunction Validate-LoadBalancerMemberSpec PowerNSXFunction Validate-LoadBalancerMonitor PowerNSXFunction Validate-LoadBalancerPool PowerNSXFunction Validate-LoadBalancerPoolMember PowerNSXFunction Validate-LoadBalancerVip PowerNSXFunction Validate-LogicalRouter PowerNSXFunction Validate-LogicalRouterBgpNeighbour PowerNSXFunction Validate-LogicalRouterInterface PowerNSXFunction Validate-LogicalRouterInterfaceSpec PowerNSXFunction Validate-LogicalRouterOspfArea PowerNSXFunction Validate-LogicalRouterOspfInterface PowerNSXFunction Validate-LogicalRouterPrefix PowerNSXFunction Validate-LogicalRouterRedistributionRule PowerNSXFunction Validate-LogicalRouterRouting PowerNSXFunction Validate-LogicalRouterStaticRoute PowerNSXFunction Validate-LogicalSwitch PowerNSXFunction Validate-LogicalSwitchOrDistributedPortGroup PowerNSXFunction Validate-LogicalSwitchOrDistributedPortGroupOrS... PowerNSXFunction Validate-SecurityGroupMember PowerNSXFunction Validate-SecurityTag PowerNSXFunction Validate-SegmentIdRange PowerNSXFunction Validate-Service PowerNSXFunction Validate-SpoofguardNic PowerNSXFunction Validate-SpoofguardPolicy PowerNSXFunction Validate-TagAssignment PowerNSXFunction Validate-TransportZone PowerNSXFunction Validate-VdsContext PowerNSXFunction Validate-VirtualMachine PowerNSXFunction Where-NsxVMUsed PowerNSX
PowerCLI C:\>Wow! There is a lot that you can verify configure and even delete right?
Verify if connection is working correctly with some Get-Commands
I have issued two “test” commands that you can use to see if it is really working…
First we verify the PowerNsxVersion / Module that I have installed (manually).
PowerCLI C:\> Get-PowerNsxVersion
Version Path Author CompanyName---- ---- ------ -----------0.0 C:\PowerNSX.psm1Then we verify ALL my NSX Edge Services Gateways that I have deployed so far.
PowerCLI C:\> Get-NsxEdge
id : edge-1version : 5status : deployeddatacenterMoid : datacenter-2datacenterName : DC1 - POD11tenant : defaultname : dc1-pod11-esg-01fqdn : dc1-pod11-esg-01enableAesni : trueenableFips : falsevseLogLevel : emergencyvnics : vnicsappliances : appliancescliSettings : cliSettingsfeatures : featuresautoConfiguration : autoConfigurationtype : gatewayServicesisUniversal : falsehypervisorAssist : falsequeryDaemon : queryDaemonedgeSummary : edgeSummaryOriginally published on the old wiki at /index.php/Getting_started_with_vSphere_PowerShell_PowerCLI_and_PowerNSX_–_PowerShell_extensions_for_NSX.