Skip to content

Commit

Permalink
Addressing PSScriptAnalyzer suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
michevnew committed Jul 28, 2023
1 parent 93a5f43 commit 74c1a7a
Show file tree
Hide file tree
Showing 50 changed files with 852 additions and 848 deletions.
2 changes: 1 addition & 1 deletion AADRolesInventory-Graph.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ if ($IncludePIMEligibleAssignments) {

if (!$roles) { Write-Verbose "No valid role assignments found, verify the required permissions have been granted?"}

Write-Verbose "A total of $($roles.count) role assignments were found, of which $(($roles | ? {$_.directoryScopeId -eq "/"}).Count) are tenant-wide and $(($roles | ? {$_.directoryScopeId -ne "/"}).Count) are AU-scoped. $(($roles | ? {!$_.status}).Count) roles are permanently assigned, you might want to address that!"
Write-Verbose "A total of $($roles.count) role assignments were found, of which $(($roles | ? {$_.directoryScopeId -eq "/"}).Count) are tenant-wide and $(($roles | ? {$_.directoryScopeId -ne "/"}).Count) are AU-scoped. $(($roles | ? {!$_.status}).Count) roles are permanently assigned, you might want to address that!"
#endregion Roles

#region Output
Expand Down
2 changes: 1 addition & 1 deletion AADRolesInventory-MG.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if ($IncludePIMEligibleAssignments) {

if (!$roles) { Write-Verbose "No valid role assignments found, verify the required permissions have been granted?"}

Write-Verbose "A total of $($roles.count) role assignments were found, of which $(($roles | ? {$_.directoryScopeId -eq "/"}).Count) are tenant-wide and $(($roles | ? {$_.directoryScopeId -ne "/"}).Count) are AU-scoped. $(($roles | ? {!$_.status}).Count) roles are permanently assigned, you might want to address that!"
Write-Verbose "A total of $($roles.count) role assignments were found, of which $(($roles | ? {$_.directoryScopeId -eq "/"}).Count) are tenant-wide and $(($roles | ? {$_.directoryScopeId -ne "/"}).Count) are AU-scoped. $(($roles | ? {!$_.status}).Count) roles are permanently assigned, you might want to address that!"
#endregion Roles

#region Output
Expand Down
2 changes: 1 addition & 1 deletion AADRolesInventory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ foreach ($AADRole in $AADRoles) {
$AADRoleMembers = Get-AzureADDirectoryRoleMember -ObjectId $AADRole.ObjectId
#if no role members assigned, skip
if (!$AADRoleMembers) { continue }

foreach ($AADRoleMember in $AADRoleMembers) {
#prepare the output
if (!$RolesHash[$AADRoleMember.ObjectId]) {
Expand Down
4 changes: 2 additions & 2 deletions AAD_Groups_MemberOf_inventory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ else {
#region GroupMembership
#Cycle over each user and fetch group membership
$output = [System.Collections.Generic.List[Object]]::new() #output variable
$i=0; $count = 1; $PercentComplete = 0;
$count = 1; $PercentComplete = 0;
foreach ($u in $Users) {
#Progress message
$ActivityMessage = "Retrieving data for user $($u.userPrincipalName). Please wait..."
Expand Down Expand Up @@ -146,4 +146,4 @@ foreach ($u in $Users) {
#endregion GroupMembership

#Finally, export to CSV
$output | select * | Export-CSV -Path "$((Get-Date).ToString('yyyy-MM-dd_HH-mm-ss'))_AADGroupMembership.csv" -NoTypeInformation -Encoding UTF8 -UseCulture
$output | select * #| Export-CSV -Path "$((Get-Date).ToString('yyyy-MM-dd_HH-mm-ss'))_AADGroupMembership.csv" -NoTypeInformation -Encoding UTF8 -UseCulture
4 changes: 2 additions & 2 deletions AAD_Groups_MemberOf_inventoryMG.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ else {
#region GroupMembership
#Cycle over each user and fetch group membership
$output = [System.Collections.Generic.List[Object]]::new() #output variable
$i=0; $count = 1; $PercentComplete = 0;
$count = 1; $PercentComplete = 0;
foreach ($u in $Users) {
#Progress message
$ActivityMessage = "Retrieving data for user $($u.userPrincipalName). Please wait..."
Expand Down Expand Up @@ -84,7 +84,7 @@ foreach ($u in $Users) {
#For each group returned, output the relevant details
foreach ($Group in $uGroups) {
if ($Group.AdditionalProperties.'@odata.type' -ne "#microsoft.graph.group") { continue } #Filter out non-group objects

#prepare the output
$uInfo = [PSCustomObject][ordered]@{
"Id" = $u.id
Expand Down
4 changes: 2 additions & 2 deletions AAD_Groups_Members_inventory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ $body = @{

#Obtain the token
Write-Verbose "Authenticating..."
try {
$tokenRequest = Invoke-WebRequest -Method Post -Uri $url -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing -ErrorAction Stop
try {
$tokenRequest = Invoke-WebRequest -Method Post -Uri $url -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing -ErrorAction Stop
$token = ($tokenRequest.Content | ConvertFrom-Json).access_token

$authHeader = @{
Expand Down
6 changes: 3 additions & 3 deletions AU_memberOf_inventory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ $body = @{

#Obtain the token
Write-Verbose "Authenticating..."
try {
$tokenRequest = Invoke-WebRequest -Method Post -Uri $url -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing -ErrorAction Stop
try {
$tokenRequest = Invoke-WebRequest -Method Post -Uri $url -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing -ErrorAction Stop
$token = ($tokenRequest.Content | ConvertFrom-Json).access_token

$authHeader = @{
Expand Down Expand Up @@ -79,7 +79,7 @@ else {
#region AUs
#Cycle over each user and fetch AU membership
$output = [System.Collections.Generic.List[Object]]::new() #output variable
$i=0; $count = 1; $PercentComplete = 0;
$count = 1; $PercentComplete = 0;
foreach ($u in $Users) {
#Progress message
$ActivityMessage = "Retrieving data for user $($u.userPrincipalName). Please wait..."
Expand Down
11 changes: 5 additions & 6 deletions Calendar_Permissions_inventory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ function Get-CalendarPermissionInventory {
#>

[CmdletBinding()]

Param
(
#Specify whether to include user mailboxes in the result
[Switch]$IncludeUserMailboxes,
[Switch]$IncludeUserMailboxes,
#Specify whether to include Shared mailboxes in the result
[Switch]$IncludeSharedMailboxes,
#Specify whether to include Room and Equipment mailboxes in the result
Expand All @@ -41,14 +41,13 @@ function Get-CalendarPermissionInventory {
#Specify whether to write the output in condensed format
[Switch]$CondensedOutput)


#Initialize the variable used to designate mailbox types, based on the input parameters
$included = @()
if($IncludeSharedMailboxes) { $included += "SharedMailbox"}
if($IncludeRoomMailboxes) { $included += "RoomMailbox"; $included += "EquipmentMailbox"}
#if no parameters specified, return only User mailboxes
if($IncludeUserMailboxes -or !$included) { $included += "UserMailbox"}

#Confirm connectivity to Exchange Online
try { $session = Get-PSSession -InstanceId (Get-OrganizationConfig).RunspaceId.Guid -ErrorAction Stop }
catch { Write-Error "No active Exchange Online session detected, please connect to ExO first: https://technet.microsoft.com/en-us/library/jj984289(v=exchg.160).aspx" -ErrorAction Stop }
Expand All @@ -60,7 +59,7 @@ function Get-CalendarPermissionInventory {
else {
$MBList = Invoke-Command -Session $session -ScriptBlock { Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails $Using:included | Select-Object -Property Displayname,Identity,PrimarySMTPAddress,RecipientTypeDetails }
}

#If no mailboxes are returned from the above cmdlet, stop the script and inform the user
if (!$MBList) { Write-Error "No mailboxes of the specifyied types were found, specify different criteria." -ErrorAction Stop}

Expand Down Expand Up @@ -130,7 +129,7 @@ function Get-CalendarPermissionInventory {
elseif ($entry.User.UserType.ToString() -eq "External") { $varUser = $entry.User.RecipientPrincipal.PrimarySmtpAddress.Replace("ExchangePublishedUser.",$null); $varType = "External" }
elseif ($entry.User.UserType.ToString() -eq "Unknown") { $varUser = $entry.User.DisplayName; $varType = "Orphaned" }
else { continue }

Add-Member -InputObject $objPermissions -MemberType NoteProperty -Name "User" -Value $varUser
Add-Member -InputObject $objPermissions -MemberType NoteProperty -Name "Permissions" -Value $($entry.AccessRights -join ";")
Add-Member -InputObject $objPermissions -MemberType NoteProperty -Name "Permission Type" -Value $varType
Expand Down
17 changes: 8 additions & 9 deletions Calendar_Permissions_inventoryV2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ function Get-CalendarPermissionInventory {
#>

[CmdletBinding()]

Param
(
#Specify whether to include user mailboxes in the result
[Switch]$IncludeUserMailboxes,
[Switch]$IncludeUserMailboxes,
#Specify whether to include Shared mailboxes in the result
[Switch]$IncludeSharedMailboxes,
#Specify whether to include Room, Equipment and Booking mailboxes in the result
[Switch]$IncludeRoomMailboxes,
#Specify whether to return all mailbox types
[Switch]$IncludeAll)


#Initialize the variable used to designate mailbox types, based on the input parameters
$included = @()
if($IncludeSharedMailboxes) { $included += "SharedMailbox"}
Expand All @@ -50,8 +49,8 @@ function Get-CalendarPermissionInventory {

#Make sure we have a V2 version of the module
try { Get-Command Get-EXOMailbox -ErrorAction Stop | Out-Null }
catch { "This script requires the Exchange Online V2 PowerShell module. Learn more about it here: https://docs.microsoft.com/en-us/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps#install-and-maintain-the-exo-v2-module" }
catch { Write-Error "This script requires the Exchange Online V2 PowerShell module. Learn more about it here: https://docs.microsoft.com/en-us/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps#install-and-maintain-the-exo-v2-module" -ErrorAction Stop }

#Confirm connectivity to Exchange Online
try { Get-EXOMailbox -ResultSize 1 -ErrorAction Stop | Out-Null }
catch {
Expand All @@ -66,15 +65,15 @@ function Get-CalendarPermissionInventory {
else {
$MBList = Get-ExOMailbox -ResultSize Unlimited -RecipientTypeDetails $included | Select-Object -Property Displayname,Identity,PrimarySMTPAddress,RecipientTypeDetails
}

#If no mailboxes are returned from the above cmdlet, stop the script and inform the user
if (!$MBList) { Write-Error "No mailboxes of the specified types were found, specify different criteria." -ErrorAction Stop }

#Once we have the mailbox list, cycle over each mailbox to gather Calendar permissions inventory
$arrPermissions = @()
$count = 1; $PercentComplete = 0;
foreach ($MB in $MBList) {

#Progress message
$ActivityMessage = "Retrieving data for mailbox $($MB.Identity). Please wait..."
$StatusMessage = ("Processing mailbox {0} of {1}: {2}" -f $count, @($MBList).count, $MB.PrimarySmtpAddress.ToString())
Expand Down Expand Up @@ -103,7 +102,7 @@ function Get-CalendarPermissionInventory {
elseif ($entry.User.UserType.ToString() -eq "External") { $varUser = $entry.User.UserPrincipalName.Replace("ExchangePublishedUser.",$null); $varType = "External" }
elseif ($entry.User.UserType.ToString() -eq "Unknown") { $varUser = $entry.User.DisplayName; $varType = "Orphaned" }
else { continue }

Add-Member -InputObject $objPermissions -MemberType NoteProperty -Name "User" -Value $varUser
Add-Member -InputObject $objPermissions -MemberType NoteProperty -Name "Permissions" -Value $($entry.AccessRights -join ";")
Add-Member -InputObject $objPermissions -MemberType NoteProperty -Name "Permission Type" -Value $varType
Expand All @@ -117,5 +116,5 @@ function Get-CalendarPermissionInventory {
}

#Invoke the Get-CalendarPermissionInventory function and pass the command line parameters. Make sure the output is stored in a variable for reuse, even if not specified in the input!
Get-CalendarPermissionInventory @PSBoundParameters -OutVariable global:varPermissions
Get-CalendarPermissionInventory @PSBoundParameters -OutVariable global:varPermissions
$varPermissions | Export-Csv -Path "$((Get-Date).ToString('yyyy-MM-dd_HH-mm-ss'))_CalendarPermissions.csv" -NoTypeInformation -Encoding UTF8 -UseCulture
22 changes: 11 additions & 11 deletions DG_MemberOf_inventory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ function Check-Connectivity {
if (!$session -or ($session.State -ne "Opened")) {
try { $script:session = Get-PSSession -InstanceId (Get-AcceptedDomain | select -First 1).RunspaceId.Guid -ErrorAction Stop }
catch {
try {
$script:session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential (Get-Credential) -Authentication Basic -AllowRedirection -ErrorAction Stop
Import-PSSession $session -ErrorAction Stop | Out-Null
}
try {
$script:session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential (Get-Credential) -Authentication Basic -AllowRedirection -ErrorAction Stop
Import-PSSession $session -ErrorAction Stop | Out-Null
}
catch { Write-Error "No active Exchange Remote PowerShell session detected, please connect first. To connect to ExO: https://technet.microsoft.com/en-us/library/jj984289(v=exchg.160).aspx" -ErrorAction Stop }
}
}
Expand Down Expand Up @@ -43,11 +43,11 @@ function Get-DGMembershipInventory {
#>

[CmdletBinding()]

Param
(
#Specify whether to include user mailboxes in the result
[Switch]$IncludeUserMailboxes,
[Switch]$IncludeUserMailboxes,
#Specify whether to include Shared mailboxes in the result
[Switch]$IncludeSharedMailboxes,
#Specify whether to include Mail users in the result
Expand All @@ -66,7 +66,7 @@ function Get-DGMembershipInventory {
if($IncludeMailUsers) { $included += "MailUser"}
if($IncludeMailContacts) { $included += "MailContact"}
if($IncludeGuestUsers) { $included += "GuestMailUser"}

#Check if we are connected to Exchange PowerShell
if (!(Check-Connectivity)) { return }

Expand All @@ -80,15 +80,15 @@ function Get-DGMembershipInventory {
else {
$MBList = Invoke-Command -Session $session -ScriptBlock { Get-Recipient -ResultSize Unlimited -RecipientTypeDetails $Using:included | Select-Object -Property PrimarySmtpAddress,DistinguishedName,ExternalEmailAddress,ExternalDirectoryObjectId } -HideComputerName
}

#If no users are returned from the above cmdlet, stop the script and inform the user
if (!$MBList) { Write-Error "No users of the specifyied types were found, specify different criteria." -ErrorAction Stop }

#prepare the output
$arrMembers = @(); $count = 1; $PercentComplete = 0;

#cycle over each object from the list
foreach ($mailbox in $MBList) {
foreach ($mailbox in $MBList) {
#display a simple progress message
$ActivityMessage = "Retrieving data for mailbox $($mailbox.PrimarySmtpAddress). Please wait..."
$StatusMessage = ("Processing {0} of {1}: {2}" -f $count, @($MBList).count, $mailbox.DistinguishedName)
Expand All @@ -109,9 +109,9 @@ function Get-DGMembershipInventory {
$objMembers = New-Object PSObject
#$i++;Add-Member -InputObject $objPermissions -MemberType NoteProperty -Name "Number" -Value $i
#Add-Member -InputObject $objMembers -MemberType NoteProperty -Name "User" -Value $mailbox.UserPrincipalName #Not returned via Get-Recipient
if ($mailbox.PrimarySmtpAddress.Address) { Add-Member -InputObject $objMembers -MemberType NoteProperty -Name "Email" -Value $mailbox.PrimarySmtpAddress.ToString() }
if ($mailbox.PrimarySmtpAddress.Address) { Add-Member -InputObject $objMembers -MemberType NoteProperty -Name "Email" -Value $mailbox.PrimarySmtpAddress.ToString() }
elseif ($mailbox.ExternalEmailAddress) { Add-Member -InputObject $objMembers -MemberType NoteProperty -Name "Email" -Value $mailbox.ExternalEmailAddress.AddressString.ToString() }
else { Add-Member -InputObject $objMembers -MemberType NoteProperty -Name "Email" -Value $mailbox.ExternalDirectoryObjectId.ToString() }
else { Add-Member -InputObject $objMembers -MemberType NoteProperty -Name "Email" -Value $mailbox.ExternalDirectoryObjectId.ToString() }
Add-Member -InputObject $objMembers -MemberType NoteProperty -Name "Groups" -Value $($list.PrimarySmtpAddress -join ",")
$arrMembers += $objMembers
}
Expand Down
14 changes: 7 additions & 7 deletions DG_MemberOf_inventoryV2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ function Check-Connectivity {

#Make sure we have a V2 version of the module
try { Get-Command Get-EXOMailbox -ErrorAction Stop | Out-Null }
catch { Write-Error "This script requires the Exchange Online V2/V3 PowerShell module. Learn more about it here: https://docs.microsoft.com/en-us/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps#install-and-maintain-the-exo-v2-module"; return $false }
catch { Write-Error "This script requires the Exchange Online V2/V3 PowerShell module. Learn more about it here: https://docs.microsoft.com/en-us/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps#install-and-maintain-the-exo-v2-module"; return $false }

#Confirm connectivity to Exchange Online
try { Get-EXOMailbox -ResultSize 1 -ErrorAction Stop | Out-Null }
catch {
Expand Down Expand Up @@ -47,7 +47,7 @@ function Get-DGMembershipInventory {
#>

[CmdletBinding()]

Param
(
#Specify whether to include user mailboxes in the result
Expand Down Expand Up @@ -76,7 +76,7 @@ function Get-DGMembershipInventory {
if($IncludeMailContacts) { $included += "MailContact" }
if($IncludeGuestUsers) { $included += "GuestMailUser" }
#if($IncludeUsers) { $included += "User" } #not needed, will mess up the array, separate check below

#Check if we are connected to Exchange PowerShell
if (!(Check-Connectivity)) { return }

Expand All @@ -88,7 +88,7 @@ function Get-DGMembershipInventory {
if ($IncludeUsers -or $IncludeAll) {
$MBList += Get-User -ResultSize Unlimited -RecipientTypeDetails User | Select-Object -Property $props
}

#Cover the rest of the recipient types
if ($IncludeAll) {
$MBList += Get-Recipient -ResultSize Unlimited -RecipientTypeDetails UserMailbox,SharedMailbox,RoomMailbox,EquipmentMailbox,SchedulingMailbox,MailUser,MailContact,GuestMailUser | Select-Object -Property $props
Expand All @@ -99,15 +99,15 @@ function Get-DGMembershipInventory {
else {
$MBList += Get-Recipient -ResultSize Unlimited -RecipientTypeDetails $included | Select-Object -Property $props
}

#If no users are returned from the above cmdlet, stop the script and inform the user
if (!$MBList) { Write-Error "No users of the specifyied types were found, specify different criteria." -ErrorAction Stop }

#prepare the output
$arrMembers = @(); $count = 1; $PercentComplete = 0;

#cycle over each object from the list
foreach ($mailbox in $MBList) {
foreach ($mailbox in $MBList) {
#Display a simple progress message
$ActivityMessage = "Retrieving data for mailbox $($mailbox.PrimarySmtpAddress). Please wait..."
$StatusMessage = ("Processing {0} of {1}: {2}" -f $count, @($MBList).count, $mailbox.DistinguishedName)
Expand Down
Loading

0 comments on commit 74c1a7a

Please sign in to comment.