1

次の PoweShell コマンドの実行中に例外が発生します。

Get-mailboxPermission -Identity MAILBOX_EMAIL | 
    fl User,AccessRights,IsInherited,Deny | Out-String -Width 300

例外:

"1" 個の引数を指定して "GetSteppablePipeline" を呼び出し中に例外が発生しました: "カスタム属性 'Parameter' の型が見つかりません。
この型を含むアセンブリが読み込まれていることを確認してください。"

これに続いて

Get-mailboxPermission -Identity "mailboxidentity" |
  fl User,AccessRights,IsInherited,Deny | Out-String -Width 300

それは私を取得していますParameterBindingException

パラメータ名 'CommandName' に一致するパラメータが見つかりません。

これは私のコードの一部です。 上記のコマンドを送信してexecuteRemoteCommand、すべてのユーザーに対して 1 つずつ機能させます。

private void initializeRunspace() {
  runSpace = RunspaceFactory.CreateRunspace();
  runSpace.Open();
  createRemotePowershell();
  executeRemoteCommand("Set-ExecutionPolicy -ExecutionPolicy RemoteSigned",true);
  executeRemoteCommand("Import-PSSession -Session $session -CommandName 'Get-MailboxPermission' ,'Get-MailboxDatabase','Add-ADPermission', 'Get-ADPermission', 'Set-EventLogLevel', 'Get-EventLogLevel'",true);
  log("Remote powershell initialization ends");
}

public void createRemotePowershell() {
  powershell = PowerShell.Create();
  powershell.Runspace = runSpace;

  PSCommand rmcommand = new PSCommand();
  rmcommand.AddCommand("New-PSSession");
  rmcommand.AddParameter("ConfigurationName", "Microsoft.Exchange");
  rmcommand.AddParameter("ConnectionUri", uri);
  if (creds != null) {
    rmcommand.AddParameter("Credential", creds);
  }
  rmcommand.AddParameter("Authentication", "kerberos");

  PSSessionOption sessionOption = new PSSessionOption();
  sessionOption.SkipCACheck = true;
  sessionOption.SkipCNCheck = true;
  sessionOption.SkipRevocationCheck = true;
  rmcommand.AddParameter("SessionOption", sessionOption);

  powershell.Commands = rmcommand;

  Collection<PSSession> result = powershell.Invoke<PSSession>();

  log("Remote Powershell Count: "+result.Count);

  if (result.Count != 1) {
    throw new Exception("Unexpected number of Remote Runspace connections returned.");
  }

  rmcommand = new PSCommand();
  rmcommand.AddCommand("Set-Variable");
  rmcommand.AddParameter("Name", "session");
  rmcommand.AddParameter("Value", result[0]);
  powershell.Commands = rmcommand;
  powershell.Invoke();
}

private String executeRemoteCommand(String shellcommand,bool retryOnSessionExpiry) {
  try {
    if (runSpace == null) {
      initializeRunspace();
    }
    rmcommand = new PSCommand();
    rmcommand.AddScript(shellcommand);
    rmcommand.Commands.Add("Out-String");
    powershell.Commands = rmcommand;

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

    StringBuilder sb = new StringBuilder();

    foreach (PSObject obj in results) {
      sb.Append(obj.ToString());
    }
    return sb.ToString();
  } catch (CmdletInvocationException ex) {
    log("RemotePowershell: CmdletInvocationException  -   " + shellcommand + "  -   " +ex.Message);
    return "Invalid";
  } catch (ParseException ex) {
    log("RemotePowershell: ParseException  -   " + shellcommand + "  -   " +ex.Message);
    return "Invalid";
  } catch (ParameterBindingException ex) {
    log("RemotePowershell: ParameterBindingException  -   " + shellcommand + "  -   " +ex.Message);
    return "Invalid";
  } catch (CommandNotFoundException ex) {
    log("RemotePowershell: CommandNotFoundException  -   " + shellcommand + "  -   " +ex.Message);
    return "Invalid";
  } catch (PSArgumentException ex) {
    log("RemotePowershell: PSArgumentException  -   " + shellcommand + "  -   " +ex.Message);
    return "EMS";
  } catch (Exception e) {
    if(e.Message.Equals("Unexpected number of Remote Runspace connections returned.")) {
      log("remoteShell cannot be established");
      return "remoteShell Failed";
    } else if(e.Message.Contains("PromptForCredential") && retryOnSessionExpiry) {
      log("Session Expired reinitializing runspace");
      try {
        closeRunSpace();
        initializeRunspace();
        log("Session Expired runspace reinitialized");
        executeRemoteCommand(shellcommand,false);
      } catch(Exception ex) {
        if(ex.Message.Equals("Unexpected number of Remote Runspace connections returned.")) {
          log("Session Expired: remoteShell cannot be established - " + shellcommand + "  -   " +ex.Message);
          closeRunSpace();
          return "remoteShell Failed";
        }
      }
    }
    log("RemotePowershell: Exception  -   " + shellcommand + "  -   " +e.Message);
    return "EMSError" + e;
  }
}
4

0 に答える 0