From 793791888eaff3e59c01bf4e9c1d05b90f4ec0ce Mon Sep 17 00:00:00 2001 From: Jared Burns Date: Fri, 19 Jan 2024 10:04:27 -0500 Subject: [PATCH] refactor: modify if statement for nsxtedgeNodes refactored the if statement for nsxtedgeNodes on PowerManagement-ManagementDomain.ps1 to allow for check of overlay network for running Virtual machines Signed-off-by: Jared Burns --- CHANGELOG.md | 1 + .../PowerManagement-ManagementDomain.ps1 | 47 ++++++++++++++++--- VMware.CloudFoundation.PowerManagement.psd1 | 2 +- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d8989e..f7422ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Refactor: - Replaced the use of `Test-NetConnection` to with `Test-EndpointConnection` from the PowerShell module `PowerValidatedSolutions`. [GH-85](https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/85) +- Replaced current if statement `nsxtedgeNodes` to check for Virtual Machines running on Overlay Network. [GH-86] (https://github.com/vmware/powershell-module-for-vmware-cloud-foundation-power-management/pull/86) Chore: diff --git a/SampleScripts/PowerManagement-ManagementDomain.ps1 b/SampleScripts/PowerManagement-ManagementDomain.ps1 index 54a9c7d..322873b 100644 --- a/SampleScripts/PowerManagement-ManagementDomain.ps1 +++ b/SampleScripts/PowerManagement-ManagementDomain.ps1 @@ -494,14 +494,49 @@ if ($PsBoundParameters.ContainsKey("shutdown") -or $PsBoundParameters.ContainsKe } } + # Check if there are any running Virtual Machines on the Overlay Networks before shutting down Edge Cluster. if ($nsxtEdgeNodes) { - Write-PowerManagementLogMessage -Type INFO -Message "Stopping the NSX Edge nodes..." -Colour Green - Stop-CloudComponent -server $vcServer.fqdn -user $vcUser -pass $vcPass -nodes $nsxtEdgeNodes -timeout 600 - } - else { - Write-PowerManagementLogMessage -Type WARNING -Message "No NSX Edge nodes present. Skipping shutdown..." -Colour Cyan + if (Test-VCFConnection -server $server) { + if (Test-VCFAuthentication -server $server -user $user -pass $pass) { + $domain = Get-VCFWorkloadDomain | Select-Object name, type | Where-Object {$_.type -eq "MANAGEMENT"} + if (($vcfVcenterDetails = Get-vCenterServerDetail -server $server -user $user -pass $pass -domain $domain.name)) { + if (Test-vSphereConnection -server $($vcfVcenterDetails.fqdn)) { + if (Test-vSphereAuthentication -server $vcfVcenterDetails.fqdn -user $vcfVcenterDetails.ssoAdmin -pass $vcfVcenterDetails.ssoAdminPass) { + if (($vcfNsxDetails = Get-NsxtServerDetail -fqdn $server -username $user -password $pass -domain $domain.name)) { + if (Test-NSXTConnection -server $vcfNsxDetails.fqdn) { + if (Test-NSXTAuthentication -server $vcfNsxDetails.fqdn -user $vcfNsxDetails.adminUser -pass $vcfNsxDetails.adminPass) { + $nsx_segments = Get-NsxtSegment | Select-Object display_name, type | Where-Object { $_.type -eq "ROUTED" } + foreach ($segment in $nsx_segments) { + $segmentName = $segment.display_name + $cloudvms = Get-VM | Get-NetworkAdapter | Where-Object { $_.NetworkName -eq $segmentName } | Select-Object Parent + } + $vmlist = $cloudvms.Parent + $stopExecuted = $false + foreach ($vm in $vmlist) { + $vmName = $vm.Name + $powerState = $vm.PowerState + if ($powerState -eq "PoweredOn") { + Write-Output "VM Name: $vmName, Power State: $powerState, Please power off the VM(s) Connect to NSX-T Segments before you shutdown Edge Cluster" + $stopExecuted = $true + } + if (-not $stopExecuted) { + Write-PowerManagementLogMessage -Type INFO -Message "Stopping the NSX Edge nodes..." -Colour Green + Stop-CloudComponent -server $server -user $user -pass $pass -nodes $nsxtEdgeNodes -timeout 600 + $stopExecuted = $true + } + } + } else { + Write-PowerManagementLogMessage -Type WARNING -Message "No NSX Edge nodes present. Skipping shutdown..." -Colour Cyan + } + } + } + } + } + } + } + } } - + #Stop NSX Manager nodes Write-PowerManagementLogMessage -Type INFO -Message "Stopping the NSX Manager nodes..." -Colour Green Stop-CloudComponent -server $vcServer.fqdn -user $vcUser -pass $vcPass -nodes $nsxtNodes -timeout 600 diff --git a/VMware.CloudFoundation.PowerManagement.psd1 b/VMware.CloudFoundation.PowerManagement.psd1 index a68ecf5..f9cbd0e 100644 --- a/VMware.CloudFoundation.PowerManagement.psd1 +++ b/VMware.CloudFoundation.PowerManagement.psd1 @@ -11,7 +11,7 @@ RootModule = 'VMware.CloudFoundation.PowerManagement.psm1' # Version number of this module. - ModuleVersion = '1.4.1.1000' + ModuleVersion = '1.4.1.1001' # Supported PSEditions # CompatiblePSEditions = @()