Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up some formatting and make it more readable. #578

Merged
merged 3 commits into from
Dec 13, 2024
Merged
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
6 changes: 2 additions & 4 deletions powershell/internal/ConvertFrom-QueryString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ function ConvertFrom-QueryString {
if ($DecodeParameterNames) { $QueryParameterPair[0] = [System.Net.WebUtility]::UrlDecode($QueryParameterPair[0]) }
if ($OutputObject -is [hashtable]) {
$OutputObject.Add($QueryParameterPair[0], [System.Net.WebUtility]::UrlDecode($QueryParameterPair[1]))
}
else {
} else {
$OutputObject | Add-Member $QueryParameterPair[0] -MemberType NoteProperty -Value ([System.Net.WebUtility]::UrlDecode($QueryParameterPair[1]))
}
}
Write-Output $OutputObject
}
}

}
}
20 changes: 10 additions & 10 deletions powershell/internal/ConvertTo-MtMaesterResults.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function ConvertTo-MtMaesterResult {
)

function GetTenantName() {
if(Test-MtConnection Graph) {
if (Test-MtConnection Graph) {
$org = Invoke-MtGraphRequest -RelativeUri 'organization'
return $org.DisplayName
} elseif (Test-MtConnection Teams) {
Expand All @@ -24,7 +24,7 @@ function ConvertTo-MtMaesterResult {
}

function GetTenantId() {
if(Test-MtConnection Graph) {
if (Test-MtConnection Graph) {
$mgContext = Get-MgContext
return $mgContext.TenantId
} elseif (Test-MtConnection Teams) {
Expand All @@ -36,12 +36,12 @@ function ConvertTo-MtMaesterResult {
}

function GetAccount() {
if(Test-MtConnection Graph) {
if (Test-MtConnection Graph) {
$mgContext = Get-MgContext
return $mgContext.Account
#} elseif (Test-MtConnection Teams) {
# $tenant = Get-CsTenant #ToValidate: N/A
# return $tenant.DisplayName
#} elseif (Test-MtConnection Teams) {
# $tenant = Get-CsTenant #ToValidate: N/A
# return $tenant.DisplayName
} else {
return "Account (not connected to Graph)"
}
Expand All @@ -57,10 +57,10 @@ function ConvertTo-MtMaesterResult {
}

function GetFormattedDate($date) {
if(!$IsCoreCLR) { # Prevent 5.1 date format to json issue
if (!$IsCoreCLR) {
# Prevent 5.1 date format to json issue
return $date.ToString("o")
}
else {
} else {
return $date
}
}
Expand Down Expand Up @@ -158,4 +158,4 @@ function ConvertTo-MtMaesterResult {
}

return $mtTestResults
}
}
6 changes: 2 additions & 4 deletions powershell/internal/ConvertTo-QueryString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ function ConvertTo-QueryString {
if ($EncodeParameterNames) { $ParameterName = [System.Net.WebUtility]::UrlEncode($ParameterName) }
[void]$QueryString.AppendFormat('{0}={1}', $ParameterName, [System.Net.WebUtility]::UrlEncode($Item.Value))
}
}
elseif ($InputObject -is [object] -and $InputObject -isnot [ValueType]) {
} elseif ($InputObject -is [object] -and $InputObject -isnot [ValueType]) {
foreach ($Item in ($InputObject | Get-Member -MemberType Property, NoteProperty)) {
if ($QueryString.Length -gt 0) { [void]$QueryString.Append('&') }
[string] $ParameterName = $Item.Name
if ($EncodeParameterNames) { $ParameterName = [System.Net.WebUtility]::UrlEncode($ParameterName) }
[void]$QueryString.AppendFormat('{0}={1}', $ParameterName, [System.Net.WebUtility]::UrlEncode($InputObject.($Item.Name)))
}
}
else {
} else {
## Non-Terminating Error
$Exception = New-Object ArgumentException -ArgumentList ('Cannot convert input of type {0} to query string.' -f $InputObject.GetType())
Write-Error -Exception $Exception -Category ([System.Management.Automation.ErrorCategory]::ParserError) -CategoryActivity $MyInvocation.MyCommand -ErrorId 'ConvertQueryStringFailureTypeNotSupported' -TargetObject $InputObject
Expand Down
2 changes: 1 addition & 1 deletion powershell/internal/Get-IsNewMaesterVersionAvailable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ function Get-IsNewMaesterVersionAvailable {
Write-Host " → Get the latest tests built by the Maester team and community." -ForegroundColor Yellow
return $true
}
} catch { Write-Verbose -Message $_}
} catch { Write-Verbose -Message $_ }
return $false
}
13 changes: 12 additions & 1 deletion powershell/internal/Get-MtConfirmation.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
function Get-MtConfirmation {
<#
.SYNOPSIS
Get confirmation from user.

.DESCRIPTION
Get confirmation from user to continue with the operation.

.EXAMPLE
Get-MtConfirmation
#>

function Get-MtConfirmation {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification = 'Colors are beautiful')]
[CmdletBinding()]
param ($message)
Expand Down
5 changes: 5 additions & 0 deletions powershell/internal/Get-MtTotalEntraIdUserCount.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<#
.SYNOPSIS
Returns the total number of users from the Microsoft Graph API.
#>

function Get-MtTotalEntraIdUserCount {
[CmdletBinding()]
[OutputType([int])]
Expand Down
1 change: 0 additions & 1 deletion powershell/internal/Get-MtUserInteractive.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ function Get-MtUserInteractive {
param ()

return ([Environment]::UserInteractive -and !([Environment]::GetCommandLineArgs() | Where-Object { $_ -like '-NonI*' }))

}
11 changes: 4 additions & 7 deletions powershell/internal/Get-ObjectProperty.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,18 @@ function Get-ObjectProperty {
if ($InputObject -is [hashtable]) {
if ($InputObject.ContainsKey($Property[$iProperty])) {
$PropertyValue = $InputObject[$Property[$iProperty]]
}
else { $PropertyValue = $null}
}
else {
} else { $PropertyValue = $null }
} else {
$PropertyValue = Select-Object -InputObject $InputObject -ExpandProperty $Property[$iProperty] -ErrorAction SilentlyContinue
}
## Check for more nested properties
if ($iProperty -lt $Property.Count - 1) {
$InputObject = $PropertyValue
if ($null -eq $InputObject) { break }
}
else {
} else {
Write-Output $PropertyValue
}
}
}
}
}
}
3 changes: 0 additions & 3 deletions powershell/internal/Invoke-MtGraphRequestCache.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,12 @@ function Invoke-MtGraphRequestCache {
Write-Verbose ("Invoking Graph: $($Uri.AbsoluteUri)")
Write-Verbose ([string]::IsNullOrEmpty($Body))


if ($Method -eq 'GET') {
$results = Invoke-MgGraphRequest -Method $Method -Uri $Uri -Headers $Headers -OutputType $OutputType # -Body $Body # Cannot use Body with GET in PS 5.1
} else {
$results = Invoke-MgGraphRequest -Method $Method -Uri $Uri -Headers $Headers -OutputType $OutputType -Body $Body
}



if (!$isBatch -and $isMethodGet) {
# Update cache
if ($isInCache) {
Expand Down
2 changes: 1 addition & 1 deletion powershell/internal/Test-MtContext.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Test-MtContext {
if ($missingScopes) {
$message = "These Graph permissions are missing in the current connection => ($($missingScopes))."
$authType = (Get-MgContext).AuthType
if ($authType -eq 'Delegated') {
if ($authType -eq 'Delegated') {
$message += " Please use 'Connect-Maester'. For more information, use 'Get-Help Connect-Maester'."
} else {
$clientId = (Get-MgContext).ClientId
Expand Down
34 changes: 17 additions & 17 deletions powershell/public/Compare-MtTestResult.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,34 @@
function Compare-MtTestResult {
[CmdletBinding()]
param (
[Parameter(ParameterSetName="Directory",Position=0,Mandatory=$true)]
[Parameter(ParameterSetName = "Directory", Position = 0, Mandatory = $true)]
# Path to folder where test results are located. The two newest results will be compared.
$BaseDir,
[Parameter(ParameterSetName="Files",Position=0,Mandatory=$true)]
[Parameter(ParameterSetName = "Files", Position = 0, Mandatory = $true)]
# Path to the previous test result JSON-file to be used as a reference.
$PriorTest,
[Parameter(ParameterSetName="Files",Position=1,Mandatory=$true)]
[Parameter(ParameterSetName = "Files", Position = 1, Mandatory = $true)]
# Path to the newer test result JSON-file to be used as the current result.
$NewTest
)

if(-not ($NewTest -and $PriorTest)){
$reportProperties = @("Account","Blocks","CurrentVersion","ExecutedAt","FailedCount","LatestVersion","PassedCount","Result","SkippedCount","TenantId","TenantName","Tests","TotalCount")
if (-not ($NewTest -and $PriorTest)) {
$reportProperties = @("Account", "Blocks", "CurrentVersion", "ExecutedAt", "FailedCount", "LatestVersion", "PassedCount", "Result", "SkippedCount", "TenantId", "TenantName", "Tests", "TotalCount")
$reports = @()
$files = Get-ChildItem "$BaseDir\TestResults-*.json"
Write-Verbose "Found $($files.Count) TestResults-*.json files in $BaseDir"
foreach($file in $files){
foreach ($file in $files) {
$report = Get-Content $file | ConvertFrom-Json
if(-not (Compare-Object $reportProperties $report.PSObject.Properties.Name)){
if (-not (Compare-Object $reportProperties $report.PSObject.Properties.Name)) {
Write-Verbose "Report properties match, adding to collection"
$reports += $report
}
}
$reports = $reports | Sort-Object ExecutedAt -Descending
$tenants = $reports | Group-Object TenantId
$reportsToCompare = @()
foreach($tenant in $tenants){
if($tenant.Count -ge 2){
foreach ($tenant in $tenants) {
if ($tenant.Count -ge 2) {
$obj = [PSCustomObject]@{
tenant = $tenant.Name
reports = $tenant.Group | Select-Object -First 2
Expand All @@ -55,18 +55,18 @@ function Compare-MtTestResult {
}
}

foreach($reportToCompare in $reportsToCompare){
foreach ($reportToCompare in $reportsToCompare) {
Compare-MtTestResult -NewTest $reportToCompare.reports[0] -PriorTest $reportToCompare.reports[1]
}
}else{
$compareTests = ($NewTest.Tests + $PriorTest.Tests) | Group-Object Name,Result
$testDeltas = $compareTests | Where-Object {$_.Count -lt 2} | Select-Object -Unique @{n="Name";e={$_.Group.Name}}
} else {
$compareTests = ($NewTest.Tests + $PriorTest.Tests) | Group-Object Name, Result
$testDeltas = $compareTests | Where-Object { $_.Count -lt 2 } | Select-Object -Unique @{n = "Name"; e = { $_.Group.Name } }
$results = @()
foreach($testDelta in $testDeltas){
foreach ($testDelta in $testDeltas) {
$result = [PSCustomObject]@{
Name = $testDelta.Name
PriorState = ($PriorTest.Tests | Where-Object {$_.Name -eq $testDelta.Name}).Result
NewState = ($NewTest.Tests | Where-Object {$_.Name -eq $testDelta.Name}).Result
Name = $testDelta.Name
PriorState = ($PriorTest.Tests | Where-Object { $_.Name -eq $testDelta.Name }).Result
NewState = ($NewTest.Tests | Where-Object { $_.Name -eq $testDelta.Name }).Result
}
$results += $result
}
Expand Down
2 changes: 1 addition & 1 deletion powershell/public/Connect-Maester.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ function Connect-Maester {

$__MtSession.Connections = $Service


if ($Service -contains "Graph" -or $Service -contains "All") {
Write-Verbose "Connecting to Microsoft Graph"
try {
Expand Down Expand Up @@ -205,6 +204,7 @@ function Connect-Maester {
}
}
}

if ($Service -contains "Teams") {
# if ($Service -contains "Teams" -or $Service -contains "All") {
Write-Verbose "Connecting to Microsoft Teams"
Expand Down
1 change: 0 additions & 1 deletion powershell/public/Get-MtGraphScope.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ function Get-MtGraphScope {
# NOTE: We should only include read-only permissions in the default scopes.
# Other permissions should be opted-in by the user with switches like -SendMail.


# Default read-only scopes required for Maester.
$scopes = @( #IMPORTANT: Read note above before adding any new scopes.
'Directory.Read.All'
Expand Down
14 changes: 7 additions & 7 deletions powershell/public/Get-MtGroupMember.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
function Get-MtGroupMember {
[CmdletBinding()]
param(
[Parameter(Position=0,mandatory=$true)]
[Parameter(Position = 0, mandatory = $true)]
# ID for the Entra group to return members for.
[guid]$GroupId,
# Include indirect members through nested groups.
[switch]$Recursive
)

try{
Invoke-MtGraphRequest -RelativeUri "groups/$GroupId/" -ApiVersion v1.0|Out-Null
}catch{
try {
Invoke-MtGraphRequest -RelativeUri "groups/$GroupId/" -ApiVersion v1.0 | Out-Null
} catch {
Write-Error "Error obtaining group ($GroupId) from Microsoft Graph. Confirm the group exists in your tenant."
return $null
}
Expand All @@ -33,14 +33,14 @@ function Get-MtGroupMember {
$members = @()
$members += Invoke-MtGraphRequest -RelativeUri "groups/$GroupId/members" -ApiVersion v1.0

if(-not $recursive){
if (-not $recursive) {
return $members
}

$members | Where-Object {`
$_.'@odata.type' -eq "#microsoft.graph.group"
$_.'@odata.type' -eq "#microsoft.graph.group"
} | ForEach-Object {`
$members += Get-MtGroupMember -GroupId $_.id -Recursive
$members += Get-MtGroupMember -GroupId $_.id -Recursive
}

return $members
Expand Down
3 changes: 1 addition & 2 deletions powershell/public/Get-MtRoleMember.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function Get-MtRoleMember {
$Active = $true
}

if($Role) {
if ($Role) {
$RoleId = $Role | ForEach-Object { (Get-MtRoleInfo -RoleName $_) }
}

Expand Down Expand Up @@ -106,7 +106,6 @@ function Get-MtRoleMember {
$dirAssignmentsSplat.Filter += " and NOT(status eq 'Canceled' or status eq 'Denied' or status eq 'Failed' or status eq 'Revoked')"
}


$dirAssignments = Invoke-MtGraphRequest @dirAssignmentsSplat

if ($dirAssignments.id.Count -eq 0) {
Expand Down
14 changes: 4 additions & 10 deletions powershell/public/Get-MtUser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function Get-MtUser {
[string]$MemberOfRole
)

begin{
begin {

$Users = New-Object -TypeName 'System.Collections.ArrayList'

Expand All @@ -63,10 +63,8 @@ function Get-MtUser {

process {


Write-Verbose "Getting $Count $UserType users from the tenant."


if ( $UserType -eq "Admin" ) {
$UserType = "Member"
if ( $MemberOfRole ) {
Expand Down Expand Up @@ -142,16 +140,12 @@ function Get-MtUser {
}
}
}

} else {

if( $UserType -eq "Member" ){
if ( $UserType -eq "Member" ) {
$queryFilter = "userType eq 'Member'"
}
elseif( $UserType -eq "Guest" ){
} elseif ( $UserType -eq "Guest" ) {
$queryFilter = "userType eq 'Guest'"
}
else{
} else {
Write-Warning "UserType $($UserType) cannot be processed! Aborting!"
throw "User can not be queried, invalid UserType: $($UserType)"
}
Expand Down
1 change: 0 additions & 1 deletion powershell/public/Invoke-Maester.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ function Invoke-Maester {
# Skip the version check.
# If set, the version check will not be performed.
[switch] $SkipVersionCheck

)

function GetDefaultFileName() {
Expand Down
Loading
Loading