public static string GetHelpUri(PSObject commandInfoPSObject) { if (commandInfoPSObject == null) { return(string.Empty); } CommandInfo cmdInfo = PSObject.Base(commandInfoPSObject) as CommandInfo; // GetHelpUri helper method is expected to be used only by System.Management.Automation.CommandInfo // objects from types.ps1xml if ((cmdInfo == null) || (string.IsNullOrEmpty(cmdInfo.Name))) { return(string.Empty); } // The type checking is needed to avoid a try..catch exception block as // the CommandInfo.CommandMetadata throws an InvalidOperationException // instead of returning null. if ((cmdInfo is CmdletInfo) || (cmdInfo is FunctionInfo) || (cmdInfo is ExternalScriptInfo) || (cmdInfo is ScriptInfo)) { if (!string.IsNullOrEmpty(cmdInfo.CommandMetadata.HelpUri)) { return(cmdInfo.CommandMetadata.HelpUri); } } AliasInfo aliasInfo = cmdInfo as AliasInfo; if ((aliasInfo != null) && (aliasInfo.ExternalCommandMetadata != null) && (!string.IsNullOrEmpty(aliasInfo.ExternalCommandMetadata.HelpUri))) { return(aliasInfo.ExternalCommandMetadata.HelpUri); } // if everything else fails..depend on Get-Help infrastructure to get us the Uri. string cmdName = cmdInfo.Name; if (!string.IsNullOrEmpty(cmdInfo.ModuleName)) { cmdName = string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", cmdInfo.ModuleName, cmdInfo.Name); } if (DoesCurrentRunspaceIncludeCoreHelpCmdlet()) { // Win8: 651300 if core get-help is present in the runspace (and it is the only get-help command), use // help system directly and avoid perf penalty. var currentContext = System.Management.Automation.Runspaces.LocalPipeline.GetExecutionContextFromTLS(); if ((currentContext != null) && (currentContext.HelpSystem != null)) { HelpRequest helpRequest = new HelpRequest(cmdName, cmdInfo.HelpCategory); helpRequest.ProviderContext = new ProviderContext( string.Empty, currentContext, currentContext.SessionState.Path); helpRequest.CommandOrigin = CommandOrigin.Runspace; foreach ( Uri result in currentContext.HelpSystem.ExactMatchHelp(helpRequest).Select( helpInfo => helpInfo.GetUriForOnlineHelp()).Where(result => result != null)) { return(result.OriginalString); } } } else { // win8: 546025. Using Get-Help as command, instead of calling HelpSystem.ExactMatchHelp // for the following reasons: // 1. Exchange creates proxies for Get-Command and Get-Help in their scenario // 2. This method is primarily used to get uri faster while serializing the CommandInfo objects (from Get-Command) // 3. Exchange uses Get-Help proxy to not call Get-Help cmdlet at-all while serializing CommandInfo objects // 4. Using HelpSystem directly will not allow Get-Help proxy to do its job. var getHelpPS = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace) .AddCommand("get-help") .AddParameter("Name", cmdName) .AddParameter("Category", cmdInfo.HelpCategory.ToString()); try { Collection <PSObject> helpInfos = getHelpPS.Invoke(); if (helpInfos != null) { for (int index = 0; index < helpInfos.Count; index++) { HelpInfo helpInfo; if (LanguagePrimitives.TryConvertTo <HelpInfo>(helpInfos[index], out helpInfo)) { Uri result = helpInfo.GetUriForOnlineHelp(); if (result != null) { return(result.OriginalString); } } else { Uri result = BaseCommandHelpInfo.GetUriFromCommandPSObject(helpInfos[index]); return((result != null) ? result.OriginalString : string.Empty); } } } } finally { getHelpPS.Dispose(); } } return(string.Empty); }
public static string GetHelpUri(PSObject commandInfoPSObject) { if (commandInfoPSObject != null) { CommandInfo info = PSObject.Base(commandInfoPSObject) as CommandInfo; if ((info == null) || string.IsNullOrEmpty(info.Name)) { return(string.Empty); } if ((((info is CmdletInfo) || (info is FunctionInfo)) || ((info is ExternalScriptInfo) || (info is ScriptInfo))) && !string.IsNullOrEmpty(info.CommandMetadata.HelpUri)) { return(info.CommandMetadata.HelpUri); } AliasInfo info2 = info as AliasInfo; if (((info2 != null) && (info2._externalCommandMetadata != null)) && !string.IsNullOrEmpty(info2._externalCommandMetadata.HelpUri)) { return(info2._externalCommandMetadata.HelpUri); } string name = info.Name; if (!string.IsNullOrEmpty(info.ModuleName)) { name = string.Format(CultureInfo.InvariantCulture, @"{0}\{1}", new object[] { info.ModuleName, info.Name }); } if (DoesCurrentRunspaceIncludeCoreHelpCmdlet()) { ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS(); if ((executionContextFromTLS != null) && (executionContextFromTLS.HelpSystem != null)) { HelpRequest helpRequest = new HelpRequest(name, info.HelpCategory) { ProviderContext = new ProviderContext(string.Empty, executionContextFromTLS, executionContextFromTLS.SessionState.Path), CommandOrigin = CommandOrigin.Runspace }; foreach (Uri uri in from helpInfo in executionContextFromTLS.HelpSystem.ExactMatchHelp(helpRequest) select helpInfo.GetUriForOnlineHelp() into result where null != result select result) { return(uri.OriginalString); } } } else { using (PowerShell shell = PowerShell.Create(RunspaceMode.CurrentRunspace).AddCommand("get-help").AddParameter("Name", name).AddParameter("Category", info.HelpCategory.ToString())) { Collection <PSObject> collection = shell.Invoke(); if (collection != null) { for (int i = 0; i < collection.Count; i++) { HelpInfo info3; if (LanguagePrimitives.TryConvertTo <HelpInfo>(collection[i], out info3)) { Uri uriForOnlineHelp = info3.GetUriForOnlineHelp(); if (null != uriForOnlineHelp) { return(uriForOnlineHelp.OriginalString); } } else { Uri uriFromCommandPSObject = BaseCommandHelpInfo.GetUriFromCommandPSObject(collection[i]); return((uriFromCommandPSObject != null) ? uriFromCommandPSObject.OriginalString : string.Empty); } } } } } } return(string.Empty); }