Skip to content

Commit 6cfcc31

Browse files
authored
feat: implement ps script analyzer fixes (#444)
* implement ps script analyzer fixes * add script analyzer to build stage * suppress warning * $null should be on the left side of equality comparisons * minor typo fixes * don't use `Diagnostics.CodeAnalysis.SuppressMessageAttribute` but use `write-debug`
1 parent bafe780 commit 6cfcc31

39 files changed

Lines changed: 124 additions & 106 deletions

File tree

build/ci-build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ stages:
8282
vmImage: '$(Vm.Linux.Image)'
8383
steps:
8484
- template: 'templates/replace-tokens.yml'
85+
- powershell: Invoke-ScriptAnalyzer -Path ./src -Recurse -Settings ./powershell-psscriptanalyzer.psd1 -ReportSummary -EnableExit
86+
displayName: 'Analyze scripts'
8587
- task: CopyFiles@2
8688
displayName: 'Copy build artifacts'
8789
inputs:

docs/preview/02-Guidelines/setting-arm-outputs-to-azure-devops-variable-group.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ In ARM and Bicep templates it is possible to specify [output parameters](https:/
99

1010
To enable maximum re-use of these output parameters within your environment we developed [this script](https://scripting.arcus-azure.net/Features/powershell/azure-devops#setting-arm-outputs-to-azure-devops-variable-group) which is available in the `Arcus.Scripting.DevOps` PowerShell module. It allows you to store those output parameters in an Azure DevOps variable group. This helps you in making sure certain parameters are available throughout your Azure DevOps environment.
1111

12-
For example, think of a use-case where your vital infrastructure components are deployed in a seperate Azure DevOps pipeline and need to be referenced from other components. Storing the necessary information such as identifiers, locations or names of these components in an Azure DevOps variable group allows you to easily use these values from other components.
12+
For example, think of a use-case where your vital infrastructure components are deployed in a separate Azure DevOps pipeline and need to be referenced from other components. Storing the necessary information such as identifiers, locations or names of these components in an Azure DevOps variable group allows you to easily use these values from other components.
1313

1414
## Example
1515
### Specify Output Parameters

docs/preview/03-Features/powershell/azure-api-management.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Sign-up or invite a new user in an existing Azure API Management instance.
108108
| `UserId` | no | The UserId that will be used to create the user |
109109
| `Password` | no | The password that the user will be able to login with |
110110
| `Note` | no | A note that will be added to the user |
111-
| `SendNotification` | no | Wether or not a notification will be sent to the email address of the user |
111+
| `SendNotification` | no | Whether or not a notification will be sent to the email address of the user |
112112
| `ConfirmationType` | no | The confirmation type that will be used when creating the user, this can be `invite` (default) or `signup` |
113113
| `ApiVersion` | no | The version of the management API to be used. (default: `2021-08-01`) |
114114
| `SubscriptionId` | no | The Id of the subscription containing the Azure API Management instance. When not provided, it will be retrieved from the current context (Get-AzContext). |

powershell-psscriptanalyzer.psd1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@{
2+
Severity=@('Error','Warning')
3+
ExcludeRules=@('PSAvoidUsingWriteHost',
4+
'PSUseToExportFieldsInManifest',
5+
'PSAvoidDefaultValueForMandatoryParameter',
6+
'PSUseApprovedVerbs',
7+
'PSUseSingularNouns',
8+
'PSUseShouldProcessForStateChangingFunctions',
9+
'PSUseDeclaredVarsMoreThanAssignments',
10+
'PSAvoidUsingPlainTextForPassword',
11+
'PSAvoidUsingUsernameAndPasswordParams',
12+
'PSReviewUnusedParameter',
13+
'PSAvoidUsingConvertToSecureStringWithPlainText'
14+
'PSAvoidGlobalVars')
15+
}

src/Arcus.Scripting.ARM/Scripts/Inject-ArmContent.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<#
1+
<#
22
Possible injection instructions in ARM templates or recursively referenced files:
33
44
${ fileToInject.xml }
@@ -40,7 +40,7 @@ function InjectFile {
4040
param($match)
4141

4242
$completeInjectionInstruction = $match.Groups[1].Value;
43-
$instructionParts = @($completeInjectionInstruction -split "," | foreach { $_.Trim() } )
43+
$instructionParts = @($completeInjectionInstruction -split "," | ForEach-Object { $_.Trim() } )
4444

4545
$filePart = $instructionParts[0];
4646
# Regex uses non-capturing group for 'FileToInject' part,
@@ -70,7 +70,7 @@ function InjectFile {
7070
$surroundContentWithDoubleQuotes = $match.Value.StartsWith('"') -and $match.Value.EndsWith('"')
7171

7272
if ($instructionParts.Length -gt 1) {
73-
$optionParts = $instructionParts | select -Skip 1
73+
$optionParts = $instructionParts | Select-Object -Skip 1
7474

7575
if ($optionParts.Contains("ReplaceSpecialChars")) {
7676
Write-Verbose "`t Replacing special characters"

src/Arcus.Scripting.ActiveDirectory/Scripts/List-AzADAppRoleAssignments.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ try {
3939
if ($appRoleAssignments) {
4040
foreach ($serviceAppRoleAssignment in $appRoleAssignments) {
4141
$servicePrincipal = Get-AzADServicePrincipal -ObjectId $serviceAppRoleAssignment.PrincipalId
42-
if ($servicePrincipal -ne $null) {
42+
if ($null -ne $servicePrincipal) {
4343
Write-Host "Role '$($appRole.Value)' is assigned to the Active Directory Application '$($serviceAppRoleAssignment.PrincipalDisplayName)' with ID '$($servicePrincipal.AppId)'" -ForegroundColor Green
4444
}
4545
}

src/Arcus.Scripting.ApiManagement/Arcus.Scripting.ApiManagement.psm1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ function Backup-AzApiManagementService {
5252
)
5353

5454
if ($PassThru) {
55-
. $PSScriptRoot\Scripts\Backup-AzApiManagementService.ps1 -ResourceGroupName $ResourceGroupName -StorageAccountResourceGroupName $StorageAccountResourceGroupName -StorageAccountName $StorageAccountName -ServiceName $ServiceName -ContainerName $ContainerName -AccessType $AccessType -IdentityClientId $IdentityClientId -BlobName $BlobName -PassThru
55+
. $PSScriptRoot\Scripts\Backup-AzApiManagementService.ps1 -ResourceGroupName $ResourceGroupName -StorageAccountResourceGroupName $StorageAccountResourceGroupName -StorageAccountName $StorageAccountName -ServiceName $ServiceName -ContainerName $ContainerName -AccessType $AccessType -IdentityClientId $IdentityClientId -BlobName $BlobName -DefaultProfile $DefaultProfile -PassThru
5656
} else {
57-
. $PSScriptRoot\Scripts\Backup-AzApiManagementService.ps1 -ResourceGroupName $ResourceGroupName -StorageAccountResourceGroupName $StorageAccountResourceGroupName -StorageAccountName $StorageAccountName -ServiceName $ServiceName -ContainerName $ContainerName -AccessType $AccessType -IdentityClientId $IdentityClientId -BlobName $BlobName
57+
. $PSScriptRoot\Scripts\Backup-AzApiManagementService.ps1 -ResourceGroupName $ResourceGroupName -StorageAccountResourceGroupName $StorageAccountResourceGroupName -StorageAccountName $StorageAccountName -ServiceName $ServiceName -ContainerName $ContainerName -AccessType $AccessType -IdentityClientId $IdentityClientId -BlobName $BlobName -DefaultProfile $DefaultProfile
5858
}
5959
}
6060

@@ -264,7 +264,7 @@ function Remove-AzApiManagementUserAccount {
264264
[string][parameter(Mandatory = $false)] $AccessToken
265265
)
266266

267-
. $PSScriptRoot\Scripts\Remove-AzApiManagementUserAccount.ps1 -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName -MailAddress $MailAddress
267+
. $PSScriptRoot\Scripts\Remove-AzApiManagementUserAccount.ps1 -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName -MailAddress $MailAddress -SubscriptionId $SubscriptionId -AccessToken $AccessToken
268268

269269
}
270270

@@ -438,9 +438,9 @@ function Restore-AzApiManagementService {
438438
)
439439

440440
if ($PassThru) {
441-
. $PSScriptRoot\Scripts\Restore-AzApiManagementService.ps1 -ResourceGroupName $ResourceGroupName -StorageAccountResourceGroupName $StorageAccountResourceGroupName -StorageAccountName $StorageAccountName -ServiceName $ServiceName -ContainerName $ContainerName -BlobName $BlobName -PassThru
441+
. $PSScriptRoot\Scripts\Restore-AzApiManagementService.ps1 -ResourceGroupName $ResourceGroupName -StorageAccountResourceGroupName $StorageAccountResourceGroupName -StorageAccountName $StorageAccountName -ServiceName $ServiceName -ContainerName $ContainerName -BlobName $BlobName -DefaultProfile $DefaultProfile -PassThru
442442
} else {
443-
. $PSScriptRoot\Scripts\Restore-AzApiManagementService.ps1 -ResourceGroupName $ResourceGroupName -StorageAccountResourceGroupName $StorageAccountResourceGroupName -StorageAccountName $StorageAccountName -ServiceName $ServiceName -ContainerName $ContainerName -BlobName $BlobName
443+
. $PSScriptRoot\Scripts\Restore-AzApiManagementService.ps1 -ResourceGroupName $ResourceGroupName -StorageAccountResourceGroupName $StorageAccountResourceGroupName -StorageAccountName $StorageAccountName -ServiceName $ServiceName -ContainerName $ContainerName -BlobName $BlobName -DefaultProfile $DefaultProfile
444444
}
445445
}
446446

src/Arcus.Scripting.ApiManagement/Scripts/Backup-AzApiManagementService.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
param(
1+
param(
22
[Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Resource group name is required"),
33
[Parameter(Mandatory = $true)][string] $StorageAccountResourceGroupName = $(throw = "Resource group for storage account is required"),
44
[Parameter(Mandatory = $true)][string] $StorageAccountName = $(throw "Storage account name is required"),
@@ -18,7 +18,7 @@ if ($AccessType -eq 'UserAssignedManagedIdentity' -and $IdentityClientId -eq "")
1818
Write-Verbose "Getting Azure storage account key for storage account '$($StorageAccountName)' in resource group '$($StorageAccountResourceGroupName)'..."
1919
$storageKeys = Get-AzStorageAccountKey -ResourceGroupName $StorageAccountResourceGroupName -StorageAccountName $StorageAccountName
2020

21-
if ($storageKeys -eq $null -or $storageKeys.count -eq 0) {
21+
if ($null -eq $storageKeys -or $storageKeys.count -eq 0) {
2222
Write-Error "Cannot backup API Management service because no access keys found for storage account '$StorageAccountName' in resource group '$($StorageAccountResourceGroupName)'"
2323
} else {
2424
Write-Host "Got Azure storage key for storage account '$($StorageAccountName)' in resource group '$($StorageAccountResourceGroupName)'!" -ForegroundColor Green

src/Arcus.Scripting.ApiManagement/Scripts/Create-AzApiManagementApiOperation.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
param(
1+
param(
22
[Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Resource group is required"),
33
[Parameter(Mandatory = $true)][string] $ServiceName = $(throw "API management service name is required"),
44
[Parameter(Mandatory = $true)][string] $ApiId = $(throw "API ID is required"),
@@ -11,7 +11,7 @@ param(
1111
)
1212

1313
$apim = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
14-
if ($apim -eq $null) {
14+
if ($null -eq $apim) {
1515
throw "Unable to find the Azure API Management instance '$ServiceName' in resource group '$ResourceGroupName'"
1616
}
1717
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName

src/Arcus.Scripting.ApiManagement/Scripts/Create-AzApiManagementUserAccount.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
param(
1+
param(
22
[string][Parameter(Mandatory = $true)] $ResourceGroupName = $(throw "Resource group name is required"),
33
[string][parameter(Mandatory = $true)] $ServiceName = $(throw "API management service name is required"),
44
[string][parameter(Mandatory = $true)] $FirstName = $(throw "The first name of the user is required"),
@@ -15,7 +15,7 @@ param(
1515
)
1616

1717
$apim = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
18-
if ($apim -eq $null) {
18+
if ($null -eq $apim) {
1919
throw "Unable to find the Azure API Management instance '$ServiceName' in resource group '$ResourceGroupName'"
2020
}
2121

0 commit comments

Comments
 (0)