Framework/Abstracts/SVTCommandBase.ps1
using namespace System.Management.Automation Set-StrictMode -Version Latest # Base class for SVT classes being called from PS commands # Provides functionality to fire important events at command call class SVTCommandBase: CommandBase { [string[]] $ExcludeTags = @(); [string[]] $ControlIds = @(); [AttestControls] $AttestControls = [AttestControls]::None; hidden [ControlStateExtension] $ControlStateExt; hidden [bool] $UserHasStateAccess = $false; SVTCommandBase([string] $subscriptionId, [InvocationInfo] $invocationContext): Base($subscriptionId, $invocationContext) { [Helpers]::AbstractClass($this, [SVTCommandBase]); } hidden [SVTEventContext] CreateSVTEventContextObject() { return [SVTEventContext]@{ SubscriptionContext = $this.SubscriptionContext; }; } hidden [void] CommandStarted() { [SVTEventContext] $arg = $this.CreateSVTEventContextObject(); $versionMessage = $this.CheckModuleVersion(); if($versionMessage) { $arg.Messages += $versionMessage; } $this.PublishEvent([SVTEvent]::CommandStarted, $arg); } hidden [void] CommandError([System.Management.Automation.ErrorRecord] $exception) { [SVTEventContext] $arg = $this.CreateSVTEventContextObject(); $arg.ExceptionMessage = $exception; $this.PublishEvent([SVTEvent]::CommandError, $arg); } hidden [void] CommandCompleted([SVTEventContext[]] $arguments) { $this.PublishEvent([SVTEvent]::CommandCompleted, $arguments); } [string] InvokeFunction([PSMethod] $methodToCall) { throw [System.NotSupportedException] "The method is not supported in the class" } [string] EvaluateControlStatus() { return ([CommandBase]$this).InvokeFunction($this.RunAllControls); } #hidden [SVTEventContext[]] RunAllControlsWithAttestation() #{ # $result = $this.RunAllControls(); # if($this.AttestControls) # { # try # { # [SVTControlAttestation] $svtControlAttestation = [SVTControlAttestation]::new($result); # $svtControlAttestation.StartControlAttestation(); # } # catch # { # $this.CommandError($_); # } # } # return $result; #} # Dummy function declaration to define the function signature # Function is supposed to override in derived class hidden [SVTEventContext[]] RunAllControls() { return @(); } hidden [void] SetSVTBaseProperties([PSObject] $svtObject) { $svtObject.FilterTags = $this.ConvertToStringArray($this.FilterTags); $svtObject.ExcludeTags = $this.ConvertToStringArray($this.ExcludeTags); $svtObject.ControlIds = $this.ConvertToStringArray($this.ControlIds); $this.InitializeControlState(); if($this.UserHasStateAccess) { $svtObject.ControlStateExt = $this.ControlStateExt; } } hidden [void] InitializeControlState() { if(-not $this.ControlStateExt) { $this.ControlStateExt = [ControlStateExtension]::new(); $this.ControlStateExt.Initialize($false); $this.UserHasStateAccess = $this.ControlStateExt.HasControlStateReadAccessPermissions(); } } [void] PostCommandCompletedAction([SVTEventContext[]] $arguments) { if($this.AttestControls -ne [AttestControls]::None) { try { [SVTControlAttestation] $svtControlAttestation = [SVTControlAttestation]::new($arguments, $this.AttestControls); $svtControlAttestation.StartControlAttestation(); } catch { $this.CommandError($_); } } } } |