Framework/Helpers/CommandHelper.ps1
using namespace System.Management.Automation Set-StrictMode -Version Latest class CommandHelper { static [CommandDetails[]] $Mapping = @( # Services Security Status [CommandDetails]@{ Verb = "Get"; Noun = "AzSDKAzureServicesSecurityStatus"; ShortName = "GRS"; }, [CommandDetails]@{ Verb = "Get"; Noun = "AzSDKControlsStatus"; ShortName = "GCS"; }, [CommandDetails]@{ Verb = "Get"; Noun = "AzSDKExpressRouteNetworkSecurityStatus"; ShortName = "GES"; }, #Subscription Security [CommandDetails]@{ Verb = "Get"; Noun = "AzSDKSubscriptionSecurityStatus"; ShortName = "GSS"; }, [CommandDetails]@{ Verb = "Set"; Noun = "AzSDKSubscriptionSecurity"; ShortName = "SSS"; }, [CommandDetails]@{ Verb = "Update"; Noun = "AzSDKSubscriptionSecurity"; ShortName = "USS"; }, [CommandDetails]@{ Verb = "Remove"; Noun = "AzSDKSubscriptionSecurity"; ShortName = "RSS"; }, # CA [CommandDetails]@{ Verb = "Get"; Noun = "AzSDKContinuousAssurance"; ShortName = "GCA"; }, [CommandDetails]@{ Verb = "Install"; Noun = "AzSDKContinuousAssurance"; ShortName = "ICA"; }, [CommandDetails]@{ Verb = "Remove"; Noun = "AzSDKContinuousAssurance"; ShortName = "RCA"; }, [CommandDetails]@{ Verb = "Update"; Noun = "AzSDKContinuousAssurance"; ShortName = "UCA"; }, #Alerts [CommandDetails]@{ Verb = "Set"; Noun = "AzSDKAlerts"; ShortName = "SAL"; }, [CommandDetails]@{ Verb = "Remove"; Noun = "AzSDKAlerts"; ShortName = "RAL"; }, #Alerts Monitoring [CommandDetails]@{ Verb = "Set"; Noun = "AzSDKAlertMonitoring"; ShortName = "SAM"; }, [CommandDetails]@{ Verb = "Remove"; Noun = "AzSDKAlertMonitoring"; ShortName = "RAM"; }, #ARM Policies [CommandDetails]@{ Verb = "Set"; Noun = "AzSDKARMPolicies"; ShortName = "SAP"; }, [CommandDetails]@{ Verb = "Remove"; Noun = "AzSDKARMPolicies"; ShortName = "RAP"; }, #RBAC [CommandDetails]@{ Verb = "Set"; Noun = "AzSDKSubscriptionRBAC"; ShortName = "SRB"; }, [CommandDetails]@{ Verb = "Remove"; Noun = "AzSDKSubscriptionRBAC"; ShortName = "RRB"; }, # Security Centre [CommandDetails]@{ Verb = "Set"; Noun = "AzSDKAzureSecurityCenterPolicies"; ShortName = "SSC"; }, # OMS [CommandDetails]@{ Verb = "Install"; Noun = "AzSDKOMSSolution"; ShortName = "IOM"; }, [CommandDetails]@{ Verb = "Uninstall"; Noun = "AzSDKOMSetup"; ShortName = "UOM"; }, # FixControl [CommandDetails]@{ Verb = "Repair"; Noun = "AzSDKAzureServicesSecurity"; ShortName = "RRS"; }, [CommandDetails]@{ Verb = "Repair"; Noun = "AzSDKSubscriptionSecurity"; ShortName = "RSS"; }, # Policy Store [CommandDetails]@{ Verb = "Install"; Noun = "AzSDKOrganizationPolicy"; ShortName = "IOP"; }, [CommandDetails]@{ Verb = "Get"; Noun = "AzSDKInfo"; ShortName = "GAI"; }, [CommandDetails]@{ Verb = "Send"; Noun = "AzSDKInternalData"; ShortName = "INV"; } ); static BeginCommand([InvocationInfo] $invocationContext) { # Validate Command Prerequisites like AzureRM multiple version load issue [CommandHelper]::CheckCommandPrerequisites($invocationContext); [CommandHelper]::SetAzSDKModuleName($invocationContext); [CommandHelper]::SetCurrentAzSDKModuleVersion($invocationContext); # display warning if alias [CommandHelper]::CheckForAlias($invocationContext.InvocationName) } static CheckCommandPrerequisites([InvocationInfo] $invocationContext) { # Validate required module version dependency try { #Loop through all required modules list $invocationContext.MyCommand.Module.RequiredModules | ForEach-Object { $requiredModule = $_ $moduleList = Get-Module $requiredModule.Name #Get list of other than required version is loaded into session $otherThanRequiredModule = @(); $otherThanRequiredModule += $moduleList | Where-Object { $_.Version -ne $requiredModule.Version} if($otherThanRequiredModule.Count -gt 0 ) { #Display warning $loadedVersions = @(); $moduleList | ForEach-Object { $loadedVersions += $_.Version.ToString() }; Write-Host "Warning: Found multiple versions of Azure PowerShell ($($requiredModule.Name)) modules loaded in the session. ($($requiredModule.Name) versions found: $([string]::Join(", ", $loadedVersions)))" -ForegroundColor Yellow Write-Host "Warning: This will lead to issues when running AzSDK cmdlets." -ForegroundColor Yellow Write-Host 'Recommendation: Please start a fresh PowerShell session and run “import-module AzSDK” first to avoid getting into this situation.' -ForegroundColor Yellow } else { # Continue execution without any error or warning Write-Debug ($requiredModule.Name + " module version dependency validation successful") } }; } catch { Write-Debug "Not able to validate version dependency $_" } #check if old and new both modules are loaded in same session $newModule = Get-Module|Where-Object {$_.Name -like "$([Constants]::NewModuleName)*"} | Select-Object -First 1 $oldModule = Get-Module|Where-Object {$_.Name -like "$([Constants]::OldModuleName)*"} | Select-Object -First 1 if($newModule -and $oldModule) { $warningMsg = [String]::Format([Constants]::MultipleModulesWarning, $newModule.Name,$oldModule.Name,$oldModule.Name) Write-Host $warningMsg -ForegroundColor Yellow #stop execution throw ([SuppressedException]::new("",[SuppressedExceptionType]::Generic)) } else { # Continue execution without any error or warning Write-Debug ("Multiple modules ($([Constants]::NewModuleName) and $([Constants]::OldModuleName)) load validation successful") } } static [void] SetAzSDKModuleName([InvocationInfo] $invocationContext) { if($invocationContext) { [Constants]::SetAzSDKModuleName($invocationContext.MyCommand.Module.Name); } } static [void] SetCurrentAzSDKModuleVersion([InvocationInfo] $invocationContext) { if($invocationContext) { [Constants]::SetAzSDKCurrentModuleVersion($invocationContext.MyCommand.Version); } } static [void] CheckForAlias([string] $methodName) { if($methodName -like "*-$([Constants]::OldModuleName)*") { #get correct casing $methodName = get-command -Name $methodName $newMethodName = $methodName -replace ("-"+[Constants]::OldModuleName),("-"+[Constants]::NewModuleName); $CommandNameChangeWarning = [Constants]::CommandNameChangeWarning -f $methodName,$newMethodName Write-Warning $CommandNameChangeWarning } } } |