Microsoft365DSC icon indicating copy to clipboard operation
Microsoft365DSC copied to clipboard

New-M365DSCResource - help for Graph-resources without any New-xx cmdlet.

Open salbeck-sit opened this issue 2 years ago • 4 comments

Description of the issue

When I try to build a new resource for a Graph-cmdlet - let's say cmdletnoun is MgPolicyCrossTenantAccessPolicyPartnerIdentitySynchronization - then New-M365DSCResource throws an exception For the cmdlet in question there are only Get- and Set- cmdlets. How do I tell that to the DRG ? Or perhaps one or more checks should be included to catch when Get-CommandName returns an error

Microsoft 365 DSC Version

DEV

Which workloads are affected

other

The DSC configuration

No response

Verbose logs showing the problem

PS> New-M365DSCResource -ResourceName AADCrossTenantAccessPolicyPartnerIdentitySynchronization -Workload MicrosoftGraph -CmdLetNoun MgPolicyCrossTenantAccessPolicyPartnerIdentitySynchronization -CmdLetVerb Set

(...)
You cannot call a method on a null-valued expression.
At C:\Users\P-B038744\Documents\Github\Microsoft365DSC\ResourceGenerator\M365DSCResourceGenerator.psm1:196 char:13
+             $repository=$repository.Substring(1,($repository.Length - ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Get-Command : The term 'New-MgPolicyCrossTenantAccessPolicyPartnerIdentitySynchronization' is not recognized as the nam
e of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, v
erify that the path is correct and try again.
At C:\Users\P-B038744\Documents\Github\Microsoft365DSC\ResourceGenerator\M365DSCResourceGenerator.psm1:3043 char:18
+     $newCmdlet = Get-Command "New-$GraphNoun"
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (New-MgPolicyCro...Synchronization:String) [Get-Command], CommandNotFoun
   dException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand

isSyncAllowed
DisplayName
TenantId
isSyncAllowed
userSyncInbound
Get-Command : The term 'New-MgPolicyCrossTenantAccessPolicyPartnerIdentitySynchronization' is not recognized as the nam
e of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, v
erify that the path is correct and try again.
At C:\Users\P-B038744\Documents\Github\Microsoft365DSC\ResourceGenerator\M365DSCResourceGenerator.psm1:424 char:22
+         $newCmdlet = Get-Command -Name "New-$($CmdLetNoun)"
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (New-MgPolicyCro...Synchronization:String) [Get-Command], CommandNotFoun
   dException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand

Environment Information + PowerShell Version

Windows PS 5.1 on Windows Server 2019

salbeck-sit avatar Mar 26 '24 12:03 salbeck-sit

This looks like an update for the DRG to allow either Set or New cmdLets to be used for the generation of new resources. Is there any New- equivalent for MgPolicyCrossTenantAccessPolicyPartnerIdentitySynchronization ?

andikrueger avatar Mar 27 '24 15:03 andikrueger

Not that I know of. It looks like a kind of throwback to the old MSOL-days. We have Get-, Set- and Remove-cmdlets.

salbeck-sit avatar Apr 02 '24 06:04 salbeck-sit

Part of the issue at hand seems to be that one - perhaps both - of the templates that DRG uses to build the module for a new DSC-resource assumes that <NewCmdLetName> must exist for any Graph-resource. For MgPolicyCrossTenantAccessPolicyPartnerIdentitySynchronization, Set- is the equivalent to New- This could be handled in the same way that DRG chooses an updateVerb that is either Update or Set based on cmdlet-availability. I think the same could be achieved if a check is included to see if a New-<cmdletNoun> cmdlet actually exists or else use the chosen updateVerb as the newVerb. If time permits, I'll have a go at this myself.

salbeck-sit avatar Apr 02 '24 06:04 salbeck-sit

A related issue with the DRG is that resources that only provide Get- and Set- but not New- cmdlets may not contain 'variants' of type List and will thus fail to find a 'repository' nor an 'assignment' cmdlet and will then throw an exception. A related issue is that when there's no New- cmdlet then there's no Create parameterset, causing an additional exception later on. I've tried various 'fixes' but something unexpected keeps popping up.

salbeck-sit avatar Apr 03 '24 10:04 salbeck-sit