1

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)
        {
        }
    }
1
  • Why running PowerShell from C# when you have C# client for Azure REST API? You can call Azure API without middle layer of PowerShell Commented Feb 14, 2018 at 9:47

2 Answers 2

1

Based on my test, there is no issue with you mentioned code. I can get empty output if input a wrong command. So please make sure that your command is correct.

Another import thing is that make sure that the command it has ouput. I test the azure command Add-AzureRmAccount.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Where is you connection to Azure?

You load the module, but you have nothing establishing a connection to Azure to use the cmdlets.

In your native PoSH instance/session, you'd to have this to work:

# Set creds
$AdminCreds = Get-Credential -Credential '[email protected]'

# Connect to Azure AD
Connect-AzureAD -Credential $Admincreds


Account                          Environment TenantId   TenantDomain            AccountType
-------                          ----------- --------   ------------            -----------
[email protected]    AzureCloud  11...      contoso.onmicrosoft.com User     

If not, you'd end up with errors like this...

Get-AzureADApplication
Get-AzureADApplication : You must call the Connect-AzureAD cmdlet before calling any other cmdlets.
At line:1 char:1
+ Get-AzureADApplication
+ ~~~~~~~~~~~~~~~~~~~~~~

2 Comments

Thanks for reply @postanote. but, i am using, following line in my powershell script. Login-AzureRmAccount -Credential $azurecred -ServicePrincipal -TenantId $tenantId running this script in powershell works absolutely fine. I also did change into my script, to load whole script rather then just giving file path. But same result. myOutPut variable doesn't get anything into it.
OoooK, I have not seen / used this cmdlet since I been using PoSH with AzureAD, hence the cmdlet I posted back with. Looking on dev workstation and servers where I have the original AzureAD cmdlets installed. This is not one of them. A quick look at the MSDOCS site for AzurePowerShell, shows this as a different install. So, I guess I need to get those on a system and try them first. Connect-AzureAD is quick simple and to the point, and probably the reason I had not looked at the AzureRM bits. So, time to play with them and see what the deal is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.