Skip to content

Commit

Permalink
Merge pull request #133 from maester365/merill-cisa
Browse files Browse the repository at this point in the history
Moved module variables to script
  • Loading branch information
merill authored Apr 25, 2024
2 parents 3437198 + d27606a commit 7e2bbf4
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 22 deletions.
9 changes: 6 additions & 3 deletions powershell/Maester.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

## Initialize Module Variables
## Update Clear-ModuleVariable function in internal/Clear-ModuleVariable.ps1 if you add new variables here
$MtGraphCache = @{}
$MtGraphBaseUri = $null
$MtTestResultDetail = @{}
$__MtSession = @{
GraphCache = @{}
GraphBaseUri = $null
TestResultDetail = @{}
}
New-Variable -Name __MtSession -Value $__MtSession -Scope Script -Force

# Import private and public scripts and expose the public ones
$privateScripts = @(Get-ChildItem -Path "$PSScriptRoot\internal" -Recurse -Filter "*.ps1")
Expand Down
4 changes: 2 additions & 2 deletions powershell/internal/Clear-ModuleVariable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Function Clear-ModuleVariable {
param()

Clear-MtGraphCache
$MtGraphBaseUri = $null
$MtTestResultDetail = @{}
$__MtSession.GraphBaseUri = $null
$__MtSession.TestResultDetail = @{}
}
2 changes: 1 addition & 1 deletion powershell/internal/ConvertTo-MtMaesterResults.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function ConvertTo-MtMaesterResult {
ScriptBlockFile = $test.ScriptBlock.File
ErrorRecord = $test.ErrorRecord
Block = $test.Block.Name
ResultDetail = $MtTestResultDetail[$test.ExpandedName]
ResultDetail = $__MtSession.TestResultDetail[$test.ExpandedName]
}
$mtTests += $mtTestInfo
}
Expand Down
10 changes: 5 additions & 5 deletions powershell/internal/Invoke-MtGraphRequestCache.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ Function Invoke-MtGraphRequestCache {

$results = $null
$isBatch = $uri.AbsoluteUri.EndsWith('$batch')
$isInCache = $MtGraphCache.ContainsKey($Uri.AbsoluteUri)
$isInCache = $__MtSession.GraphCache.ContainsKey($Uri.AbsoluteUri)
$cacheKey = $Uri.AbsoluteUri
$isMethodGet = $Method -eq 'GET'

if (!$DisableCache -and !$isBatch -and $isInCache -and $isMethodGet) {
# Don't read from cache for batch requests.
Write-Verbose ("Checking cache: $($cacheKey)")
$results = $MtGraphCache[$cacheKey]
Write-Verbose ("Using graph cache: $($cacheKey)")
$results = $__MtSession.GraphCache[$cacheKey]
}

if (!$results) {
Expand All @@ -36,9 +36,9 @@ Function Invoke-MtGraphRequestCache {
if (!$isBatch -and $isMethodGet) {
# Update cache
if ($isInCache) {
$MtGraphCache[$cacheKey] = $results
$__MtSession.GraphCache[$cacheKey] = $results
} else {
$MtGraphCache.Add($cacheKey, $results)
$__MtSession.GraphCache.Add($cacheKey, $results)
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions powershell/public/Add-MtTestResultDetail.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ Function Add-MtTestResultDetail {

Write-MtProgress -Activity "Running tests" -Status $testName
Write-Verbose "Adding test result detail for $testName"
Write-Verbose "Description: $Description"
# Write-Verbose "Description: $Description" # Makes it VERY verbose
Write-Verbose "Result: $Result"

if ($null -ne $MtTestResultDetail -and ![string]::IsNullOrEmpty($testName)) {
# Only set if we are running in the context of Maester
$MtTestResultDetail[$testName] = $testInfo
if ($__MtSession -and $__MtSession.TestResultDetail) {
if (![string]::IsNullOrEmpty($testName)) {
# Only set if we are running in the context of Maester
$__MtSession.TestResultDetail[$testName] = $testInfo
}
}
}
2 changes: 1 addition & 1 deletion powershell/public/Clear-MtGraphCache.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ function Clear-MtGraphCache {

Write-Verbose -Message "Clearing the results cached from Graph API calls in this session"

$MtGraphCache = @{}
$__MtSession.GraphCache = @{}
}
3 changes: 1 addition & 2 deletions powershell/public/Get-MtConditionalAccessPolicy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Function Get-MtConditionalAccessPolicy {

Write-Verbose -Message "Getting conditional access policies."

# Note Graph v1.0 appears to return updates faster than beta
return Invoke-MtGraphRequest -RelativeUri 'identity/conditionalAccess/policies' -ApiVersion v1.0
return Invoke-MtGraphRequest -RelativeUri 'identity/conditionalAccess/policies' -ApiVersion beta

}
6 changes: 3 additions & 3 deletions powershell/public/Invoke-MtGraphRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ Function Invoke-MtGraphRequest {

begin {
if ([string]::IsNullOrEmpty($GraphBaseUri)) {
if ([string]::IsNullOrEmpty($MtGraphBaseUri)) {
if ([string]::IsNullOrEmpty($__MtSession.GraphBaseUri)) {
Write-Verbose -Message "Setting GraphBaseUri to default value from MgContext."
$MtGraphBaseUri = $((Get-MgEnvironment -Name (Get-MgContext).Environment).GraphEndpoint)
$__MtSession.GraphBaseUri = $((Get-MgEnvironment -Name (Get-MgContext).Environment).GraphEndpoint)
}
}
$GraphBaseUri = $MtGraphBaseUri
$GraphBaseUri = $__MtSession.GraphBaseUri

$listRequests = New-Object 'System.Collections.Generic.List[psobject]'

Expand Down
2 changes: 1 addition & 1 deletion powershell/public/Test-MtPimAlertsExists.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Function Test-MtPimAlertsExists {
$Alert = Invoke-MtGraphRequest -ApiVersion "beta" -RelativeUri "privilegedAccess/aadroles/resources/$($tenantId)/alerts" | Where-Object { $_.id -eq $AlertId }

$AffectedRoleAssignments = $Alert.additionalData | ForEach-Object {
$CurrentItem = $_['item']
$CurrentItem = $_.item
$result = New-Object psobject
foreach ($entry in $CurrentItem.GetEnumerator()) {
$result | Add-Member -MemberType NoteProperty -Name $entry.key -Value $entry.value -Force
Expand Down

0 comments on commit 7e2bbf4

Please sign in to comment.