public static IEnumerable <CommandInfo> GetModuleCmdlets(PSCmdlet cmdlet, params string[] modulePaths) { var getCmdletsCommand = String.Join(" + ", modulePaths.Select(mp => $"(Get-Command -Module (Import-Module '{mp}' -PassThru))")); return((cmdlet?.RunScript <CommandInfo>(getCmdletsCommand) ?? RunScript <CommandInfo>(getCmdletsCommand)) .Where(ci => ci.CommandType != CommandTypes.Alias)); }
public static IEnumerable<CmdletAndHelpInfo> GetModuleCmdletsAndHelpInfo(PSCmdlet cmdlet, params string[] modulePaths) { var getCmdletAndHelp = String.Join(" + ", modulePaths.Select(mp => $@"(Get-Command -Module (Import-Module '{mp}' -PassThru) | Where-Object {{ $_.CommandType -ne 'Alias' }} | ForEach-Object {{ @{{ CommandInfo = $_; HelpInfo = ( invoke-command {{ try {{ Get-Help -Name $_.Name -Full }} catch{{ '' }} }} ) }} }})" )); return (cmdlet?.RunScript<Hashtable>(getCmdletAndHelp) ?? RunScript<Hashtable>(getCmdletAndHelp)) .Select(h => new CmdletAndHelpInfo { CommandInfo = (h["CommandInfo"] as PSObject)?.BaseObject as CommandInfo, HelpInfo = h["HelpInfo"] as PSObject }); }
public static IEnumerable<PSObject> GetScriptHelpInfo(PSCmdlet cmdlet, params string[] modulePaths) { var importModules = String.Join(Environment.NewLine, modulePaths.Select(mp => $"Import-Module '{mp}'")); var getHelpCommand = $@" $currentFunctions = Get-ChildItem function: {importModules} Get-ChildItem function: | Where-Object {{ ($currentFunctions -notcontains $_) -and $_.CmdletBinding }} | ForEach-Object {{ Get-Help -Name $_.Name -Full }} "; return cmdlet?.RunScript<PSObject>(getHelpCommand) ?? RunScript<PSObject>(getHelpCommand); }
public static IEnumerable<FunctionInfo> GetScriptCmdlets(PSCmdlet cmdlet, string scriptFolder) { // https://stackoverflow.com/a/40969712/294804 var getCmdletsCommand = $@" $currentFunctions = Get-ChildItem function: Get-ChildItem -Path '{scriptFolder}' -Recurse -Include '*.ps1' -File | ForEach-Object {{ . $_.FullName }} Get-ChildItem function: | Where-Object {{ ($currentFunctions -notcontains $_) -and $_.CmdletBinding }} "; return cmdlet?.RunScript<FunctionInfo>(getCmdletsCommand) ?? RunScript<FunctionInfo>(getCmdletsCommand); }
public static void RunScript(this PSCmdlet cmdlet, ScriptBlock block) => cmdlet.RunScript <PSObject>(block.ToString());
public static void RunScript(this PSCmdlet cmdlet, string script) => cmdlet.RunScript <PSObject>(script);