Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions Microsoft.AVS.Management/AVSGenericUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,102 @@ Function Limit-WildcardsandCodeInjectionCharacters {
}

}

function Normalize-VCBannerText {
<#
.DESCRIPTION
This function normalizes vCenter login banner text by replacing smart quotes,
cleaning unsupported characters, and preserving paragraph line breaks.
.PARAMETER String
Banner text to normalize.
.EXAMPLE
Normalize-VCBannerText -String "Welcome to \"AVS\""
Returns normalized text that is safe to pass to banner commands.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]
$String
)
begin {
# Normalize smart single and double quotes to ASCII quotes.
$String = $String.Replace([char]0x2018, "'").Replace([char]0x2019, "'")
$String = $String.Replace([char]0x201C, '"').Replace([char]0x201D, '"')

# Normalize non-breaking space to regular space.
$String = $String.Replace([char]0x00A0, ' ')

# Remove shell-sensitive special characters.
$String = $String.Replace("&", "").Replace("$", "")
$String = $String.Replace([string][char]0x0060, "")
$String = $String.Replace("(", "").Replace(")", "")
$String = $String.Replace("<", "").Replace(">", "")

# Remove emojis and unsupported symbols, while preserving letters, numbers,
# punctuation, spaces, and new lines.
$String = $String -replace '[^\p{L}\p{N}\p{P}\p{Zs}\t\r\n]', ''

# Collapse repeated spaces and tabs per line, preserving line breaks.
$lines = $String -split "(\r?\n)"
for ($i = 0; $i -lt $lines.Count; $i++) {
if ($lines[$i] -notmatch '^\r?\n$') {
$lines[$i] = ($lines[$i] -replace '[ \t]{2,}', ' ').TrimEnd()
}
}
$String = $lines -join ''
}
process {
return $String
}
}

function Assert-VCSSHSession {
[CmdletBinding()]
param()

if ($null -eq $SSH_Sessions -or -not $SSH_Sessions.ContainsKey("VC")) {
throw "SSH session to vCenter is not available. Ensure `$SSH_Sessions['VC'] is pre-established by the AVS platform."
}

$SshSession = $SSH_Sessions["VC"].Value
if ($null -eq $SshSession) {
throw "Failed to initialize SSH session to vCenter."
}

return $SshSession
}

function Write-VCSSHPermissionDiagnostic {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
$SshSession
)

try {
$probe = Invoke-SSHCommand -SSHSession $SshSession -Command "whoami; id; sudo -n -l 2>&1; echo SUDO_EXIT_CODE:`$?"
$lines = @($probe.Output | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })

$userName = if ($lines.Count -gt 0) { $lines[0].Trim() } else { "unknown" }

$idLine = $lines | Where-Object { $_ -match "^uid=" } | Select-Object -First 1
$isRoot = $false
if ($idLine -and $idLine -match "uid=0\(") {
$isRoot = $true
}

$hasPasswordlessSudo = $false
if ($lines -match "SUDO_EXIT_CODE:0") {
$hasPasswordlessSudo = $true
}
elseif ($lines -match "NOPASSWD") {
$hasPasswordlessSudo = $true
}

Write-Host ("VC SSH precheck: User={0}; IsRoot={1}; HasPasswordlessSudo={2}" -f $userName, $isRoot, $hasPasswordlessSudo)
}
catch {
Write-Warning ("VC SSH precheck could not run: {0}" -f $_.Exception.Message)
}
}
85 changes: 44 additions & 41 deletions Microsoft.AVS.Management/Microsoft.AVS.Management.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -10,131 +10,134 @@

# Script module or binary module file associated with this manifest.
RootModule = 'Microsoft.AVS.Management.psm1'

# Version number of this module.
ModuleVersion = '10.0.0'
ModuleVersion = '10.1.0'

# Supported PSEditions
# CompatiblePSEditions = @()

# ID used to uniquely identify this module
GUID = '84d7f529-e6a8-4b7e-99b4-dafc636ffad2'

# Author of this module
Author = 'Microsoft'

# Company or vendor of this module
CompanyName = 'Microsoft'

# Copyright statement for this module
Copyright = '(c) Microsoft. All rights reserved.'

# Description of the functionality provided by this module
Description = 'Various cmdlets for adminstrator level tasks in managing Azure VMware Solutions'

# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '7.4'

# Name of the PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# ClrVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @(
@{ModuleName = 'VMware.vSphere.SsoAdmin'; RequiredVersion = '1.4.0' }
@{ModuleName = 'VMware.VimAutomation.Core'; RequiredVersion = '13.3.0.24145081' }
@{ModuleName = 'VMware.VimAutomation.Storage'; RequiredVersion = '13.3.0.24145081' }
@{ModuleName = 'Posh-SSH'; ModuleVersion = '3.2.7' })

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
ScriptsToProcess = 'Classes.ps1'

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# Functions to export from this module
FunctionsToExport = @(
'Set-ToolsRepo'
'Set-CustomDRS'
'Remove-CustomRole'
'Get-EsxtopData'
'Set-VCLoginBanner'
'Get-VCLoginBanner'
'Remove-VCLoginBanner'
)

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()

# Variables to export from this module
# VariablesToExport = @()

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @()

# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
Tags = 'VMware','PowerCLI','Azure','AVS'

# A URL to the license for this module.
LicenseUri = 'https://raw.githubusercontent.com/Azure/Microsoft.AVS.Management/refs/heads/main/LICENSE'

# A URL to the main website for this project.
ProjectUri = 'https://github.com/Azure/Microsoft.AVS.Management'

# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
# ReleaseNotes = ''

# Prerelease string of this module
# Prerelease = ''

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false

# External dependent modules of this module
# ExternalModuleDependencies = @()

} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}
Loading