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;
私は変数に特別な何も持っていないことを再度チェックしました。私は電子メールアドレスを直接コマンドに入れようとしました。残念ながら、同じエラーが発生します。 – MrMatt
ああ、申し訳ありませんが、それは "アイデンティティ"のフラグを認識しないと言って、 "アイデンティティ"の代わりに "アイデンティティ"を追加すればどうですか? – Alex
こんにちはアレックス、私は 'アイデンティティ'を試してみましたが、違いはないようですが、解決策が見つかりました。グループメンバーを見る前に次のコードを追加して、作業を開始しました。
PSCommand ImportSession = new PSCommand(); ImportSession.AddCommand("Import-PSSession"); ImportSession.AddParameter("Session", SessionHolder); powershell.Commands = ImportSession; powershell.Invoke();
– MrMatt