Skip to content

Commit

Permalink
feat: improve code efficiency of Invoke-VcfPasswordPolicy
Browse files Browse the repository at this point in the history
Improvements around code efficiency:

- Added $commandSwitch to create the -allDomains or -workloadDomain switch
- Refactored all Publish- calls to use $commandSwitch resulting in single command per element and reduction of ten lines of code per element
- 25% reduction in lines of code for `Invoke-VcfPasswordPolicy`

Signed-off-by: Gary Blake [email protected]
  • Loading branch information
GaryJBlake committed Dec 23, 2022
1 parent b72d8de commit 4e4e6d9
Showing 1 changed file with 38 additions and 79 deletions.
117 changes: 38 additions & 79 deletions VMware.CloudFoundation.Reporting.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -578,13 +578,12 @@ Function Invoke-VcfPasswordPolicy {
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$sddcRootPass,
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$reportPath,
[Parameter (ParameterSetName = 'All-WorkloadDomains', Mandatory = $true)] [ValidateNotNullOrEmpty()] [Switch]$allDomains,
[Parameter (ParameterSetName = 'Specific--WorkloadDomain', Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$workloadDomain,
[Parameter (ParameterSetName = 'Specific-WorkloadDomain', Mandatory = $true)] [ValidateNotNullOrEmpty()] [String]$workloadDomain,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [Switch]$darkMode,
[Parameter (Mandatory = $false)] [ValidateNotNullOrEmpty()] [Switch]$policyByProduct
)

Try {

Clear-Host; Write-Host ""

if (Test-VCFConnection -server $sddcManagerFqdn) {
Expand All @@ -593,92 +592,53 @@ Function Invoke-VcfPasswordPolicy {
if (!(Test-Path -Path $reportPath)) {Write-Warning "Unable to locate report path $reportPath, enter a valid path and try again"; Write-Host ""; Break }
if ($PsBoundParameters.ContainsKey("allDomains")) {
$reportname = $defaultReport.Split('.')[0] + "-" + $sddcManagerFqdn.Split(".")[0] + ".htm"
$reportData = "<h1>SDDC Manager: $sddcManagerFqdn</h1>"
$workflowMessage = "VMware Cloud Foundation instance ($sddcManagerFqdn)"
$commandSwitch = "-allDomains"
} else {
$reportname = $defaultReport.Split('.')[0] + "-" + $workloadDomain + ".htm"
$reportData = "<h1>Workload Domain: $workloadDomain</h1>"
$workflowMessage = "Workload Domain ($workloadDomain)"
$commandSwitch = "-workloadDomain $workloadDomain"
}
Start-SetupLogFile -Path $reportPath -ScriptName $MyInvocation.MyCommand.Name # Setup Log Location and Log File
Write-LogMessage -Type INFO -Message "Starting the Process of Running a Password Policy Report for $workflowMessage." -Colour Yellow
Write-LogMessage -Type INFO -Message "Setting up the log file to path $logfile."
Write-LogMessage -Type INFO -Message "Setting up report folder and report $reportName."

# Collect Password Policies
if ($PsBoundParameters.ContainsKey('allDomains')) {
Write-LogMessage -Type INFO -Message "Collecting SDDC Manager Password Policies for $workflowMessage."
$sddcManagerPasswordExpirationHtml = Publish-SddcManagerPasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -sddcRootPass $sddcRootPass -allDomains
$sddcManagerPasswordComplexityHtml = Publish-SddcManagerPasswordComplexity -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -sddcRootPass $sddcRootPass -allDomains
$sddcManagerAccountLockoutHtml = Publish-SddcManagerAccountLockout -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -sddcRootPass $sddcRootPass -allDomains

Write-LogMessage -Type INFO -Message "Collecting vCenter Single Sign-On Password Policies for $workflowMessage."
$ssoPasswordExpirationHtml = Publish-SsoPasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -allDomains
$ssoPasswordComplexityHtml = Publish-SsoPasswordComplexity -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -allDomains
$SsoAccountLockoutHtml = Publish-SsoAccountLockout -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -allDomains

Write-LogMessage -Type INFO -Message "Collecting vCenter Server Password Expiration Policy for $workflowMessage."
$vcenterPasswordExpirationHtml = Publish-VcenterPasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -allDomains

Write-LogMessage -Type INFO -Message "Collecting vCenter Server (Local User) Password Policies for $workflowMessage."
$vcenterLocalPasswordExpirationHtml = Publish-VcenterLocalPasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -allDomains
$vcenterLocalPasswordComplexityHtml = Publish-VcenterLocalPasswordComplexity -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -allDomains
$vcenterLocalAccountLockoutHtml = Publish-VcenterLocalAccountLockout -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -allDomains

Write-LogMessage -Type INFO -Message "Collecting NSX Manager Password Policies for $workflowMessage."
$nsxManagerPasswordExpirationHtml = Publish-NsxManagerPasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -allDomains
$nsxManagerPasswordComplexityHtml = Publish-NsxManagerPasswordComplexity -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -allDomains
$nsxMangerAccountLockoutHtml = Publish-NsxManagerAccountLockout -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -allDomains

Write-LogMessage -Type INFO -Message "Collecting NSX Edge Password Policies for $workflowMessage."
$nsxEdgePasswordExpirationHtml = Publish-NsxEdgePasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -allDomains
$nsxEdgePasswordComplexityHtml = Publish-NsxEdgePasswordComplexity -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -allDomains
$nsxEdgeAccountLockoutHtml = Publish-NsxEdgeAccountLockout -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -allDomains

Write-LogMessage -Type INFO -Message "Collecting ESXi Password Policies for $workflowMessage."
$esxiPasswordExpirationHtml = Publish-EsxiPasswordPolicy -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -policy PasswordExpiration -allDomains
$esxiPasswordComplexityHtml = Publish-EsxiPasswordPolicy -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -policy PasswordComplexity -allDomains
$esxiAccountLockoutHtml = Publish-EsxiPasswordPolicy -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -policy AccountLockout -allDomains
}
else {
Write-LogMessage -Type INFO -Message "Collecting SDDC Manager Password Expiration Policies for $workflowMessage."
$sddcManagerPasswordExpirationHtml = Publish-SddcManagerPasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -sddcRootPass $sddcRootPass -workloadDomain $workloadDomain
$sddcManagerPasswordComplexityHtml = Publish-SddcManagerPasswordComplexity -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -sddcRootPass $sddcRootPass -workloadDomain $workloadDomain
$sddcManagerAccountLockoutHtml = Publish-SddcManagerAccountLockout -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -sddcRootPass $sddcRootPass -workloadDomain $workloadDomain

Write-LogMessage -Type INFO -Message "Collecting vCenter Single Sign-On Password Expiration Policies for $workflowMessage."
$ssoPasswordExpirationHtml = Publish-SsoPasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -workloadDomain $workloadDomain
$ssoPasswordComplexityHtml = Publish-SsoPasswordComplexity -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -workloadDomain $workloadDomain
$SsoAccountLockoutHtml = Publish-SsoAccountLockout -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -workloadDomain $workloadDomain

Write-LogMessage -Type INFO -Message "Collecting vCenter Password Expiration Policy for $workflowMessage."
$vcenterPasswordExpirationHtml = Publish-VcenterPasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -workloadDomain $workloadDomain

Write-LogMessage -Type INFO -Message "Collecting vCenter Server (Local User) Password Policies for $workflowMessage."
$vcenterLocalPasswordExpirationHtml = Publish-VcenterLocalPasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -workloadDomain $workloadDomain
$vcenterLocalPasswordComplexityHtml = Publish-VcenterLocalPasswordComplexity -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -workloadDomain $workloadDomain
$vcenterLocalAccountLockoutHtml = Publish-VcenterLocalAccountLockout -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -workloadDomain $workloadDomain

Write-LogMessage -Type INFO -Message "Collecting NSX Manager Password Policies for $workflowMessage."
$nsxManagerPasswordExpirationHtml = Publish-NsxManagerPasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -workloadDomain $workloadDomain
$nsxManagerPasswordComplexityHtml = Publish-NsxManagerPasswordComplexity -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -workloadDomain $workloadDomain
$nsxMangerAccountLockoutHtml = Publish-NsxManagerAccountLockout -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -workloadDomain $workloadDomain

Write-LogMessage -Type INFO -Message "Collecting NSX Edge Password Policies for $workflowMessage."
$nsxEdgePasswordExpirationHtml = Publish-NsxEdgePasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -workloadDomain $workloadDomain
$nsxEdgePasswordComplexityHtml = Publish-NsxEdgePasswordComplexity -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -workloadDomain $workloadDomain
$nsxEdgeAccountLockoutHtml = Publish-NsxEdgeAccountLockout -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -workloadDomain $workloadDomain

Write-LogMessage -Type INFO -Message "Collecting ESXi Password Policies for $workflowMessage."
$esxiPasswordExpirationHtml = Publish-EsxiPasswordPolicy -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -policy PasswordExpiration -workloadDomain $workloadDomain
$esxiPasswordComplexityHtml = Publish-EsxiPasswordPolicy -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -policy PasswordComplexity -workloadDomain $workloadDomain
$esxiAccountLockoutHtml = Publish-EsxiPasswordPolicy -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -policy AccountLockout -workloadDomain $workloadDomain
}
Write-LogMessage -Type INFO -Message "Collecting SDDC Manager Password Policies for $workflowMessage."
$sddcManagerPasswordExpirationHtml = Invoke-Expression "Publish-SddcManagerPasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -sddcRootPass $sddcRootPass $($commandSwitch)"
$sddcManagerPasswordComplexityHtml = Invoke-Expression "Publish-SddcManagerPasswordComplexity -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -sddcRootPass $sddcRootPass $($commandSwitch)"
$sddcManagerAccountLockoutHtml = Invoke-Expression "Publish-SddcManagerAccountLockout -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -sddcRootPass $sddcRootPass $($commandSwitch)"

Write-LogMessage -Type INFO -Message "Collecting vCenter Single Sign-On Password Policies for $workflowMessage."
$ssoPasswordExpirationHtml = Invoke-Expression "Publish-SsoPasswordPolicy -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -policy PasswordExpiration $($commandSwitch)"
$ssoPasswordComplexityHtml = Invoke-Expression "Publish-SsoPasswordPolicy -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -policy PasswordComplexity $($commandSwitch)"
$SsoAccountLockoutHtml = Invoke-Expression "Publish-SsoPasswordPolicy -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -policy AccountLockout $($commandSwitch)"

Write-LogMessage -Type INFO -Message "Collecting vCenter Server Password Expiration Policy for $workflowMessage."
$vcenterPasswordExpirationHtml = Invoke-Expression "Publish-VcenterPasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass $($commandSwitch)"

Write-LogMessage -Type INFO -Message "Collecting vCenter Server (Local User) Password Policies for $workflowMessage."
$vcenterLocalPasswordExpirationHtml = Invoke-Expression "Publish-VcenterLocalPasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass $($commandSwitch)"
$vcenterLocalPasswordComplexityHtml = Invoke-Expression "Publish-VcenterLocalPasswordComplexity -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass $($commandSwitch)"
$vcenterLocalAccountLockoutHtml = Invoke-Expression "Publish-VcenterLocalAccountLockout -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass $($commandSwitch)"

Write-LogMessage -Type INFO -Message "Collecting NSX Manager Password Policies for $workflowMessage."
$nsxManagerPasswordExpirationHtml = Invoke-Expression "Publish-NsxManagerPasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass $($commandSwitch)"
$nsxManagerPasswordComplexityHtml = Invoke-Expression "Publish-NsxManagerPasswordComplexity -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass $($commandSwitch)"
$nsxMangerAccountLockoutHtml = Invoke-Expression "Publish-NsxManagerAccountLockout -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass $($commandSwitch)"

Write-LogMessage -Type INFO -Message "Collecting NSX Edge Password Policies for $workflowMessage."
$nsxEdgePasswordExpirationHtml = Invoke-Expression "Publish-NsxEdgePasswordExpiration -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass $($commandSwitch)"
$nsxEdgePasswordComplexityHtml = Invoke-Expression "Publish-NsxEdgePasswordComplexity -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass $($commandSwitch)"
$nsxEdgeAccountLockoutHtml = Invoke-Expression "Publish-NsxEdgeAccountLockout -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass $($commandSwitch)"

Write-LogMessage -Type INFO -Message "Collecting ESXi Password Policies for $workflowMessage."
$esxiPasswordExpirationHtml = Invoke-Expression "Publish-EsxiPasswordPolicy -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -policy PasswordExpiration $($commandSwitch)"
$esxiPasswordComplexityHtml = Invoke-Expression "Publish-EsxiPasswordPolicy -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -policy PasswordComplexity $($commandSwitch)"
$esxiAccountLockoutHtml = Invoke-Expression "Publish-EsxiPasswordPolicy -server $sddcManagerFqdn -user $sddcManagerUser -pass $sddcManagerPass -policy AccountLockout $($commandSwitch)"

# Combine all information gathered into a single HTML report
if ($PsBoundParameters.ContainsKey("allDomains")) {
$reportData = "<h1>SDDC Manager: $sddcManagerFqdn</h1>"
} else{
$reportData = "<h1>Workload Domain: $workloadDomain</h1>"
}
if ($PsBoundParameters.ContainsKey("policyByProduct")) {
$reportData += $sddcManagerPasswordExpirationHtml
$reportData += $sddcManagerPasswordComplexityHtml
Expand Down Expand Up @@ -747,8 +707,7 @@ Function Invoke-VcfPasswordPolicy {
}
}
}
}
Catch {
} Catch {
Debug-CatchWriter -object $_
}
}
Expand Down

0 comments on commit 4e4e6d9

Please sign in to comment.