簡體   English   中英

調用 PowerShell Azure 模塊並從 C# 創建資源組

[英]Calling PowerShell Azure module and creating resource group from C#

我有以下 PowerShell 腳本 1) 安裝 Azure PowerShell SDK 2) 使用服務原則登錄到 Azure 和 3) 創建資源組。 我試圖從 C# .NET 6 調用此腳本,但出現此錯誤:

New-AzResourceGroup -name $recoveryResourceGroupName -location $locat …
     |  ~~~~~~~~~~~~~~~~~~~
     | The term 'New-AzResourceGroup' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

我認為它根本沒有運行 PowerShell 腳本代碼。

請注意,實際腳本所做的不僅僅是創建資源組,但這只是一個示例。

// The difference between CreateDefault and CreateDefault2 is that
// CreateDefault includes engine snap-ins, while CreateDefault2 does not.
var initialState = InitialSessionState.CreateDefault2();
using var ps = PowerShell.Create(initialState);
var results = await ps.AddScript(@"
[string][ValidateNotNullOrEmpty()] $applicationId = """"
[string][ValidateNotNullOrEmpty()] $secret = """"
[string][ValidateNotNullOrEmpty()] $subscriptionId = """"
[string][ValidateNotNullOrEmpty()] $tenantId = """"

# Install Azure Powershell modules
Write-Output "Installing Modules..."
if (Get-Module -ListAvailable -Name 'Az*') {
    Write-Output "  Az Already Installed"
} 
else {
    Install-Module -Name 'Az' -Scope CurrentUser -Repository PSGallery -Force
    Write-Output "Installed AZ"
}

# Import Azure module
Import-Module 'Az'

# Login to azure using credentials from the KeyVault
$secretAsSecureString = ConvertTo-SecureString -String $secret -AsPlainText -Force
$pscredential = New-Object -TypeName System.Management.Automation.PSCredential($applicationId, $secretAsSecureString)
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId

# Select Right subscription
Set-AzContext $subscriptionId

New-AzResourceGroup -Name 'TestRg123' -Location 'eastus2euap'
").InvokeAsync();

foreach (PSObject outputItem in results)
{
    Debug.WriteLine(outputItem);
}

更新 #1 :我更新了腳本並添加了-AllowClubber以安裝 Az,但這就是我在 output 中得到的:

在此處輸入圖像描述

我認為Az沒有安裝,出於某種原因它認為 Az 已經安裝

然后

New-AzResourceGroup: 
Line |
  97 |  New-AzResourceGroup -name $recoveryResourceGroupName -location $locat …
     |  ~~~~~~~~~~~~~~~~~~~
     | The term 'New-AzResourceGroup' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

更新 #2 :我修改了 PowerShell 腳本以無條件安裝Az ,但我仍然遇到相同的錯誤,即未定義New-AzResourceGroup

我在使用命令時遇到類似的錯誤

New-AzResourceGroup

然后我使用了 Azure cli 命令,然后我得到了在 azure 中創建的資源組

我的 powershell 腳本(更新了你的腳本):

using System.Diagnostics;
using System.Management.Automation.Runspaces;
using System.Management.Automation;

var initialState = InitialSessionState.CreateDefault2();
initialState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;

using var ps = PowerShell.Create(initialState);
var results = ps.AddScript(@"
Install-PackageProvider -Name Nuget -Scope CurrentUser –Force

Install-Module –Name PowerShellGet -Scope CurrentUser –Force

Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
Install-Module -Name Az.Resources -AllowClobber -Scope CurrentUser

# Import Azure module

Import-Module 'Az'
Import-Module 'Az.Accounts'
Import-Module 'Az.RecoveryServices'

az login

try {
    az account set --subscription ""XXX"
    az group create --name rithDemo --location eastus
}
catch
{
    $string_err = $_ | Out-String
    Write-Output ""Failed to run test $testname because $string_err""
}
").Invoke();

XXX- 訂閱名稱

rithDemo- 資源組名稱

  • 在腳本中,我首先使用az login登錄到 azure。 然后您將被重定向到 azure 登錄頁面,您可以在那里登錄。
  • 然后我使用az account set命令設置訂閱
  • 然后我使用az group create創建了一個資源組
  • 還在腳本中添加了 AllowClobber 通過這個過程,資源組被創建。

Output:

在此處輸入圖像描述

檢查您的 powershell 版本並嘗試下載最新版本: https://github.com/PowerShell/PowerShell

在您的腳本中,您應該能夠使用:

Import-Module Az.Resources

#Just in case

Az Upgrade 

暫無
暫無

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

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