Skip to content

Address PSScriptAnalyzer warnings in PowerShell scripts #295

@willvelida

Description

@willvelida

Context

PR #294 added PSScriptAnalyzer scanning. The first CI run found 9 warnings across 4 scripts. These are code quality warnings, not security issues.

Findings

Rule Severity Script Description
PSUseDeclaredVarsMoreThanAssignments Warning scripts/ui-chat-api-identity/setup-agent-identity.ps1 Variable declared but only assigned, not used elsewhere
PSUseDeclaredVarsMoreThanAssignments Warning scripts/reporting-api-identity/setup-agent-identity.ps1 Same
PSUseDeclaredVarsMoreThanAssignments Warning cosmos-emulator.ps1 Same
PSUseBOMForUnicodeEncodedFile Warning scripts/ui-chat-api-identity/configure-agent-identity.ps1 File should use BOM for Unicode encoding
PSUseBOMForUnicodeEncodedFile Warning scripts/reporting-api-identity/configure-agent-identity.ps1 Same
PSUseBOMForUnicodeEncodedFile Warning scripts/reporting-svc-identity/configure-agent-identity.ps1 Same
PSUseBOMForUnicodeEncodedFile Warning cosmos-emulator.ps1 Same
PSUseShouldProcessForStateChangingFunctions Warning cosmos-emulator.ps1 Functions that change state should support -WhatIf/-Confirm
PSUseShouldProcessForStateChangingFunctions Warning cosmos-emulator.ps1 Same (2 functions)

Suggested Fixes

  1. PSUseDeclaredVarsMoreThanAssignments — Review variables in setup-agent-identity.ps1 scripts and cosmos-emulator.ps1. Remove or use the declared-but-unused variables.

  2. PSUseBOMForUnicodeEncodedFile — Re-save configure-agent-identity.ps1 files and cosmos-emulator.ps1 with UTF-8 BOM encoding.

  3. PSUseShouldProcessForStateChangingFunctions — Add [CmdletBinding(SupportsShouldProcess)] to cosmos-emulator.ps1 functions that start/stop Docker containers, or suppress this rule in PSScriptAnalyzerSettings.psd1.

Alternative

If these warnings are not worth fixing, add them to the exclusion list in PSScriptAnalyzerSettings.psd1:

@{
    Severity     = @('Error', 'Warning')
    ExcludeRules = @(
        'PSAvoidUsingWriteHost'
        'PSUseBOMForUnicodeEncodedFile'
        'PSUseShouldProcessForStateChangingFunctions'
    )
}

References

  • PSScriptAnalyzer workflow: .github/workflows/psscriptanalyzer.yml
  • Settings: PSScriptAnalyzerSettings.psd1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions