RSS

Author Archives: Kelvin Wong

PS: Get cluster CPU/Memory over-subscription

I am not the originator of these code (sorry I don’t remember where in google land they came from), but am posting it here for reference, since I may need to reuse it someday.

Note: CPU calculation is divided by 2 due to hyper-threading

#get cluster CPU over-subscription
Get-Cluster | Sort name | Select Name, @{N="CpuOversubscriptionFactor";E={[math]::Round((($_|get-VM|measure numcpu -sum).Sum)/(($_|get-vmhost|measure numcpu -sum).sum)/2,2)}}

#get cluster Memory over-subscription
Get-Cluster | Sort name | Select Name, @{N="MemOversubscriptionFactor";E={[math]::Round((($_|get-VM|measure MemoryGB-sum).Sum)/(($_|get-vmhost|measure MemoryTotalGB -sum).sum),2)}}

#get VMHost CPU over-subscription in a cluster
Get-Cluster <name> | Get-VMHost | Sort name | Select Name, @{N="CpuOversubscriptionFactor";E={[math]::Round((($_|get-VM|measure numcpu -sum).Sum)/(($_|measure numcpu -sum).sum)/2,2)}}
 
Leave a comment

Posted by on May 30, 2020 in powershell

 

PowerNSX: Exporting NSX Edge & Logical Router summary configuraiotn

This script is straight forward, it will extract the NSX Edge and Logical Router information into a CSV file.

Filename = Get-NSXAllEdgeSummary.ps1

<#
.SYNOPSIS  Exports NSX Edges & Logical Router Details

.DESCRIPTION Exports NSX Edges & Logical Router details of an existing connection to a CSV file. 
 Some multiple string values like static route are joined with "|" delimiter. So in CSV they appear as a single record
 You can manipulate this by converting "|" to "newline" in XLS for example to review your configuraiton consistency
 This was written for NSX-V 6.3/4 
  
.NOTES 
 1.0: Kelvin, 12 DEC 2019 : Base version

.PRE-REQUISITE
 - load PowerCLI & PowerNSX mode. 
   - In older version of PowerNSX Get-NSXEdge may also return logical router, so you will get duplicate values. 
   - Best to use the latest version of PowerNSX
 - Connect-NSX -vcenter <vcentername>

.PARAMETER
  CsvOut - full path csv file name if not using default

.OUTPUT
  CsvOut = contains NSX Edge interface in csv format, defaults to current folder ".\<vcentername>-allesgsummary.csv"

.EXAMPLE
 .\Get-NSXAllEdgeSummary.ps1
#>
param(
[Parameter(Mandatory=$false)]
[String]$CsvOut=".\$($DefaultNSXConnection.VIConnection)-allesgsummary.csv"
)

