I am trying to import the "Microsoft.PowerApps.Administration.PowerShell" module in my PowerShell Azure Function App. The import is successful but the following command in my "run.ps1" file is throwing an error:
Add-PowerAppsAccount -Username $username -Password $pass
Following are the contents of function.json:
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "Request",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "Response"
}
]
}
Following are the contents of host.json:
{
"version": "2.0",
"managedDependency": {
"Enabled": true
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.*, 4.0.0)"
}
}
Following is the Error:
2023-04-01T18:44:29Z [Error] EXCEPTION: The following exception occurred while retrieving member "WithAuthority": "Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified."
Exception :
Type : System.Management.Automation.ExtendedTypeSystemException
ErrorRecord :
Exception :
Type : System.Management.Automation.ParentContainsErrorRecordException
Message : The following exception occurred while retrieving member "WithAuthority": "Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified."
HResult : -2146233087
CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : CatchFromBaseGetMember
InvocationInfo :
ScriptLineNumber : 273
OffsetInLine : 13
HistoryId : -1
ScriptName : C:homedataManagedDependencies2304011814374714419.rMicrosoft.PowerApps.Administration.PowerShell2.0.156Microsoft.PowerApps.AuthModule.psm1
Line : $PublicClientApplication = [Microsoft.Identity.Client.PublicClientApplicationBuilder]::Create($ApplicationId).WithAuthority($authBaseuri, $aadAuthAudience, $true).WithDefaultRedirectUri().Build()
PositionMessage : At C:homedataManagedDependencies2304011814374714419.rMicrosoft.PowerApps.Administration.PowerShell2.0.156Microsoft.PowerApps.AuthModule.psm1:273 char:13
+ $PublicClientApplication = [Microsoft.Identity.Client.Pub …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PSScriptRoot : C:homedataManagedDependencies2304011814374714419.rMicrosoft.PowerApps.Administration.PowerShell2.0.156
PSCommandPath : C:homedataManagedDependencies2304011814374714419.rMicrosoft.PowerApps.Administration.PowerShell2.0.156Microsoft.PowerApps.AuthModule.psm1
CommandOrigin : Internal
ScriptStackTrace : at Add-PowerAppsAccountInternal, C:homedataManagedDependencies2304011814374714419.rMicrosoft.PowerApps.Administration.PowerShell2.0.156Microsoft.PowerApps.AuthModule.psm1: line 273
at Add-PowerAppsAccount, C:homedataManagedDependencies2304011814374714419.rMicrosoft.PowerApps.Administration.PowerShell2.0.156Microsoft.PowerApps.AuthModule.psm1: line 145
at <ScriptBlock>, C:homesitewwwrootHttpTrigger1run.ps1: line 17
TargetSite :
Name : CheckActionPreference
DeclaringType : System.Management.Automation.ExceptionHandlingOps, System.Management.Automation, Version=7.2.9.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35
MemberType : Method
Module : System.Management.Automation.dll
Message : The following exception occurred while retrieving member "WithAuthority": "Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified."
Data : System.Collections.ListDictionaryInternal
InnerException :
Type : System.IO.FileNotFoundException
Message : Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
FileName : System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
TargetSite :
Name : GetSignature
DeclaringType : System.Signature, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
MemberType : Method
Module : System.Private.CoreLib.dll
Source : System.Private.CoreLib
HResult : -2147024894
StackTrace :
at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
at System.Reflection.RuntimeMethodInfo.<get_Signature>g__LazyCreateSignature|24_0()
at System.Reflection.RuntimeMethodInfo.FetchNonReturnParameters()
at System.Reflection.RuntimeMethodInfo.GetParameters()
at System.Management.Automation.DotNetAdapter.SameSignature(MethodBase method1, MethodBase method2)
at System.Management.Automation.DotNetAdapter.AddOverload(List`1 previousMethodEntry, MethodInfo method)
at System.Management.Automation.DotNetAdapter.PopulateMethodReflectionTable(Type type, MethodInfo[] methods, CacheTable typeMethods)
at System.Management.Automation.DotNetAdapter.PopulateMethodReflectionTable(Type type, CacheTable typeMethods, BindingFlags bindingFlags)
at System.Management.Automation.DotNetAdapter.GetInstanceMethodReflectionTable(Type type)
at System.Management.Automation.DotNetAdapter.GetDotNetMethodImpl[T](Object obj, String methodName, MemberNamePredicate predicate)
at System.Management.Automation.DotNetAdapter.GetDotNetMethod[T](Object obj, String methodName)
at System.Management.Automation.DotNetAdapter.GetMember[T](Object obj, String memberName)
at System.Management.Automation.Adapter.BaseGetMember[T](Object obj, String memberName)
Source : System.Management.Automation
HResult : -2146233087
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.DoEndProcessing()
at System.Management.Automation.CommandProcessorBase.Complete()
CategoryInfo : NotSpecified: (:) [Add-PowerAppsAccountInternal], ExtendedTypeSystemException
FullyQualifiedErrorId : CatchFromBaseGetMember,Add-PowerAppsAccountInternal
InvocationInfo :
MyCommand : Add-PowerAppsAccountInternal
ScriptLineNumber : 145
OffsetInLine : 5
HistoryId : 1
ScriptName : C:homedataManagedDependencies2304011814374714419.rMicrosoft.PowerApps.Administration.PowerShell2.0.156Microsoft.PowerApps.AuthModule.psm1
Line : Add-PowerAppsAccountInternal -Audience $Audience -Endpoint $Endpoint -Username $Username -Password $Password -TenantID $TenantID -CertificateThumbprint $CertificateThumbprint -ClientSecret $ClientSecret -ApplicationId $ApplicationId
PositionMessage : At C:homedataManagedDependencies2304011814374714419.rMicrosoft.PowerApps.Administration.PowerShell2.0.156Microsoft.PowerApps.AuthModule.psm1:145 char:5
+ Add-PowerAppsAccountInternal -Audience $Audience -Endpoint $Endpo …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PSScriptRoot : C:homedataManagedDependencies2304011814374714419.rMicrosoft.PowerApps.Administration.PowerShell2.0.156
PSCommandPath : C:homedataManagedDependencies2304011814374714419.rMicrosoft.PowerApps.Administration.PowerShell2.0.156Microsoft.PowerApps.AuthModule.psm1
InvocationName : Add-PowerAppsAccountInternal
CommandOrigin : Internal
ScriptStackTrace : at Add-PowerAppsAccountInternal, C:homedataManagedDependencies2304011814374714419.rMicrosoft.PowerApps.Administration.PowerShell2.0.156Microsoft.PowerApps.AuthModule.psm1: line 273
at Add-PowerAppsAccount, C:homedataManagedDependencies2304011814374714419.rMicrosoft.PowerApps.Administration.PowerShell2.0.156Microsoft.PowerApps.AuthModule.psm1: line 145
at <ScriptBlock>, C:homesitewwwrootHttpTrigger1run.ps1: line 17
PipelineIterationInfo :
2023-04-01T18:44:30Z [Error] Executed 'Functions.HttpTrigger1' (Failed, Id=a1cf6e48-da48-48ca-bd65-a4463d3ac217, Duration=3675ms)
2
Answers
I’ve not used this module before but the requirements stated in the docs on MS Learn state you need to use PowerShell
v5.x
. Azure Functions runs on thev7.x
runtime.This seems to correlate with the error message you’re getting.
"Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified."
This module (akin to other MS modules) must rely on a prompt or popup authentication box which is not possible in
v7.x
PowerShell.You can however call out to Windows PowerShell within Azure Functions by adding
-UseWindowsPowerShell
to your import module command.Not sure if the authentication within the module lets you use SPN authentication which will bypass MFA prompts however.
An alternative is to use Azure Automation Runbooks which do have a
v5.1
runtime available if you can’t get them to work in Azure Functions.I have reproduced in my environment and got expected results as below:
Firstly installed using below command and followed Document:
Then Imported with following command:
Then when I run it in run.ps1, I got it run successful as below:
For further refer reference1.