[Az.Resources 5.3.0] New-AzADServicePrincipal throws error 'cannot call method on a null-valued expression'
Description
Error when running:
New-AzADServicePrincipal -DisplayName $name
New-AzADServicePrincipal: You cannot call a method on a null-valued expression.
Verified with 5.2.0 (previous version) that this works.
Issue script & Debug output
PS /home/matsest> $DebugPreference='Continue'
PS /home/matsest> $newSp = New-AzADServicePrincipal -DisplayName "mats-est-test2"
New-AzADServicePrincipal: You cannot call a method on a null-valued expression.
Environment data
PS /home/matsest> $PSVersionTable
Name Value
---- -----
PSVersion 7.2.1
PSEdition Core
GitCommitId 7.2.1
OS Linux 5.10.60.1-microsoft-standard-WSL2 #1 SMP Wed Aug 25 23:20:18 UTC 2021
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Module versions
PS /home/matsest> get-module az*
ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Script 2.7.2 Az.Accounts {Add-AzEnvironment, Clear-AzContext, Clear-AzDefault, Connect-AzAccount…}
Script 5.3.0 Az.Resources {Export-AzResourceGroup, Export-AzTemplateSpec, Get-AzDenyAssignment, Get-AzDeployment…}
Error output
PS /home/matsest> resolve-azerror
DEBUG: Sought all Az modules and got latest version 0.0.0
DEBUG: 1:37:21 PM - ResolveError begin processing with ParameterSet 'AnyErrorParameterSet'.
DEBUG: 1:37:21 PM - using account id '[email protected]'...
WARNING: Upcoming breaking changes in the cmdlet 'Resolve-AzError' :
The `Resolve-Error` alias will be removed in a future release. Please change any scripts that use this alias to use `Resolve-AzError` instead.
Note : Go to https://aka.ms/azps-changewarnings for steps to suppress this breaking change warning, and other information on breaking changes in Azure PowerShell.
HistoryId: -1
Message : You cannot call a method on a null-valued expression.
StackTrace : at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
at System.Management.Automation.PSScriptCmdlet.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
at System.Management.Automation.PSScriptCmdlet.DoProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()
Exception : System.Management.Automation.RuntimeException
InvocationInfo : {}
Line : $param['EndDate'] = $PSBoundParameters['StartDate'].AddYears(1)
Position : At /home/matsest/.local/share/powershell/Modules/Az.Resources/5.3.0/MSGraph.Autorest/custom/New-AzADServicePrincipal.ps1:749 char:11
+ $param['EndDate'] = $PSBoundParameters['StartDate'].AddYear …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId : -1
Message : You cannot call a method on a null-valued expression.
StackTrace : at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
at System.Management.Automation.PSScriptCmdlet.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
at System.Management.Automation.PSScriptCmdlet.DoProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()
Exception : System.Management.Automation.RuntimeException
InvocationInfo : {}
Line : $param['EndDate'] = $PSBoundParameters['StartDate'].AddYears(1)
Position : At /home/matsest/.local/share/powershell/Modules/Az.Resources/5.3.0/MSGraph.Autorest/custom/New-AzADServicePrincipal.ps1:749 char:11
+ $param['EndDate'] = $PSBoundParameters['StartDate'].AddYear …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId : -1
The Azure PowerShell team is listening, please let us know how we are doing: https://aka.ms/azpssurvey?Q_CHL=ERROR.
DEBUG: AzureQoSEvent: Module: Az.Accounts:2.7.2; CommandName: Resolve-AzError; PSVersion: 7.2.1; IsSuccess: True; Duration: 00:00:00.0309544
DEBUG: Finish sending metric.
DEBUG: 1:37:22 PM - ResolveError end processing.
Looks like the issue from Resolve-AzError lists that the $PSBoundParameters['StartDate'] cannot be found.
Looking at #16924 it looks like the logic introduced with $param['StartDate'] and $PSBoundParameters['StartDate'] is nonconsistent and causes a reference to a null expression when the StartDate and EndDate is not set @veryearly @wyunchi-ms
Yeah, a different but related error occurs when you -ApplicationId or -ApplicationObject params as well.
It is throwing
New-AzADServicePrincipal: A parameter cannot be found that matches parameter name 'ApplicationId'.
@gautamdsheth Yes, it looks related to the same change to New-AzAdServicePrincipal.
Get-AzADApplication -IdentifierUri also doenst return the app registration anymore. It errors out with
Az.Resources\5.3.0\MSGraph.Autorest\internal\ProxyCmdletDefinitions.ps1:874:23 Line | 874 | $scriptCmd = {& $wrappedCmd @PSBoundParameters} | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Invalid filter clause
Hi @matsest @gautamdsheth ,
Issue has been fixed in https://www.powershellgallery.com/packages/Az.Resources/5.3.1, thanks for reporting this bug.
Get-AzADApplication -IdentifierUri also doenst return the app registration anymore. It errors out with
Az.Resources\5.3.0\MSGraph.Autorest\internal\ProxyCmdletDefinitions.ps1:874:23 Line | 874 | $scriptCmd = {& $wrappedCmd @PSBoundParameters} | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Invalid filter clause
Hi @cvs79 , This issue is fixed in https://www.powershellgallery.com/packages/Az.Resources/5.3.1, please try this latest version. Thanks for reporting.
Use Install-Module -Name Az.Resources to update the latest Az.Resources
I am running into this same error using CloudShell in Azure Gov.