2012-04-16 2 views
3

Windows Server 2008 R2 컴퓨터가 있고 Power Shell v1.0이 있습니다. C#으로 Power Shell을 사용하여 MS 365 온라인 서비스에 연결하려고했습니다. Office 365 cmdlet 및 Microsoft Online Services 로그인 도우미를 설치했습니다. (참조 : http://onlinehelp.microsoft.com/en-us/office365-enterprises/hh124998.aspx#BKMK_install)Office 365 : Power Shell을 사용하여 Office Online 365 서비스에 연결 스크립트 문제

내 스크립트는 다음과 같습니다

$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 

나는 성공적으로 파워 쉘 명령 창에서이 스크립트를 실행할 수 있습니다. 하지만 C# 응용 프로그램에서이 스크립트를 실행하는 데 문제가 있습니다.

용어 '연결-Msolservice'는 cmdlet에, 함수의 이름, 스크립트 파일, 또는로 인식되지 않습니다 : 나는 다음과 같은 예외가

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(); 
      }     
     } 
    } 

: 여기

내 C# 코드입니다 실행할 수있는 프로그램. 이름의 철자를 확인하거나 경로가 포함되어 있으면 경로가 올바른지 확인하고 다시 시도하십시오.

누락 된 품목이 있습니까?

감사

답변

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

using (Runspace runspace = RunspaceFactory.CreateRunspace(iss)) 
{ 
// blah 
} 
+2

음 감사합니다. 나는 또한 당신의 제안으로 시도 할 것입니다. 그러나 "Connect-Msolservice"명령을 인식하지 못한 이유를 발견했습니다. 이유는 간단했다. 나는 내 컴퓨터가 Microsoft Server 2008 64 비트 MS SQL 서비스 모듈 (64 비트)을 설치 한 동안 플랫폼 대상이 x86 인 콘솔 응용 프로그램을 만들었습니다. 어떤 CPU로든 플랫폼 대상을 변경하여 문제를 해결하십시오. – Prakash

+0

안녕 데이비드, 위의 솔루션을 시도했지만 같은 결과 (성공 없음). BTW. MS 온라인 서비스 모듈 (64 비트)의 cmdlet은 64 비트 프로세스/응용 프로그램에서 실행/실행되어야합니다. 이것이 제가 경험 한 것입니다. 이것에 대해 알고 싶다면 나와 공유하십시오. – Prakash

+0

공유 주셔서 감사합니다, Prakash. 나는 같은 문제에 부딪쳤다. – ACR

0

당신이 일 다음 설치되어 있는지 확인하십시오 : 주석에 대한

1. SharePoint Online Management Shell 
2. Microsoft Online Services Sign-In Assistant version 7.0 or greater version 
3. Windows Azure Active Directory Module 
관련 문제