$r = @()
$NSXEdges = Get-NsxEdge
$NSXEdges += Get-NsxLogicalRouter
foreach ( $e in $NSXEdges) {
  
  $Edge = Get-NSXEdge -objectId $e.Id

  $StaticRoutes = @()
  $Edge.features.routing.staticRouting.staticRoutes.route |
  % { $StaticRoutes += "nw={0},nx={1},vn={2},mtu={3},ad={4}" -f $_.network,$_.nextHop,$_.vnic,$_.mtu,$_.adminDistance }

  $BGPredist = @()
  $Edge.features.routing.bgp.redistribution.rules.rule |
  % { $BGPredist += "o={0},b={1},s={2},c={3},ac={4}" -f $_.from.ospf,$_.from.bgp,$_.from.static,$_.from.connected,$_.action }

  $AppliancesHA = @()
  $Edge.appliances.appliance |
  % { $AppliancesHA += "{0},{1},{2},{3}" -f $_.vmHostname,$_.resourcePoolName,$_.datastoreName,$_.hostName }

  
      $t = [PSCustomObject]@{
        name                = $edge.name
        dataCenterName      = $Edge.dataCenterName
        id                  = $Edge.id
        enableAesni         = $Edge.enableAesni
        enableFips          = $Edge.enableFips
        vseLogLevel         = $Edge.vseLogLevel
        IsUniversal         = $Edge.IsUniversal
        dnsEnabled          = $Edge.features.dns.enabled
        dnsforwarders       = ($Edge.features.dns.dnsViews.dnsView.forwarders.ipAddress) -join "|"
        syslogEnabled       = $Edge.features.syslog.enabled
        syslogprotocol      = $Edge.features.syslog.protocol
        syslogipAddress     = ($Edge.features.syslog.serverAddresses.ipAddress) -join "|"
        highAvailability    = $Edge.features.highAvailability.enabled
        AppliancesHA        = $AppliancesHA -join "|"
        routerId            = $Edge.features.routing.routingGlobalConfig.routerId
        ecmp                = $Edge.features.routing.routingGlobalConfig.ecmp
        rtloggingenabled    = $Edge.features.routing.routingGlobalConfig.logging.enable
        rtlogginglogLevel   = $Edge.features.routing.routingGlobalConfig.logging.logLevel
        StaticRoute         = $StaticRoutes -join "|"
        ospfEnabled         = $Edge.features.routing.ospf.enabled
        BGPredistEnabled    = $Edge.features.routing.bgp.redistribution.enabled
        BGPlocalAS          = $Edge.features.routing.bgp.localAS
        BGPredistInfo       = $BGPredist -join "|"
        BGPGracefulRestart  = $Edge.features.routing.bgp.gracefulRestart
        BGPdefaultOriginate = $Edge.features.routing.bgp.defaultOriginate
        firewallEnabled     = $Edge.features.firewall.enabled
        edgeStatus          = $Edge.edgeSummary.edgeStatus
        localEgressEnabled  = $Edge.edgeSummary.localEgressEnabled
        applianceSize       = $Edge.appliances.applianceSize
      }
      $r += $t
}

$r | Export-Csv -NoTypeInformation $CsvOut
Write-Host Exported to $CsvOut
 
Leave a comment

Posted by on May 25, 2020 in PowerNSX

 

PowerNSX: Exporting NSX Edge BGP settings

I wrote this script as we needed to verify the consistencies of the NSX Edge bgp settings in our environment. This script is straight forward, it will extract the information into a CSV file.

Filename =Get-NSXAllEdgeSummary.ps1

<#
.SYNOPSIS  Exports NSX Edges BGP settings

.DESCRIPTION Exports NSX Edges BGO settings of an existing connection to a CSV file. 
 Some multiple string values like bgp filters are join with "|" delimiter. So in CSV they appear as a single record
 You can manipulate this by converting "|" to "newline" in XLS for example to review your configuraiton consistency
 This was written for NSX-V 6.3/4
  
.NOTES 
 1.0: Kelvin, 12 DEC 2019 : Base version

.PRE-REQUISITE
 - load PowerCLI & PowerNSX mode.
 - Connect-NSX -vcenter <vcentername>

.PARAMETER
  CsvOut - full path csv file name if not using default

.OUTPUT
  CsvOut = contains NSX Edge interface in csv format, defaults to current folder ".\<vcentername>-allesgbgpfilters.csv"

.EXAMPLE
 .\Get-Get-NSXAllEdgeBGPDetails.ps1
#>
param(
[Parameter(Mandatory=$false)]
[String]$CsvOut=".\$($DefaultNSXConnection.VIConnection)-allesgbgpfilters.csv"
)

$r = @()
$NSXEdges = Get-NsxEdge
$NSXEdges += Get-NsxLogicalRouter
foreach ( $edge in $NSXEdges) {
  
  foreach ($bgp in $edge.features.routing.bgp.bgpNeighbours.bgpNeighbour) {
  
    $bgpFilters = @()
    $bgp.bgpFilters.bgpFilter |
    % {if (![string]::IsNullOrEmpty($_.network)) {
      $bgpFilters += "nw={0},dir={1},act={2}" -f $_.network,$_.direction,$_.action
      } }
    
      $t = [PSCustomObject]@{
        name                   = $edge.name
        bgpEnabled             = $Edge.features.routing.bgp.enabled
        bgplocalAS             = $Edge.features.routing.bgp.localAS
        bgpnbIPAddress         = $bgp.ipAddress
        bgpnremoteAS           = $bgp.remoteAS
        bgpnbprotocolAddress   = $bgp.protocolAddress
        bgpnbforwardingAddress = $bgp.forwardingAddress
        bgpnweight             = $bgp.weight        
        bgpnholdDownTimer      = $bgp.holdDownTimer        
        bgpnkeepAliveTimer     = $bgp.keepAliveTimer
        bgpnFilters            = $bgpFilters -join "|"
      }
    $r += $t
  }
  
}

