简体   繁体   中英

How to call PowerShell script with azure commands in C# windows application

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)
        {
        }
    }

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 .

在此处输入图片说明

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 'admin@domain.com'

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


Account                          Environment TenantId   TenantDomain            AccountType
-------                          ----------- --------   ------------            -----------
admin@contoso.onmicrosoft.com    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
+ ~~~~~~~~~~~~~~~~~~~~~~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM