PowerShell Azure函数导入模块错误

iezvtpos  于 2023-04-07  发布在  Shell
关注(0)|答案(2)|浏览(236)

我正在尝试导入我的PowerShell Azure Function应用程序中的“Microsoft.PowerApps.Administration.PowerShell”模块。导入成功,但我的“run.ps1”文件中的以下命令引发错误:

Add-PowerAppsAccount -Username $username -Password $pass

以下是function.json的内容:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "Request",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "Response"
    }
  ]
}

以下是host.json的内容:

{
  "version": "2.0",
  "managedDependency": {
    "Enabled": true
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  }
}

以下是错误:

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:\home\data\ManagedDependencies\2304011814374714419.r\Microsoft.PowerApps.Administration.PowerShell\2.0.156\Microsoft.PowerApps.AuthModule.psm1
            Line             : $PublicClientApplication = [Microsoft.Identity.Client.PublicClientApplicationBuilder]::Create($ApplicationId).WithAuthority($authBaseuri, $aadAuthAudience, $true).WithDefaultRedirectUri().Build()
                               
            PositionMessage  : At C:\home\data\ManagedDependencies\2304011814374714419.r\Microsoft.PowerApps.Administration.PowerShell\2.0.156\Microsoft.PowerApps.AuthModule.psm1:273 char:13
                               +             $PublicClientApplication = [Microsoft.Identity.Client.Pub …
                               +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            PSScriptRoot     : C:\home\data\ManagedDependencies\2304011814374714419.r\Microsoft.PowerApps.Administration.PowerShell\2.0.156
            PSCommandPath    : C:\home\data\ManagedDependencies\2304011814374714419.r\Microsoft.PowerApps.Administration.PowerShell\2.0.156\Microsoft.PowerApps.AuthModule.psm1
            CommandOrigin    : Internal
        ScriptStackTrace      : at Add-PowerAppsAccountInternal, C:\home\data\ManagedDependencies\2304011814374714419.r\Microsoft.PowerApps.Administration.PowerShell\2.0.156\Microsoft.PowerApps.AuthModule.psm1: line 273
                                at Add-PowerAppsAccount, C:\home\data\ManagedDependencies\2304011814374714419.r\Microsoft.PowerApps.Administration.PowerShell\2.0.156\Microsoft.PowerApps.AuthModule.psm1: line 145
                                at <ScriptBlock>, C:\home\site\wwwroot\HttpTrigger1\run.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:\home\data\ManagedDependencies\2304011814374714419.r\Microsoft.PowerApps.Administration.PowerShell\2.0.156\Microsoft.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:\home\data\ManagedDependencies\2304011814374714419.r\Microsoft.PowerApps.Administration.PowerShell\2.0.156\Microsoft.PowerApps.AuthModule.psm1:145 char:5
                       +     Add-PowerAppsAccountInternal -Audience $Audience -Endpoint $Endpo …
                       +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    PSScriptRoot     : C:\home\data\ManagedDependencies\2304011814374714419.r\Microsoft.PowerApps.Administration.PowerShell\2.0.156
    PSCommandPath    : C:\home\data\ManagedDependencies\2304011814374714419.r\Microsoft.PowerApps.Administration.PowerShell\2.0.156\Microsoft.PowerApps.AuthModule.psm1
    InvocationName   : Add-PowerAppsAccountInternal
    CommandOrigin    : Internal
ScriptStackTrace      : at Add-PowerAppsAccountInternal, C:\home\data\ManagedDependencies\2304011814374714419.r\Microsoft.PowerApps.Administration.PowerShell\2.0.156\Microsoft.PowerApps.AuthModule.psm1: line 273
                        at Add-PowerAppsAccount, C:\home\data\ManagedDependencies\2304011814374714419.r\Microsoft.PowerApps.Administration.PowerShell\2.0.156\Microsoft.PowerApps.AuthModule.psm1: line 145
                        at <ScriptBlock>, C:\home\site\wwwroot\HttpTrigger1\run.ps1: line 17
PipelineIterationInfo : 



2023-04-01T18:44:30Z   [Error]   Executed 'Functions.HttpTrigger1' (Failed, Id=a1cf6e48-da48-48ca-bd65-a4463d3ac217, Duration=3675ms)
kyks70gy

kyks70gy1#

我以前没有使用过这个模块,但是MS Learn上的文档中规定的要求是你需要使用PowerShell v5.x。Azure Functions在v7.x运行时上运行。
这似乎与您收到的错误消息相关。
"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."
此模块(类似于其他MS模块)必须依赖于提示或弹出身份验证框,这在v7.x PowerShell中是不可能的。
但是,您可以通过将-UseWindowsPowerShell添加到导入模块命令来在Azure Functions中调用Windows PowerShell。
不确定模块中的身份验证是否允许您使用SPN身份验证,这将绕过MFA提示。
另一种方法是使用Azure自动化Runbook,如果您无法让它们在Azure Functions中工作,则可以使用v5.1运行时。

siv3szwd

siv3szwd2#

我已经在我的环境中复制了,并得到了预期的结果如下:
首先使用下面的命令安装,并遵循Document

Install-Module -Name Microsoft.PowerApps.Administration.PowerShell -Scope CurrentUser

然后使用以下命令导入:

然后,当我在run.ps1中运行它时,我得到了如下运行成功:

  • 你得到这个错误的原因之一可能是因为@jdweng提到的端点。
  • 另一个原因可能是因为你没有安装管理员权限的模块,没有正确导入它。如果你正确导入它,你会得到所需的输出,因为我有。

有关详细信息,请参阅reference1

相关问题