microsoft / Partner-Center-PowerShell

PowerShell module for managing Partner Center resources.
https://docs.microsoft.com/powershell/partnercenter/
MIT License
130 stars 59 forks source link

Subsequent calls to New-PartnerAccessToken cause exception in Azure Functions GetSteppablePipeline #376

Open codepend opened 2 years ago

codepend commented 2 years ago

I have a Storage Queue triggered Azure Function which performs perfectly the first time it is triggered, but subsequent triggers are met with an error when calling the New-PartnerAccessToken.

`

Write out the queue message and insertion time to the information log.

Write-Host "PowerShell queue trigger function processed work item: $QueueItem"

Import-Module MSOnline -UseWindowsPowershell Import-Module PartnerCenter -UseWindowsPowershell Import-Module Az.Storage Import-Module AzureAD -UseWindowsPowershell

$ApplicationId = $ENV:ApplicationID $ApplicationSecret = $ENV:ApplicationSecret $secPas = $ApplicationSecret| ConvertTo-SecureString -AsPlainText -Force $tenantID = $ENV:TenantID $RefreshToken = $ENV:RefreshToken $ExchangeRefreshToken = $ENV:ExchangeRefreshToken

$credential = New-Object System.Management.Automation.PSCredential($ApplicationId, $secPas)

$aadGraphToken = New-PartnerAccessToken -ApplicationId $ApplicationId -Credential $credential -RefreshToken $refreshToken -Scopes 'https://graph.windows.net/.default' -ServicePrincipal -Tenant $tenantID $graphToken = New-PartnerAccessToken -ApplicationId $ApplicationId -Credential $credential -RefreshToken $refreshToken -Scopes 'https://graph.microsoft.com/.default' -ServicePrincipal -Tenant $tenantID ` It fails immediately on the $aadGraphToken line with the following error:

Exception calling "GetSteppablePipeline" with "1" argument(s): "The parameter is incorrect. "Exception :Type : System.Management.Automation.ParentContainsErrorRecordExceptionMessage : Exception calling "GetSteppablePipeline" with "1" argument(s): "The parameter is incorrect. "HResult : -2146233087CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordExceptionFullyQualifiedErrorId : CmdletInvocationExceptionInvocationInfo :ScriptLineNumber : 8171OffsetInLine : 13HistoryId : -1ScriptName : C:\local\Temp\remoteIpMoProxy_PartnerCenter_3.0.10_localhost_7a01bbfe-eae9-4f3a-974e-953f2e958b13\remoteIpMoProxy_PartnerCenter_3.0.10_localhost_7a01bbfe-eae9-4f3a-974e-953f2e958b13.psm1Line : $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)PositionMessage : At C:\local\Temp\remoteIpMoProxy_PartnerCenter_3.0.10_localhost_7a01bbfe-eae9-4f3a-974e-953f2e958b13\remoteIpMoProxy_PartnerCenter_3.0.10_localhost_7a01bbfe-eae9-4f3a-974e-953f2e958b13.psm1:8171 char:13+ $steppablePipeline = $scriptCmd.GetSteppablePipeline($myI …+ ~~~~~~~~~~~~~PSScriptRoot : C:\local\Temp\remoteIpMoProxy_PartnerCenter_3.0.10_localhost_7a01bbfe-eae9-4f3a-974e-953f2e958b13PSCommandPath : C:\local\Temp\remoteIpMoProxy_PartnerCenter_3.0.10_localhost_7a01bbfe-eae9-4f3a-974e-953f2e958b13\remoteIpMoProxy_PartnerCenter_3.0.10_localhost_7a01bbfe-eae9-4f3a-974e-953f2e958b13.psm1CommandOrigin : InternalScriptStackTrace : at , C:\local\Temp\remoteIpMoProxy_PartnerCenter_3.0.10_localhost_7a01bbfe-eae9-4f3a-974e-953f2e958b13\remoteIpMoProxy_PartnerCenter_3.0.10_localhost_7a01bbfe-eae9-4f3a-974e-953f2e958b13.psm1: line 8171at , C:\home\site\wwwroot\PrepareExportForTenant\run.ps1: line 39 2021-10-29T21:40:30.182 [Error] Executed 'Functions.PrepareExportForTenant' (Failed, Id=4ddc3c44-aa88-4b8f-b0c0-a44f0bf7f498, Duration=7058ms)Result: FailureException: Exception calling "GetSteppablePipeline" with "1" argument(s): "The parameter is incorrect. "Stack: at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)at System.Management.Automation.Runspaces.Pipeline.Invoke()at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 output, PSInvocationSettings settings)at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 output, PSInvocationSettings settings)at System.Management.Automation.PowerShell.Invoke[T](IEnumerable input, IList1 output, PSInvocationSettings settings)at System.Management.Automation.PowerShell.Invoke[T]()at Microsoft.Azure.Functions.PowerShellWorker.PowerShell.PowerShellExtensions.InvokeAndClearCommands[T](PowerShell pwsh) in /home/vsts/work/1/s/src/PowerShell/PowerShellExtensions.cs:line 45at Microsoft.Azure.Functions.PowerShellWorker.PowerShell.PowerShellManager.InvokeNonOrchestrationFunction(DurableController durableController, IDictionary outputBindings) in /home/vsts/work/1/s/src/PowerShell/PowerShellManager.cs:line 294at Microsoft.Azure.Functions.PowerShellWorker.PowerShell.PowerShellManager.InvokeFunction(AzFunctionInfo functionInfo, Hashtable triggerMetadata, TraceContext traceContext, IList1 inputData, FunctionInvocationPerformanceStopwatch stopwatch) in /home/vsts/work/1/s/src/PowerShell/PowerShellManager.cs:line 229at Microsoft.Azure.Functions.PowerShellWorker.RequestProcessor.InvokeFunction(AzFunctionInfo functionInfo, PowerShellManager psManager, FunctionInvocationPerformanceStopwatch stopwatch, InvocationRequest invocationRequest) in /home/vsts/work/1/s/src/RequestProcessor.cs:line 333at Microsoft.Azure.Functions.PowerShellWorker.RequestProcessor.ProcessInvocationRequestImpl(StreamingMessage request, AzFunctionInfo functionInfo, PowerShellManager psManager, FunctionInvocationPerformanceStopwatch stopwatch) in /home/vsts/work/1/s/src/RequestProcessor.cs:line 306

It is possible that I am just missing something, though I am not sure what?

Is there a lingering session of some sort after the first run ends?