MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections.ObjectModel;5 6using Dbg = System.Management.Automation.Diagnostics;7 8namespace System.Management.Automation9{10 /// <summary>11 /// Class MamlCommandHelpInfo keeps track of help information to be returned by12 /// command help provider.13 /// </summary>14 internal class RemoteHelpInfo : BaseCommandHelpInfo15 {16 private readonly PSObject _deserializedRemoteHelp;17 18 internal RemoteHelpInfo(19 ExecutionContext context,20 RemoteRunspace remoteRunspace,21 string localCommandName,22 string remoteHelpTopic,23 string remoteHelpCategory,24 HelpCategory localHelpCategory) : base(localHelpCategory)25 {26 Dbg.Assert(remoteRunspace != null, "Caller should verify arguments");27 28 using (PowerShell powerShell = PowerShell.Create())29 {30 powerShell.AddCommand("Get-Help");31 powerShell.AddParameter("Name", remoteHelpTopic);32 if (!string.IsNullOrEmpty(remoteHelpCategory))33 {34 powerShell.AddParameter("Category", remoteHelpCategory);35 }36 37 powerShell.Runspace = remoteRunspace;38 39 Collection<PSObject> helpResults;40 using (new PowerShellStopper(context, powerShell))41 {42 helpResults = powerShell.Invoke();43 }44 45 if ((helpResults == null) || (helpResults.Count == 0))46 {47 throw new Microsoft.PowerShell.Commands.HelpNotFoundException(remoteHelpTopic);48 }49 50 Dbg.Assert(helpResults.Count == 1, "Remote help should return exactly one result");51 _deserializedRemoteHelp = helpResults[0];52 _deserializedRemoteHelp.Methods.Remove("ToString");53 // Win8: bug9457: Remote proxy command's name can be changed locally using -Prefix54 // parameter of the Import-PSSession cmdlet. To give better user experience for55 // get-help (on par with get-command), it was decided to use the local command name56 // for the help content.57 PSPropertyInfo nameInfo = _deserializedRemoteHelp.Properties["Name"];58 if (nameInfo != null)59 {60 nameInfo.Value = localCommandName;61 }62 63 PSObject commandDetails = this.Details;64 if (commandDetails != null)65 {66 nameInfo = commandDetails.Properties["Name"];67 if (nameInfo != null)68 {69 nameInfo.Value = localCommandName;70 }71 else72 {73 commandDetails.InstanceMembers.Add(new PSNoteProperty("Name", localCommandName));74 }75 }76 }77 }78 79 internal override PSObject FullHelp80 {81 get82 {83 return _deserializedRemoteHelp;84 }85 }86 87 private string GetHelpProperty(string propertyName)88 {89 PSPropertyInfo property = _deserializedRemoteHelp.Properties[propertyName];90 if (property == null)91 {92 return null;93 }94 95 return property.Value as string;96 }97 98 internal override string Component99 {100 get101 {102 return this.GetHelpProperty("Component");103 }104 }105 106 internal override string Functionality107 {108 get109 {110 return this.GetHelpProperty("Functionality");111 }112 }113 114 internal override string Role115 {116 get117 {118 return this.GetHelpProperty("Role");119 }120 }121 }122}123 