$r | Export-Csv -NoTypeInformation $CsvOut 
Write-Host Exported to $CsvOut 
 
Leave a comment

Posted by on May 25, 2020 in PowerNSX

 

PowerNSX: Exporting NSX Logical Switch Interface details

I wrote this script as we needed to verify the consistencies of the Logical Routers in our environment. This script is straight forward, it will extract the interface details of all NSX Logical Routers into a CSV file.

Filename = Get-NSXRouterInterfaceDetails.ps1

<#
.SYNOPSIS  Exports NSX Logical Switch configuration and interface details

.DESCRIPTION Exports NSX Logical Switch interface details of an existing connection to a CSV file. LR have a different interface struture to Edge,
 hence a separate script is required. This was written for NSX-V 6.3/4
  
.NOTES 
 1.0: Kelvin, 12 DEC 2019 : Base version

.PRE-REQUISITE
 - load PowerCLI & PowerNSX mode.
 - Connect-NSX -vcenter <vcentername>

.PARAMETER
  CsvOut - full path csv file name if not using default

.OUTPUT
  CsvOut = contains NSX Edge interface in csv format, defaults to current folder ".\<vcentername>-NSXRouterInterfaceDetails.csv"

.EXAMPLE
 .\Get-NSXEdgeInterfaceDetails.ps1
#>

param(
[Parameter(Mandatory=$false)]
[String]$CsvOut=".\$($DefaultNSXConnection.VIConnection)-routerifdetails.csv"
)

$r = @()
$NSXLogicalRouters = Get-NsxLogicalRouter
foreach ( $lrtr in $NSXLogicalRouters) {

  $Interfaces = $lrtr.interfaces.interface | ? {$_.isConnected}
  foreach ( $i in $interfaces) {

    foreach ($a in $i.addressGroups.addressGroup) {

      $t = [PSCustomObject]@{
        name                = $lrtr.name
        dataCenterName      = $lrtr.dataCenterName
        id                  = $lrtr.id
        IsUniversal         = $lrtr.IsUniversal
        routerId            = $lrtr.features.routing.routingGlobalConfig.routerId
        ecmp                = $lrtr.features.routing.routingGlobalConfig.ecmp
        if_label            = $i.label
        if_name             = $i.name
        if_type             = $i.type
        connectedToId       = $i.connectedToId
        connectedToName     = $i.connectedToName
        mtu                 = $i.mtu
        primaryAddress      = $a.primaryAddress
        subnetMask          = $a.subnetMask
        subnetPrefixLength  = $a.subnetPrefixLength
      }
      $r += $t
    }
  }
}

$r | Export-Csv -NoTypeInformation $CsvOut 
Write-Host Exported to $CsvOut 
 
Leave a comment

Posted by on May 25, 2020 in PowerNSX

 

PowerNSX: Exporting NSX Edge Interface details

I wrote this script as we needed to verify the consistencies of the Edges in our environment. This script is straight forward, it will extract the interface details of all NSX Edges (not Logical Routers) into a CSV file.

Filename = Get-NSXEdgeInterfaceDetails.ps1

<#
.SYNOPSIS  Exports NSX Edges Interface details

.DESCRIPTION Exports NSX Edges inteface details of an existing connection to a CSV file. 
  
.NOTES 
 1.0: Kelvin, 12 DEC 2019 : Base version

.PRE-REQUISITE
 - load PowerCLI & PowerNSX mode. Note that old PowerNSX may also export logic router with Get-NSXEdge call and you will get some empty fields because their interface structureis differenet
 - Connect-NSX -vcenter <vcentername>

.PARAMETER
  CsvOut - full path csv file name if not using default

