运行以下 PoweShell 命令时出现异常:
Get-mailboxPermission -Identity MAILBOX_EMAIL |
fl User,AccessRights,IsInherited,Deny | Out-String -Width 300
例外:
使用“1”参数调用“GetSteppablePipeline”的异常:“找不到自定义属性“参数”的类型。
确保已加载包含此类型的程序集。”
紧随其后的是
Get-mailboxPermission -Identity "mailboxidentity" |
fl User,AccessRights,IsInherited,Deny | Out-String -Width 300
这让我ParameterBindingException
找不到与参数名称“CommandName”匹配的参数。
这是我的代码的一部分:
我将把上面的命令executeRemoteCommand
一一发送给所有用户。
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;
}
}