Azure / Azure/azure-powershell

Az.Resources missing constructor for Get-AzPolicyDefinition

Open
#18,265 2 comments 0 reactions 0 assignees View on GitHub
customer-reported Policy question Service Attention
Dominant language
C#
Stars
4.8k
Forks
4.3k
Avg merge
2d 17h
Merged PRs (30d)
51

Description

### Description

I am creating some Pester tests for a function that we have created to retrieve and remove policy types Assignments, Inititatives and Definitions from a target Management Group.

I have successfully Mocked out the results for Get-AzPolicyAssignment and Get-AzPolicySetDefinition as a PSCustomObject and piped the results to Remove-AzPolicyAssignment and Remove-AzPolicySetDefinition respectively.

When attempting to Mock out the results for Get-AzPolicyDefinition, I simply copied the working PSCustomObject from the working examples above.
The issue is that when I pipe those results into Remove-AzPolicyDefinition I receive the error:

`Exception: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.`

I thought, no problem, I will create the New-Object using the TypeName as I have done for other Pester Mocks but this is where I hit the "A constructor was not found" message.

PS C:\> $obj = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.Policy.PsPolicyDefinition

New-Object : A constructor was not found. Cannot find an appropriate constructor for type Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.Policy.PsPolicyDefinition.
At line:1 char:8
+ $obj = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

I have tried loading the GetAzureRoleAssignmentCommand Constructor but it did not resolve my issue.
https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.management.internal.resources.models.policydefinition.-ctor?view=az-ps-latest

Import-Module "C:\Users\jason\Documents\PowerShell\Modules\Az.Resources\5.6.0\Microsoft.Azure.PowerShell.Cmdlets.ResourceManager.dll"

I get an error stating that "Assembly with the same name is already loaded"

### Script or Debug output

```PowerShell
PS C:\Files\Repo> $DebugPreference = "Continue"
$obj = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.Policy.PsPolicyDefinition -Debug

New-Object:
Line |
2 | $obj = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager. …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| A constructor was not found. Cannot find an appropriate constructor for type Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.Policy.PsPolicyDefinition.
```

### Environment data

```PowerShell
Name Value
---- -----
PSVersion 7.2.4
PSEdition Core
GitCommitId 7.2.4
OS Microsoft Windows 10.0.19043
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
```

### Module versions

```PowerShell
ModuleType Version PreRelease Name
---------- ------- ---------- ----
Script 5.6.0 Az.Resources
```

### Error output

```PowerShell
New-Object: A constructor was not found. Cannot find an appropriate constructor for type Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.Policy.PsPolicyDefinition.
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.