【发布时间】: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