Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# Powershell API msol behavior

If I run the first below code, the expected outcome occurs, results has the expected object in it. If the second example is run, results will be null and not contain any results. Is the latter set up in the correct manner, or am I missing some detail?

Works as expected

using (PowerShell ps = PowerShell.Create())
{
    ps.AddScript("connect-msolservice; Get-MsolCompanyInformation");
    Collection<PSObject> results = ps.Invoke();
    foreach (PSObject result in results)
    {
        label.Content = "Last AD Sync: " + result.Properties["LastDirSyncTime"].Value;
    }
}

Microsoft recommends chaining commands in the below example, however, results will not contain any data.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

using (PowerShell ps = PowerShell.Create())
{
    ps.AddCommand("connect-msolservice");
    ps.AddCommand("Get-MsolCompanyInformation");
    Collection<PSObject> results = ps.Invoke();
    foreach (PSObject result in results)
    {
        label.Content = "Last AD Sync: " + result.Properties["LastDirSyncTime"].Value;
    }
}

>Solution :

The only part missing from the second example is statement termination between the two commands – equivalent to the ; in the script:

ps.AddCommand("connect-msolservice")
  .AddStatement() // this is our `;`
  .AddCommand("Get-MsolCompanyInformation");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading