PowerPlatform-EnterprisePolicies
CLAUDE.md16 starsChanged 10 months agoMITView on GitHub
PowerShell module and scripts for managing Power Platform Enterprise Policies as Azure resources. Supports: - Customer Managed Key (CMK) policies for encryption - Subnet Injection policies for virtual network delegation - Diagnostic tools for VNET functionality troubleshooting Supports multiple Azure environments: AzureCloud, AzureUSGovernment (DoD, USGovHigh), AzureChinaCloud. Testing requirement: Tests must pass on both PowerShell (Core) and Windows PowerShell. The test command only runs in the current environment, so you must invoke it separately in each: CRITICAL: Always verify that Build…
The file
Show its rules and text
We split this CLAUDE.md into the rules it gives an agent
31 rules. About 0 are common, 12 are distinctive, and 19 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈12
Distinctive · estimate
This author's own choices. Worth a look.
19
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 46
NEVER manually edit docs for public functions: - only edit the comment-based help (.SYNOPSIS, .DESCRIPTION, .OUTPUTS, .EXAMPLE) in the …
- Very specific: line 72
Check if the Az cmdlet is already in Fake-AzModuleFunctions.psm1
- Distinctive: line 73
If not, add a stub function with the required parameters:
- Distinctive: line 118
Do not use these as reference for new code: Follow patterns in the module only.
- Distinctive: line 135
Before creating new helper functions, check if one already exists:
- Very specific: line 162
Exception: Test files (Source/Tests/*.Tests.ps1) do not require this header.
- Distinctive: line 175
Use the Write tool to create the file with the header and code
- Very specific: line 189
Use $ErrorActionPreference = "Stop" at the start of functions
- Very specific: line 194
Use parameter sets: When a function has mutually exclusive behaviors, use ParameterSetName to define them
- Very specific: line 196
Use enums: When a parameter accepts a fixed set of values, use the defined enums (PolicyType, BAPEndpoint, etc.) instead of strings
- Very specific: line 197
Always use named parameters: When calling functions, use -ParameterName $value syntax, not positional arguments
- Very specific: line 198
Validate parameters: Use [ValidateNotNullOrEmpty()] for mandatory string parameters.
- Very specific: line 199
Single authentication call: Call Connect-Azure once at the start of a function, not in each branch
- Distinctive: line 202
Write concise, meaningful tests focused on behavior and error handling
- Distinctive: line 203
Avoid testing implementation details (e.g., "Should call X with correct parameters") - focus on outcomes
- Distinctive: line 204
Avoid redundant tests that exercise the same code path with different inputs
- Very specific: line 205
When using ConvertTo-SecureString -AsPlainText in tests, add the suppression attribute at the top of the file:
- Distinctive: line 231
Create a pull request from your feature branch to main
- Distinctive: line 239
Use these in commit messages to control version bumps:
- Distinctive: line 255
Before submitting a PR, ensure:
- Very specific: line 258
Run Build/build.ps1 -Tasks @("BuildHelp") to regenerate docs
- Distinctive: line 259
Update README.md if adding new user-facing features or parameters
- Very specific: line 261
Create/update manual documentation in docs/en-US/Microsoft.PowerPlatform.EnterprisePolicies/
- Very specific: line 262
Update Build/build.settings.ps1 PostBuildHelp task to include references to new types
- Very specific: line 263
Add new types to the $ExportableTypes array in Types.psm1
- Very specific: line 269
When adding new classes or enums to Private/Types.psm1:
- Distinctive: line 271
Keep types minimal: Only include properties that are actually needed.
- Very specific: line 277
Update build.settings.ps1: Add entries to the $markdownToAppend variable in the PostBuildHelp task to link to your documentation file.
- Very specific: line 281
When creating custom ValidateArgumentsAttribute classes (like ValidateAzureResourceIdAttribute), the type accelerator registration in …
- Very specific: line 335
Use # ClassName as the main heading (not ## ClassName)
- Very specific: line 336
Use ### PropertyName for each property with Type: and Description: lines below
≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project Overview
PowerShell module and scripts for managing Power Platform Enterprise Policies as Azure resources. Supports:
- Customer Managed Key (CMK) policies for encryption
- Subnet Injection policies for virtual network delegation
- Diagnostic tools for VNET functionality troubleshooting
Supports multiple Azure environments: AzureCloud, AzureUSGovernment (DoD, USGovHigh), AzureChinaCloud.
Build Commands
# Restore NuGet packages (required before building/testing)
dotnet restore
# Build the module to Release directory
Build/build.ps1 -Tasks @("Build")
# Run tests (in current PowerShell environment)
Build/build.ps1 -Tasks @("Test")
# Build and test together
Build/build.ps1 -Tasks @("Build", "Test")
# Generate markdown documentation (updates docs/ folder)
Build/build.ps1 -Tasks @("BuildHelp")Testing requirement: Tests must pass on both PowerShell (Core) and Windows PowerShell. The test command only runs in the current environment, so you must invoke it separately in each:
# Run in Windows PowerShell
powershell -Command "Build/build.ps1 -Tasks @('Test')"
# Run in PowerShell Core
pwsh -Command "Build/build.ps1 -Tasks @('Test')"CRITICAL: Always verify that Build Succeeded! appears in the output after running any build task. If the build fails or errors occur, do not proceed - investigate and fix the issue first. Build failures can leave files in an incomplete state (e.g., BuildHelp may not append the Types section to docs if it fails mid-task).
Important: The docs/ folder contains autogenerated documentation. After any changes to module scripts, run .\Build\build.ps1 -Tasks @("BuildHelp") to regenerate the docs.
NEVER manually edit docs for public functions - only edit the comment-based help (.SYNOPSIS, .DESCRIPTION, .OUTPUTS, .EXAMPLE) in the script file itself, then run BuildHelp. Manual edits will be overwritten.
Exception: Types in Private/Types.psm1 (classes and enums) cannot be autogenerated and require manual documentation updates in docs/.
Note: Non-Microsoft contributors must modify Nuget.config to point to the public NuGet repository.
Running Tests
Tests use Pester loaded from NuGet packages (version defined in Directory.Packages.props). The test framework validates:
- Module structure and manifest
- Public/Private functions
- File headers (Microsoft sample code disclaimer required)
# Load test environment in VSCode (Run and Debug > "Load Modules")
# Or manually:
. Source/Tests/Shared.ps1
# Tests are in Source/Tests/*.Tests.ps1
# Mock Azure module: Source/Tests/FakeAzModule/FakeAZ.psd1Fake Az Module for Testing
The tests use a fake Az module (Source/Tests/FakeAzModule/) instead of the real Az module. When adding new cmdlets that call Az cmdlets:
- Check if the Az cmdlet is already in
Fake-AzModuleFunctions.psm1 - If not, add a stub function with the required parameters:
function Remove-AzResource { param( [string]$ResourceId, [switch]$Force ) } - In tests, mock the function with
-ModuleName "Microsoft.PowerPlatform.EnterprisePolicies"
Architecture
Module Structure
Source/Microsoft.PowerPlatform.EnterprisePolicies/
├── Public/
│ └── SubnetInjection/
│ ├── New-SubnetInjectionEnterprisePolicy.ps1 # Create subnet injection policy
│ ├── Get-SubnetInjectionEnterprisePolicy.ps1 # Retrieve subnet injection policies
│ ├── Remove-SubnetInjectionEnterprisePolicy.ps1 # Remove subnet injection policy
│ ├── New-VnetForSubnetDelegation.ps1 # VNet setup cmdlet
│ └── Diagnostics/ # Diagnostic cmdlets
│ ├── Get-EnvironmentUsage.ps1
│ ├── Get-EnvironmentHistoricalUsage.ps1
│ ├── Get-EnvironmentRegion.ps1
│ ├── Test-AccountPermissions.ps1
│ ├── Test-DnsResolution.ps1
│ └── Test-NetworkConnectivity.ps1
└── Private/ # Internal implementation
├── Types.psm1 # Enums and data classes
├── AuthenticationOperations.ps1 # Azure authentication, MSAL client caching
├── EnvironmentOperations.ps1 # BAP environment operations
├── RESTHelpers.ps1 # HTTP client with retry logic, BAP API endpoints
├── AzHelper.ps1 # Azure resource operations
├── CacheMethods.ps1 # Response caching
└── VnetValidations.ps1 # Network validationLegacy Scripts (DO NOT follow these patterns)
Everything under Source/ outside the module and tests is legacy code being replaced:
Source/Cmk/- Legacy CMK scriptsSource/SubnetInjection/- Legacy subnet injection scriptsSource/Common/- Legacy shared helpers
Do not use these as reference for new code. Follow patterns in the module only.
Source/Tests/ is NOT legacy - it contains the Pester tests for the module.
Key Types (Private/Types.psm1)
BAPEndpointenum: tip1, tip2, prod, usgovhigh, dod, chinaAzureEnvironmentenum: AzureCloud, AzureChinaCloud, AzureUSGovernmentPolicyTypeenum: Encryption, NetworkInjection, Identity- Data classes:
NetworkUsage,VnetInformation,EnvironmentNetworkUsageDocument
REST API Pattern
The module uses RESTHelpers.ps1 for all BAP (Business Application Platform) API calls:
- Singleton
HttpClientwith User-Agent header - Retry logic with
Retry-Afterheader support (429/503 responses) - Environment-specific endpoints via
Get-BAPEndpointUrl
Existing Utility Functions
Before creating new helper functions, check if one already exists:
ConvertFrom-JsonToClass(RESTHelpers.ps1): Converts JSON to typed classes. Use this instead of creating custom converters.Get-BAPEndpointUrl(RESTHelpers.ps1): Gets BAP API endpoint URLs for a given endpointGet-BAPResourceUrl(RESTHelpers.ps1): Gets BAP resource/audience URLs for token acquisitionConnect-Azure(AuthenticationOperations.ps1): Handles Azure authentication with endpoint mapping
Authentication Flow
AuthenticationOperations.ps1 handles Azure authentication:
- Reuses existing
AzContextwhen available - Maps
BAPEndpointtoAzureEnvironmentfor login - Supports tenant-specific authentication with
-TenantId
Code Conventions
Required File Header
All .ps1 files in the module (Source/Microsoft.PowerPlatform.EnterprisePolicies/) must start with this Microsoft sample code disclaimer:
<#
SAMPLE CODE NOTICE
THIS SAMPLE CODE IS MADE AVAILABLE AS IS. MICROSOFT MAKES NO WARRANTIES, WHETHER EXPRESS OR IMPLIED,
OF FITNESS FOR A PARTICULAR PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OR CONDITIONS OF MERCHANTABILITY.
THE ENTIRE RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS SAMPLE CODE REMAINS WITH THE USER.
NO TECHNICAL SUPPORT IS PROVIDED. YOU MAY NOT DISTRIBUTE THIS CODE UNLESS YOU HAVE A LICENSE AGREEMENT WITH MICROSOFT THAT ALLOWS YOU TO DO SO.
#>Exception: Test files (Source/Tests/*.Tests.ps1) do not require this header.
Creating New PowerShell Files (CRLF Line Endings)
CRITICAL: The Write tool creates files with LF line endings, but the header disclaimer test expects CRLF (\r\n). After creating any new .ps1 file in the module, you MUST fix the line endings by running:
Set-Content -Path 'path/to/file.ps1' -Value (Get-Content 'path/to/file.ps1')This reads the file and writes it back with Windows CRLF line endings. Without this step, the "Contains header disclaimer file" test will fail.
Example workflow for creating a new cmdlet:
- Use the Write tool to create the file with the header and code
- Immediately run the
Set-Contentcommand to fix line endings - Then run tests
Common Parameters for Public Cmdlets
Public cmdlets that call Azure/BAP APIs should include these common parameters:
TenantId- Optional Azure AD tenant IDEndpoint- BAP endpoint (default:[BAPEndpoint]::Prod)ForceAuth- Switch to force re-authentication instead of reusing existing session
General Conventions
- Public functions go in
Public/subdirectories, private inPrivate/ - Each public/private function should have a corresponding test file
- Functions use advanced parameter validation with
CmdletBinding - Use
$ErrorActionPreference = "Stop"at the start of functions - In
HelpMessageattributes, do not enumerate specific enum values (e.g., don't say "AzureCloud, AzureUSGovernment, AzureChinaCloud") - enum values may change over time
PowerShell Best Practices
- Avoid code duplication: Consolidate similar functions into one using parameter sets
- Use parameter sets: When a function has mutually exclusive behaviors, use
ParameterSetNameto define them - Use splatting: Build
$paramshashtable and call cmdlets with@paramsinstead of long parameter lists - Use enums: When a parameter accepts a fixed set of values, use the defined enums (
PolicyType,BAPEndpoint, etc.) instead of strings - Always use named parameters: When calling functions, use
-ParameterName $valuesyntax, not positional arguments - Validate parameters: Use
[ValidateNotNullOrEmpty()]for mandatory string parameters; use[string]::IsNullOrWhiteSpace()for optional parameters when building splatted calls - Single authentication call: Call
Connect-Azureonce at the start of a function, not in each branch
Testing Conventions
- Write concise, meaningful tests focused on behavior and error handling
- Avoid testing implementation details (e.g., "Should call X with correct parameters") - focus on outcomes
- Avoid redundant tests that exercise the same code path with different inputs
- When using
ConvertTo-SecureString -AsPlainTextin tests, add the suppression attribute at the top of the file:[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Justification="Unit test code")] param() - Testing internal types: Custom types defined in
Private/Types.psm1aren't directly accessible in tests. UseInModuleScopeto access them:It 'Should validate resource ID' { InModuleScope "Microsoft.PowerPlatform.EnterprisePolicies" { $attribute = [ValidateAzureResourceIdAttribute]::new("Microsoft.PowerPlatform/enterprisePolicies") { $attribute.Validate($resourceId, $null) } | Should -Not -Throw } }
Dependencies
Required PowerShell modules (installed via InstallPowerAppsCmdlets.ps1):
- Az.Accounts, Az.Network, Az.Resources, Az.PowerPlatform
- Pester (via NuGet for testing, version in
Directory.Packages.props)
Git Workflow
- Never push directly to main - the repository requires pull requests
- Always create a feature branch with the naming pattern:
user-alias/relevant-branch-name- Example:
osfaixat/update-environmentregion-api
- Example:
- Create a pull request from your feature branch to main
CI/CD
Azure Pipelines with security scanning (CodeQL, CredScan, BinSkim, PoliCheck). Version managed by GitVersion with semantic versioning.
Commit Message Versioning
Use these in commit messages to control version bumps:
+semver:major(suffix) - Breaking API changes or milestone accomplishments (use sparingly)+semver:minor(suffix) - New public cmdlets or significant behavior changes via private functions+semver:patch(suffix) - Small bug fixes (default if no suffix)[skip ci](prefix) - Changes that don't affect the module (CLAUDE.md, README.md, docs, legacy scripts)
Examples:
Add Get-SubnetInjectionEnterprisePolicy cmdlet +semver:minor
Fix null reference in error handling +semver:patch
[skip ci] Update CLAUDE.md with coding guidelinesPR Checklist
Before submitting a PR, ensure:
- If module code changed (
Source/Microsoft.PowerPlatform.EnterprisePolicies/):- Tests pass in both PowerShell Core (
pwsh) and Windows PowerShell (powershell) - Run
Build/build.ps1 -Tasks @("BuildHelp")to regenerate docs
- Tests pass in both PowerShell Core (
- Update
README.mdif adding new user-facing features or parameters - If modifying
Types.psm1:- Create/update manual documentation in
docs/en-US/Microsoft.PowerPlatform.EnterprisePolicies/ - Update
Build/build.settings.ps1PostBuildHelp task to include references to new types - Add new types to the
$ExportableTypesarray in Types.psm1
- Create/update manual documentation in
Note: Tests are not required for changes to legacy scripts, README.md, CLAUDE.md, or other non-module files.
Adding New Types
When adding new classes or enums to Private/Types.psm1:
- Keep types minimal: Only include properties that are actually needed. Don't add every possible property from an API response - include only what consumers will use.
- Add to $ExportableTypes: New types must be added to the
$ExportableTypesarray at the bottom of Types.psm1 to be exported. - Create manual documentation: Types cannot be auto-documented. Create a markdown file in
docs/en-US/Microsoft.PowerPlatform.EnterprisePolicies/following the manual type documentation format (see below). - Update build.settings.ps1: Add entries to the
$markdownToAppendvariable in thePostBuildHelptask to link to your documentation file.
Custom Validation Attributes
When creating custom ValidateArgumentsAttribute classes (like ValidateAzureResourceIdAttribute), the type accelerator registration in Types.psm1 automatically registers both:
- The full name (e.g.,
ValidateAzureResourceIdAttribute) - The short name without "Attribute" suffix (e.g.,
ValidateAzureResourceId)
This allows using [ValidateAzureResourceId("...")] syntax in parameter declarations. The registration logic handles this automatically for any type ending in "Attribute".
Manual Type Documentation Format
CRITICAL: Manual type docs use a different format than autogenerated cmdlet docs. Using the wrong format will break the build.
Key metadata differences:
- Autogenerated cmdlet docs:
document type: cmdlet, noautogeneratedfield - Manual type docs:
document type: class(orenum), must includeautogenerated: false
Required structure for classes:
---
document type: class
external help file: Microsoft.PowerPlatform.EnterprisePolicies-Help.xml
HelpUri: ''
Locale: en-US
Module Name: Microsoft.PowerPlatform.EnterprisePolicies
ms.date: MM/DD/YYYY
PlatyPS schema version: 2024-05-01
title: ClassName
autogenerated: false
---
# ClassName
## DESCRIPTION
The `ClassName` class represents...
## PROPERTIES
### PropertyName
Type: `System.String`
Description: Description of the property.
## METHODS
This class does not define any methods.
## EXAMPLES
```powershell
$obj = [ClassName]::new()
$obj.PropertyName = "value"
**Important notes:**
- Use `# ClassName` as the main heading (not `## ClassName`)
- Use `### PropertyName` for each property with `Type:` and `Description:` lines below
- Examples must show **PowerShell class initialization**, not JSON
- Multiple related classes can be in one file, separated by `---` horizontal rules
- See `docs/en-US/Microsoft.PowerPlatform.EnterprisePolicies/NetworkUsage.md` for a reference exampleDoes it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

