[英]Calling PowerShell Azure module and creating resource group from C#
I have the following PowerShell script that 1) installs Azure PowerShell SDK 2) logs in to Azure using the service principle and 3) creates a resource group.我有以下 PowerShell 脚本 1) 安装 Azure PowerShell SDK 2) 使用服务原则登录到 Azure 和 3) 创建资源组。 I am trying to call this script from C# .NET 6 but I am getting this error:我试图从 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.
I think it's not running the PowerShell script code at all.我认为它根本没有运行 PowerShell 脚本代码。
Note that the actual script does a lot more than just creating a resource group, but this is just an example.请注意,实际脚本所做的不仅仅是创建资源组,但这只是一个示例。
// 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);
}
UPDATE #1 : I updated the script and added -AllowClubber
to make Az is installed but this is what I am getting in the output:更新 #1 :我更新了脚本并添加了-AllowClubber
以安装 Az,但这就是我在 output 中得到的:
I think Az
is not getting installed and for some reason it think Az is already installed我认为Az
没有安装,出于某种原因它认为 Az 已经安装
And then然后
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.
UPDATE #2 : I modified the PowerShell script to unconditionally install the Az
and I am still getting the same error that New-AzResourceGroup
is not defined更新 #2 :我修改了 PowerShell 脚本以无条件安装Az
,但我仍然遇到相同的错误,即未定义New-AzResourceGroup
I have got similar error when i am using command我在使用命令时遇到类似的错误
New-AzResourceGroup
Then i have used Azure cli commands then i get the resource group created in azure然后我使用了 Azure cli 命令,然后我得到了在 azure 中创建的资源组
My powershell script(Updated your script):我的 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- Subscription name XXX- 订阅名称
rithDemo- resource group name rithDemo- 资源组名称
az login
.在脚本中,我首先使用az login
登录到 azure。 Then you will be redirected to azure login page there you can login.然后您将被重定向到 azure 登录页面,您可以在那里登录。az account set
command然后我使用az account set
命令设置订阅az group create
然后我使用az group create
创建了一个资源组Output: Output:
Check your powershell version and try to download latest: https://github.com/PowerShell/PowerShell检查您的 powershell 版本并尝试下载最新版本: https://github.com/PowerShell/PowerShell
In your script you should be able to use:在您的脚本中,您应该能够使用:
Import-Module Az.Resources
#Just in case
Az Upgrade
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.