Skip to content

Add Test-EntraScript to submodules #1454

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

Closed
wants to merge 6 commits into from
Closed
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
110 changes: 110 additions & 0 deletions module/Entra/Microsoft.Entra/Applications/Test-EntraScript.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# ------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
# ------------------------------------------------------------------------------

function Test-EntraScript {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[Alias('FullName', 'Name')]
[string[]]
$Path,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]
$Content,

[switch]
$Quiet
)

begin {
function Test-ScriptCommand {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[Alias('FullName')]
[string]
$Name,

[Parameter(Mandatory = $true)]
[string]
$Content,

[switch]
$Quiet,

[AllowEmptyCollection()]
[string[]]
$RequiredCommands,

[AllowEmptyCollection()]
[string[]]
$ForbiddenCommands
)

$ast = [System.Management.Automation.Language.Parser]::ParseInput($Content, [ref]$null, [ref]$null)
$allCommands = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.CommandAst] }, $true)
$allCommandNames = @($allCommands).ForEach{ $_.CommandElements[0].Value }

$findings = @()
foreach ($command in $allCommands) {
if ($command.CommandElements[0].Value -notin $ForbiddenCommands) { continue }
$findings += [PSCustomObject]@{
PSTypeName = 'Microsoft.Entra.CommandRequirement'
Name = $Name
Line = $command.Extent.StartLineNumber
Type = 'UnsupportedCommand'
Command = $command.CommandElements[0].Value
Code = $command.Extent.Text
}
}
foreach ($requiredCommand in $RequiredCommands) {
if ($requiredCommand -notin $allCommandNames) { continue }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this logic correct? should we continue when a required command is not found?

$findings += [PSCustomObject]@{
PSTypeName = 'Microsoft.Entra.CommandRequirement'
Name = $Name
Line = -1
Type = 'RequiredCommandMissing'
Command = $requiredCommand
Code = ''
}
}

if (-not $Quiet) {
$findings
return
}

$findings -as [bool]
}

$testParam = @{
Quiet = $Quiet
#ForbiddenCommands = $script:MISSING_CMDS # Originally set when using the Compat builder
}
}
process {
if ($Path -and $Content) {
Test-ScriptCommand -Name @($Path)[0] -Content $Content
return
}
foreach ($entry in $Path) {
try { $resolvedPaths = Resolve-Path -Path $entry -ErrorAction Stop }
catch {
Write-Error $_
continue
}

foreach ($resolvedPath in $resolvedPaths) {
if (-not (Test-Path -Path $resolvedPath -PathType Leaf)) {
Write-Warning "Not a file: $resolvedPath"
continue
}

$scriptContent = (Get-Content -LiteralPath $resolvedPath) -join "`n"
Test-ScriptCommand -Name $resolvedPath -Content $scriptContent @testParam
}
}
}
}
110 changes: 110 additions & 0 deletions module/Entra/Microsoft.Entra/Authentication/Test-EntraScript.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# ------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
# ------------------------------------------------------------------------------

function Test-EntraScript {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[Alias('FullName', 'Name')]
[string[]]
$Path,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]
$Content,

[switch]
$Quiet
)

begin {
function Test-ScriptCommand {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[Alias('FullName')]
[string]
$Name,

[Parameter(Mandatory = $true)]
[string]
$Content,

[switch]
$Quiet,

[AllowEmptyCollection()]
[string[]]
$RequiredCommands,

[AllowEmptyCollection()]
[string[]]
$ForbiddenCommands
)

$ast = [System.Management.Automation.Language.Parser]::ParseInput($Content, [ref]$null, [ref]$null)
$allCommands = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.CommandAst] }, $true)
$allCommandNames = @($allCommands).ForEach{ $_.CommandElements[0].Value }

