簡體   English   中英

我們可以在 .net 控制台應用程序中的運行空間內執行 az PowerShell 模塊命令,該控制台應用程序部署為 Azure 應用程序服務上的 Web 作業嗎?

[英]Can we execute az PowerShell module commands inside of a runspace in a .net console app deployed as a webjob on Azure app service?

我有一個運行在 azure 應用程序服務中的 Web 作業,它使用運行空間來執行 azure powershell 模塊命令。 在本地它工作正常,因為我可以直接使用 Windows powershell 安裝 az 模塊,但是當我將它部署到 Azure 時,它失敗了。

最初我在執行Connect-AzAccount -Identity時遇到以下錯誤

Connect-AzAccount : The term 'Connect-AzAccount' is not recognized as the name of a cmdlet

然后我嘗試在運行空間內安裝 az 模塊,但我開始收到另一個錯誤:

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

這是我嘗試安裝 az 模塊的方式:

    runSpace.Open();

    RunspaceInvoke scriptInvoker = new RunspaceInvoke();
    scriptInvoker.Invoke("Install-PackageProvider Nuget -MinimumVersion 2.8.5.201 -Force -Scope CurrentUser");
    scriptInvoker.Invoke("Import-PackageProvider Nuget");
    scriptInvoker.Invoke("Install-Module Az.Websites -Force -AllowClobber -Scope CurrentUser");
    scriptInvoker.Invoke("Import-Module Az.Websites");
    scriptInvoker.Invoke("Install-Module Az.Storage -Force -AllowClobber -Scope CurrentUser");
    scriptInvoker.Invoke("Import-Module Az.Storage");

那么是否可以在appservice中執行az powershell模塊命令呢?

  • 有多種方法可以解決此問題,以下是一些解決方案。

修復 1:要解決此問題,請運行以下命令。

Import-Module Az.Accounts

當您執行上述命令時,它會提示您輸入憑據。

修復 2:

  • 安裝模塊 Az。 通過使用以下 PowerShell cmdlet,
Import-Module Az
  • 像下面這樣連接到AzAccount
Connect-AzAccount
  • 您可以從此處檢查有效的解決方案

  • 還要通過相關討論檢查此SO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM