Question

I've developed a web role to manage Azure VM's that is working locally but NOT when it's deployed in a Cloud Service.

I have executed the cmd that is in the web role in PowerShell through an RDP connection to the Cloud Service, so I know PowerShell v3.0 and Azure cmd are working fine.

First steps I had some permissions and certs issues but solved, the problem now is I can't see any error in Event Viewer.

I'm using PowerShell.Create() of System.Automation.dll

string script = "Set-ExecutionPolicy Unrestricted -Force
script = "Import-Module \"D:\\Program Files (x86)\\Microsoft SDKs\\Windows Azure\\PowerShell\\Azure\" 2 >> C:\errorp.out";
script = "Set-AzureSubscription –DefaultSubscription \"Test Environment\"";
script = "Get-AzureVM " + vm

I'm trying to get the error in all the commands with "2 >> C:\errorp.out" (actually is in all commands but didn't copy here) but it creates a blank file.

Am I missing any extra configuration to be able to do that?

Was it helpful?

Solution 2

Silly mistake: I was creating a new shell for each line of the script

var shell = PowerShell.Create();

So, in the second line, after do "Import-Module Azure", this second shell didn't have access to the Azure commands. I get this thanks to @Rick for introduce me the $error, however could get working this in C#, what I did instead is:

if (shell.Streams.Error.Count > 0)
{
   for (int i = 0; i < shell.Streams.Error.Count; i++)
   {
      ResultBox.Text += "Error: " + shell.Streams.Error[i] + "\r\n";
   }
}

OTHER TIPS

The $error variable will have your error history. For example

$error | format-list -property *
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top