$findings = @()
foreach ($command in $allCommands) {
if ($command.CommandElements[0].Value -notin $ForbiddenCommands) { continue }
$findings += [PSCustomObject]@{
PSTypeName = 'Microsoft.Entra.CommandRequirement'
Name = $Name
Line = $command.Extent.StartLineNumber
Type = 'UnsupportedCommand'
Command = $command.CommandElements[0].Value
Code = $command.Extent.Text
}
}
foreach ($requiredCommand in $RequiredCommands) {
if ($requiredCommand -notin $allCommandNames) { continue }
$findings += [PSCustomObject]@{
PSTypeName = 'Microsoft.Entra.CommandRequirement'
Name = $Name
Line = -1
Type = 'RequiredCommandMissing'
Command = $requiredCommand
Code = ''
}
}

if (-not $Quiet) {
$findings
return
}

$findings -as [bool]
}

$testParam = @{
Quiet = $Quiet
#ForbiddenCommands = $script:MISSING_CMDS # Originally set when using the Compat builder
}
}
process {
if ($Path -and $Content) {
Test-ScriptCommand -Name @($Path)[0] -Content $Content
return
}
foreach ($entry in $Path) {
try { $resolvedPaths = Resolve-Path -Path $entry -ErrorAction Stop }
catch {
Write-Error $_
continue
}

foreach ($resolvedPath in $resolvedPaths) {
if (-not (Test-Path -Path $resolvedPath -PathType Leaf)) {
Write-Warning "Not a file: $resolvedPath"
continue
}

$scriptContent = (Get-Content -LiteralPath $resolvedPath) -join "`n"
Test-ScriptCommand -Name $resolvedPath -Content $scriptContent @testParam
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# ------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
# ------------------------------------------------------------------------------

function Test-EntraScript {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[Alias('FullName', 'Name')]
[string[]]
$Path,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]
$Content,

[switch]
$Quiet
)

begin {
function Test-ScriptCommand {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[Alias('FullName')]
[string]
$Name,

[Parameter(Mandatory = $true)]
[string]
$Content,

[switch]
$Quiet,

[AllowEmptyCollection()]
[string[]]
$RequiredCommands,

[AllowEmptyCollection()]
[string[]]
$ForbiddenCommands
)

$ast = [System.Management.Automation.Language.Parser]::ParseInput($Content, [ref]$null, [ref]$null)
$allCommands = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.CommandAst] }, $true)
$allCommandNames = @($allCommands).ForEach{ $_.CommandElements[0].Value }

$findings = @()
foreach ($command in $allCommands) {
if ($command.CommandElements[0].Value -notin $ForbiddenCommands) { continue }
$findings += [PSCustomObject]@{
PSTypeName = 'Microsoft.Entra.CommandRequirement'
Name = $Name
Line = $command.Extent.StartLineNumber
Type = 'UnsupportedCommand'
Command = $command.CommandElements[0].Value
Code = $command.Extent.Text
}
}
foreach ($requiredCommand in $RequiredCommands) {
if ($requiredCommand -notin $allCommandNames) { continue }
$findings += [PSCustomObject]@{
PSTypeName = 'Microsoft.Entra.CommandRequirement'
Name = $Name
Line = -1
Type = 'RequiredCommandMissing'
Command = $requiredCommand
Code = ''
}
}

if (-not $Quiet) {
$findings
return
}

$findings -as [bool]
}

$testParam = @{
Quiet = $Quiet
#ForbiddenCommands = $script:MISSING_CMDS # Originally set when using the Compat builder
}
}
process {
if ($Path -and $Content) {
Test-ScriptCommand -Name @($Path)[0] -Content $Content
return
}
foreach ($entry in $Path) {
try { $resolvedPaths = Resolve-Path -Path $entry -ErrorAction Stop }
catch {
Write-Error $_
continue
}

foreach ($resolvedPath in $resolvedPaths) {
if (-not (Test-Path -Path $resolvedPath -PathType Leaf)) {
Write-Warning "Not a file: $resolvedPath"
continue
}

$scriptContent = (Get-Content -LiteralPath $resolvedPath) -join "`n"
Test-ScriptCommand -Name $resolvedPath -Content $scriptContent @testParam
}
}
}
}
Loading