【问题标题】:Get-AzureAccount not finding accounts when running from C#从 C# 运行时,Get-AzureAccount 找不到帐户
【发布时间】:2016-03-31 00:18:36
【问题描述】:

我有以下 powershell 脚本(“azureaccounts.ps1”):

$accountExisted = @(Get-AzureAccount)
Write-Log $accountExisted.Count

当我从 Windows Powershell 运行它时,它会找到 5 个 Azure 帐户。
当我从 CMD 运行它时,它会找到 5 个 Azure 帐户。 (cmd中的“powershell azureaccounts.ps1”)

但是当我从 C# Web 应用程序执行脚本时,它没有找到任何帐户。

使用 PowerShell API:

PowerShell powerShell = PowerShell.Create();
powerShell.AddScript(@"C:\...\azureaccounts.ps1");
var results = powerShell.Invoke();

还有 Process 对象:

string sPowershellDir = @"C:\...\WindowsPowerShell\v1.0";
string sDeploymentScriptDir = @"C:\...\azureaccounts.ps1";
string sCommand = String.Format(@"powershell.exe -ExecutionPolicy ByPass -File {0}", sDeploymentScriptDir};
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + sCommand);
procStartInfo.UseShellExecute = false;
procStartInfo.WorkingDirectory = sPowershellDir;
Process oProcess = Process.Start(procStartInfo);

一种解决方案是执行 Add-AzureAccount,但该 cmdlet 会提示一个弹出窗口,因此它不会自动发生。

有什么建议吗?

【问题讨论】:

    标签: c# powershell azure azure-powershell


    【解决方案1】:

    当您从命令行执行时,您会继承之前所做的验证。您可能已经通过您现在想要避免的弹出窗口进行了身份验证。从 PowerShell 的角度来看,使用 Web 应用程序或新进程,您从一个从未向您的 Azure 环境进行身份验证的进程开始。

    为了以无人值守的方式进行身份验证,您可能需要遵循https://azure.microsoft.com/en-us/documentation/articles/resource-group-authenticate-service-principal/ 文档中的说明。请注意,本文档基于 Azure 模块 V1+。

    不过,这个想法是一样的。 请注意,如果您以无人参与的方式从 1 个帐户连接,PowerShell 脚本将回答 1。但我想您希望该脚本执行其他操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      相关资源
      最近更新 更多