簡體   English   中英

Office 365:使用Power Shell腳本問題連接到Office Online 365服務

[英]Office 365: Connecting to Office Online 365 service using Power Shell Script issue

我有Windows Server 2008 R2機器,它有Power Shell v1.0。 我想使用Power Shell和C#連接到MS 365在線服務。 我已安裝Office 365 cmdlet和Microsoft Online Services登錄助手。 (參考: http//onlinehelp.microsoft.com/en-us/office365-enterprises/hh124998.aspx#BKMK_install

我的腳本是:

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

$credential = New-Object System.Management.Automation.PsCredential("xxxx@xxxx.onmicrosoft.com",$password)

$cred = Get-Credential -cred $credential

Import-Module MSOnline

Connect-Msolservice -cred $cred

我可以在Power Shell命令窗口中成功運行此腳本。 但我在c#應用程序中運行此腳本時遇到問題。

這是我的c#代碼:

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

我得到以下異常:

術語“Connect-Msolservice”不被識別為cmdlet,函數,腳本文件或可操作程序的名稱。 檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是否正確,然后重試。

有什么遺漏?

謝謝

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

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

請確保您已安裝以下內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM