2017-04-20 11 views
0

Office 365グループに接続しようとしていますが、Powershellを使用してC#プロジェクトを通じてグループのメンバーシップを一覧表示しようとしています。C#でPowerShellを使用してOffice 365グループのメンバーを読み取る

From this article、私が使用する必要があるコマンドは、ここで

Get-UnifiedGroupLinks –Identity groupalias –LinkType Members 

で決定しました私の現在のコードです:上記で使用

string connectionUri = "https://outlook.office365.com/powershell-liveid/"; 
SecureString secpassword = new SecureString(); 
foreach (char c in Password) 
{ 
    secpassword.AppendChar(c); 
} 
PSCredential credential = new PSCredential(UserName, secpassword); 

Runspace runspace = RunspaceFactory.CreateRunspace(); 
PSObject SessionHolder = null; 
using (PowerShell powershell = PowerShell.Create()) 
{ 
    PSCommand command = new PSCommand(); 
    command.AddCommand("New-PSSession"); 
    command.AddParameter("ConfigurationName", "Microsoft.Exchange"); 
    command.AddParameter("ConnectionUri", new Uri(connectionUri)); 
    command.AddParameter("Credential", credential); 
    command.AddParameter("Authentication", "Basic"); 
    powershell.Commands = command; 

    runspace.Open(); 
    powershell.Runspace = runspace; 
    Collection<System.Management.Automation.PSObject> result = powershell.Invoke(); 
    if (powershell.Streams.Error.Count > 0 || result.Count != 1) 
    { 
     throw new Exception("Fail to establish the connection"); 
    } 
    else 
     SessionHolder = result[0]; 
} 
using (PowerShell powershell = PowerShell.Create()) 
{ 
    PSCommand command = new PSCommand(); 
    // –Identity groupalias –LinkType Members 
    command = new PSCommand(); 
    command.AddCommand("Invoke-Command"); 
    command.AddParameter("ScriptBlock", System.Management.Automation.ScriptBlock.Create("Get-UnifiedGroupLinks")); 
    command.AddParameter("Session", SessionHolder); 
    command.AddParameter("Identity", groupAddress); 
    command.AddParameter("LinkType", "Members"); 
    powershell.Commands = command; 
    powershell.Runspace = runspace; 

    Collection<PSObject> PSOutput = powershell.Invoke(); 
    // loop through each output object item 
    foreach (PSObject outputItem in PSOutput) 
    { 

    } 
} 

変数、宣言は示されていない: "ユーザー名"、 "password"、 "groupAddress"

私はサービスに接続することができますが、グループメンバーを取得しようとすると「パラメータ名「ID」に一致するパラメータが見つかりません」

コードのトラブルシューティングを続行する方法がわかりません。私は、グループの電子メール、グループエイリアス、およびグループ表示名をIdentityパラメータで試しました。おそらく私は別の何かが間違っている?

私は、Windows 10マシン上のVisual Studio 2017で、次のライブラリを使用しています:

using System.Management.Automation; 
using System.Management.Automation.Runspaces; 

答えて

0

私はそれはそれを遅くするので、より良い方法があるかどうか、私はわからないんだけど、ここで私の更新された...物事が作業するコードのビットを発見しましたコード:

string connectionUri = "https://outlook.office365.com/powershell-liveid/"; 
SecureString secpassword = new SecureString(); 
foreach (char c in Password) 
{ 
    secpassword.AppendChar(c); 
} 
PSCredential credential = new PSCredential(UserName, secpassword); 

Runspace runspace = RunspaceFactory.CreateRunspace(); 
PSObject SessionHolder = null; 
using (PowerShell powershell = PowerShell.Create()) 
{ 
    string connectionUri = "https://outlook.office365.com/powershell-liveid/"; 
    SecureString secpassword = new SecureString(); 
    foreach (char c in Password) 
    { 
     secpassword.AppendChar(c); 
    } 
    PSCredential credential = new PSCredential(UserName, secpassword); 
    PSCommand command = new PSCommand(); 
    command.AddCommand("New-PSSession"); 
    command.AddParameter("ConfigurationName", "Microsoft.Exchange"); 
    command.AddParameter("ConnectionUri", new Uri(connectionUri)); 
    command.AddParameter("Credential", credential); 
    command.AddParameter("Authentication", "Basic"); 
    powershell.Commands = command; 

    runspace.Open(); 
    powershell.Runspace = runspace; 
    Collection<System.Management.Automation.PSObject> result = powershell.Invoke(); 
    if (powershell.Streams.Error.Count > 0 || result.Count != 1) 
     throw new Exception("Fail to establish the connection"); 
    else 
     SessionHolder = result[0]; 

    PSCommand ImportSession = new PSCommand(); 
    ImportSession.AddCommand("Import-PSSession"); 
    ImportSession.AddParameter("Session", SessionHolder); 
    powershell.Commands = ImportSession; 
    powershell.Invoke(); 

    PSCommand GrabGroup = new PSCommand(); 
    GrabGroup.AddCommand("Get-UnifiedGroupLinks"); 
    GrabGroup.AddParameter("Identity", GroupAddress); 
    GrabGroup.AddParameter("LinkType", "Members"); 
    powershell.Commands = GrabGroup; 
    Collection<PSObject> PSOutput_GroupMembers = powershell.Invoke(); 
    foreach (PSObject outputItem in PSOutput_GroupMembers) 
    { 
     //Process Members 
    } 
} 

鍵は、私がそれを使い始める前にインポートする必要があるようです。

0

私はいくつかの余分な文字で変数を渡しているとき、通常、私はエラーを取得します。変数がどのように渡されているかを確認します。変数が渡されているかどうかをチェックします。変数には単に "email"が含まれているか、@ {"email"} Powershellでは非常に一般的です。

明るい光を放つ希望。コマンドは非常に簡単ですので:

Get-UnifiedGroupLinks -Identity [name] -LinkType members 
+0

私は変数に特別な何も持っていないことを再度チェックしました。私は電子メールアドレスを直接コマンドに入れようとしました。残念ながら、同じエラーが発生します。 – MrMatt

+0

ああ、申し訳ありませんが、それは "アイデンティティ"のフラグを認識しないと言って、 "アイデンティティ"の代わりに "アイデンティティ"を追加すればどうですか? – Alex

+0

こんにちはアレックス、私は 'アイデンティティ'を試してみましたが、違いはないようですが、解決策が見つかりました。グループメンバーを見る前に次のコードを追加して、作業を開始しました。 PSCommand ImportSession = new PSCommand(); ImportSession.AddCommand("Import-PSSession"); ImportSession.AddParameter("Session", SessionHolder); powershell.Commands = ImportSession; powershell.Invoke(); MrMatt

関連する問題