Reputation: 25
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)
{
}
}
Upvotes: 1
Views: 1610
Reputation: 24569
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.
Upvotes: 1
Reputation: 16116
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
+ ~~~~~~~~~~~~~~~~~~~~~~
Upvotes: 0