.OUTPUT
  CsvOut = contains NSX Edge interface in csv format, defaults to current folder ".\<vcentername>-edgsifdetails.csv"

.EXAMPLE
 .\Get-NSXEdgeInterfaceDetails.ps1
#>
param(
[Parameter(Mandatory=$false)]
[String]$CsvOut=".\$($DefaultNSXConnection.VIConnection)-esgifdetails.csv"
)

$outfile = ".\$($DefaultNSXConnection.VIConnection)-esgifdetails.csv"
$r = @()
$NSXEdges = Get-NsxEdge
foreach ( $Edge in $NSXEdges) {

  $Interfaces = $Edge.vnics.vnic | ? {$_.isConnected}
  foreach ( $i in $interfaces) {

    foreach ($a in $i.addressGroups.addressGroup) {
      
      $t = [PSCustomObject]@{
        name                = $edge.name
        dataCenterName      = $Edge.dataCenterName
        id                  = $Edge.id
        enableAesni         = $Edge.enableAesni
        enableFips          = $Edge.enableFips
        vseLogLevel         = $Edge.vseLogLevel
        IsUniversal         = $Edge.IsUniversal
        routerId            = $Edge.features.routing.routingGlobalConfig.routerId
        ecmp                = $Edge.features.routing.routingGlobalConfig.ecmp
        if_label            = $i.label
        if_name             = $i.name
        if_type             = $i.type
        portgroupId         = $i.portgroupId
        portgroupName       = $i.portgroupName
        mtu                 = $i.mtu
        enableProxyArp      = $i.enableProxyArp
        enableSendRedirects = $i.enableSendRedirects
        primaryAddress      = $a.primaryAddress
        subnetMask          = $a.subnetMask
        subnetPrefixLength  = $a.subnetPrefixLength
      }
      $r += $t
    }
  }
}

$r | Export-Csv -NoTypeInformation $CsvOut
Write-Host Exported to $CsvOut
 
Leave a comment

Posted by on May 25, 2020 in PowerNSX

 

Vsphere_vchecklog

This set of scripts were developed using similar concept as vchecks but for a more real world enterprise usage. Its not that vcheck is not good, but when you are in a big company, having to read a vcheck email to check the status of daily is very tired. vChecks suffers from the following:

  • Lack of over RAG (red/amber/green) status on subject line. I agree that its hard to really determine what should be red or amber. But in an enterprise operations, RAG give a quick overview of your status. So if the subject says green, you really don’t need to read through the email. Having said that, this script doesn’t send any emails, more about this later.
  • You don’t know if a plugin was successfully executed or not. When a vcheck plugin executes, it either produces a select object or doesn’t. Not producing a select object could also mean that script did not execute. For enterprise operations, its essential to know if each health is executed and the status even if its all green.
  • If I need to run vchecks continuously, say every 30 mins or hour, it may not be achievable because in an enterprise setting, it may take a while to complete a full run of vchecks.

This is an alternate version of vCheck-like script written by me, but sending status outputs to a log file instead. This was developed as the use case for vCheck did not fit with a large enterprise operation organisation.

Git hub repo here -> https://github.com/kwongheng/vsphere_vchecklog

 
Leave a comment

Posted by on April 19, 2020 in powershell, Scripts, vmware

 

PowerCLI: Up time of virtual machines

2 very prominent solutions are offered when you try to search for powerCLI scripts to check up time of virtual machines in your estate

  • $vm.ExtensionData.Runtime.BootTime
  • Get-Stat -Entity $VM -State sys.uptime.latest -RealTime -MaxSample 1

I don’t even know why some people recommends them. It is very obvious from other posts that these values have issues.

$vm.ExtensionData.Runtime.BootTime is not a consistent value, i.e. the value is not there all the time.

whereas, sys.uptime.latest only records the uptime in that host. If the VM is moved to another host, this is reset

It also took me awhile to find the correct solutions and it was quite frustrating because many techies just blindly offer the wrong advise to users.

Anyway, the correct method is this:

Get-Stat -Entity $VM -State sys.osuptime.latest -RealTime -MaxSample 1

You can compare the values on the same VM, yourself

 
Leave a comment

Posted by on March 22, 2020 in powershell, vmware