[英]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
創建了一個資源組Output:
檢查您的 powershell 版本並嘗試下載最新版本: https://github.com/PowerShell/PowerShell
在您的腳本中,您應該能夠使用:
Import-Module Az.Resources
#Just in case
Az Upgrade
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.