Below is my event, where, if I pass, simple powershell script it runs fine, but if I pass script contains any Azure commands, output is blank. (That azure command script is running fine from powershell command prompt)
private void RunScript_Click(object sender, EventArgs e)
{
string result = "";
PSDataCollection<PSObject> myOutPut = new PSDataCollection<PSObject>();
try
{
InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] {
@"C:\Program Files\WindowsPowerShell\Modules\AzureRM\5.2.0\AzureRM.psd1",
});
using (Runspace objRunSpace = RunspaceFactory.CreateRunspace(initial))
{
objRunSpace.Open();
using (PowerShell objPowerShell = PowerShell.Create())
{
objPowerShell.Runspace = objRunSpace;
string Script = textBoxURL.Text;
objPowerShell.AddScript(Script);
objPowerShell.AddCommand("Out-String");
IAsyncResult IvokeResult = objPowerShell.BeginInvoke<PSObject, PSObject>(null, myOutPut);
while (IvokeResult.IsCompleted == false)
{
System.Threading.Thread.Sleep(100);
}
foreach (PSObject outitem in myOutPut)
{
result += outitem.ToString();
}
}
}
textBoxOutPut.Text = result;
}
catch (Exception ex)
{
}
}
