0

そのため、関数を2回目に実行した後、関数が独自のデータを生成するのに問題があります。ユーザー名とパスワードが正しければ、初めて GetUsers コマンドを実行すると、ユーザーの配列リストが返されます。もう一度実行すると、使用するユーザー名やパスワードに関係なく、同じユーザーが返されます。私のコードはかなり混乱していると確信していますが、それをクリアしようとしていじっています。

ちょっとしたバックグラウンドについてですが、私は自分自身を初心者プログラマーだと考えているので、問題は非常に基本的なものである可能性があります。

ありがとう!

 public ArrayList GetUsers(string user, string pass)
        {
            InitialSessionState iss = InitialSessionState.CreateDefault();

            iss.ImportPSModule(new[] { "MSOnline" });
            using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss))
            {
              myRunSpace.Open();
              using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create())
              {
                  ArrayList available_users = new ArrayList();

                  //create Powershell runspace

                  powershell.Runspace = myRunSpace;

                  Command connect = new Command("Connect-MsolService");
                  System.Security.SecureString secureString = new System.Security.SecureString();
                  string myPassword = pass;
                  foreach (char c in myPassword)
                      secureString.AppendChar(c);

                  connect.Parameters.Add("Credential", new PSCredential(user, secureString));
                  powershell.Commands.AddCommand(connect);

                  Collection<PSObject> results = null;
                  results = powershell.Invoke();
                  powershell.Commands.Clear();

                  Command getuser = new Command("Get-MsolUser");
                  powershell.Commands.AddCommand(getuser);
                  results = null;

                  powershell.Stop();

                  results = powershell.Invoke();

                  foreach (PSObject item in results)
                  {
                      string userprincipalname = item.Properties["UserPrincipalName"].Value as string;
                      available_users.Add(userprincipalname);
                  }

                  powershell.Dispose();
                  myRunSpace.Dispose();

                  powershell.Runspace.Dispose();
                  powershell.Runspace.Close();
                  myRunSpace.Close();
                  return available_users;
              }
            }
        }
4

1 に答える 1

0

available_users タイプを明示的に削除または無効にしようとしましたか? その必要はないと思いますが…

于 2012-05-03T21:23:58.443 に答える