Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have Windows Server 2008 R2 machine and it has Power Shell v1.0. I wanted to connect to MS 365 online service using Power Shell with C#. I have installed Office 365 cmdlets and Microsoft Online Services Sign-In Assistant. ( Ref: http://onlinehelp.microsoft.com/en-us/office365-enterprises/hh124998.aspx#BKMK_install )

My script is:

$password = ConvertTo-SecureString "xxxxx" -AsPlainText –Force

$credential = New-Object System.Management.Automation.PsCredential("[email protected]",$password)

$cred = Get-Credential -cred $credential

Import-Module MSOnline

Connect-Msolservice -cred $cred

I can successfully run this script in the Power Shell Command window. But I have problem running this script in c# application.

Here is my c# code:

  public void RunScript()
    {
        StringBuilder ss = new StringBuilder();
        ss.AppendLine("$password = ConvertTo-SecureString \"" + pwd + "\" -AsPlainText –Force");
        ss.AppendLine("$credential = New-Object  System.Management.Automation.PsCredential(\"" + userName + "\",$password)");
        ss.AppendLine("$cred = Get-Credential -cred $credential");
        ss.AppendLine("Import-Module MSOnline");
        ss.AppendLine("Connect-Msolservice -cred $cred");

        using (Runspace runspace = RunspaceFactory.CreateRunspace())
        {
            Collection<PSObject> results = null;
            try
            {
                runspace.Open();
                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript(ss.toString());

                results = pipeline.Invoke();
            }
            finally
            {
                runspace.Close();
            }                
        }
    }

I get the following exception:

The term 'Connect-Msolservice' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Is there anything missing?

Thanks

share|improve this question

1 Answer

InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { "MSOnline" });

using (Runspace runspace = RunspaceFactory.CreateRunspace(iss))
{
// blah
}
share|improve this answer
Well thanks for the comment. I will try with your suggestion also. However, I have found the reason why it didn't recognize the "Connect-Msolservice" command. The reason was simple. I had created a console application whose Platform target is x86 while my dev machine is Microsoft Server 2008 64bit where I have installed MS Online Service module (64bits). By changing the Platform target to any cpu solve the issue. – Prakash Apr 17 '12 at 17:41
Hi David,I tried with the above solution but the same result (no success). BTW. It seems that cmdlets of MS Online service module (64bits) has to be executed/run from the 64bits Process/application. This is what I have experienced. Please share if you have any idea on this. – Prakash Apr 18 '12 at 11:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.