从C#运行Azure PowerShell脚本 [英] Running Azure PowerShell Script from C#

查看:56
本文介绍了从C#运行Azure PowerShell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,

Hello Everyone,

我正在尝试从PowerShell脚本向Azure资源组添加标签。
该脚本完美独立。当我尝试通过身份验证从C#代码调用脚本时。
我正在使用的脚本没有变化,只是我从C#调用它。

I am trying to add tags to Azure resource group from PowerShell script. The script works perfect standalone. When I try to call the script from C# code due to authentication. There is no change in the script I am using, just that I am calling it from C#.

下面是我的代码片段:

我在JSON文件中包含所有标签详细信息。

I have all the tag details in JSON file.

PowerShell代码:

PowerShell Code:

$ password = $ SPNKey | ConvertTo-SecureString -asPlainText -Force

  $ Credentials = New-Object System.Management.Automation.PSCredential($ SPNID,$ password)  -ErrorAction Stop 



  Login-AzureRmAccount -Credential $ Credentials -TenantId $ TenantID -SubscriptionId $ SubscriptionID -ServicePrincipal -ErrorAction Stop



  $ TagsData = Get-Content -Path $ JSONFilePath | ConvertFrom-Json -ErrorAction Stop



  $ hash = @ {}

  ForEach($ Tags in $ TagsData.Tags)

  {

if($ Item.TagKey.Length -ne 1)

{

  $ hash.Add($ Item.TagKey,$ Item.TagValue)

}

  }

$password = $SPNKey | ConvertTo-SecureString -asPlainText -Force
  $Credentials = New-Object System.Management.Automation.PSCredential($SPNID,$password)  -ErrorAction Stop 

 Login-AzureRmAccount -Credential $Credentials -TenantId $TenantID -SubscriptionId $SubscriptionID -ServicePrincipal -ErrorAction Stop

  $TagsData = Get-Content -Path $JSONFilePath | ConvertFrom-Json -ErrorAction Stop

  $hash = @{}
  ForEach($Item in $TagsData.Tags)
  {
if($Item.TagKey.Length -ne 1)
{
  $hash.Add($Item.TagKey,$Item.TagValue)
}
  }

Set-AzureRmResourceGroup -Name $ resourceGroupName -Tag $ hash 

Set-AzureRmResourceGroup -Name $resourceGroupName -Tag $hash 

C#代码:

C# Code:

processInfo = new ProcessStartInfo("powershell.exe""-File " + tagPSScriptPaht + " " + subID + " " + tenantid + " " + clientid + " " + clientkey + " " + Combo_RG.SelectedValue + " " + jsonFile);
                processInfo.CreateNoWindow = false;
                processInfo.UseShellExecute = false;
                //processInfo.Arguments = "-noexit";
 
 
                process = Process.Start(processInfo);
                process.WaitForExit();
 
                errorLevel = process.ExitCode;
                process.Close();

错误消息:

Error Message:

作为附件粘贴的错误消息 

Error message pasted as attachment 




PS:   Login-AzureRmAccount,  Add-AzureRmAccount无误地工作,甚至列出订阅详情从C#调用时。问题是当我尝试更新或读取Azure中的任何数据时。

PS: Login-AzureRmAccount, Add-AzureRmAccount work with no errors and lists the subscription details even when called from C#. Issue is when I try to update or read any data from Azure.

提前感谢您,

问候,

Guru

  ;

推荐答案

您好GuruPrasad HP,如果您使用的是最新的AzureRM模块且仍然面临此问题然后可能会发出与REST运行时身份验证令牌缓存问题相关的问题,该问题与令牌提供程序支持和/或ADAL支持和/或
目标.NET框架有关。所以AFAIK我建议在这里
和/或提出这个问题
here
 和/或  here 。希望这有帮助!!

Hi GuruPrasad H P, If you are using latest AzureRM modules and are still facing this issue then probably issue looks to be related to REST runtime authentication token caching issue which would be related to token provider support and/or ADAL support and/or target .NET framework. So AFAIK I would suggest raising this issue here and/or here and/or here. Hope this helps!!


这篇关于从C#运行Azure PowerShell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