public static void ReloadModules(CommandInvocationIntrinsics invokeCommand, params PSModuleInfo[] moduleInfos) { var modulePaths = GetCommaSeparatedQuotedList(moduleInfos.Select(GetModulePath).ToArray()); if (!String.IsNullOrEmpty(modulePaths)) { var command = $"Import-Module -Name {modulePaths} -Force"; invokeCommand.NewScriptBlock(command).Invoke(); } }
internal static PSModuleInfo[] GetModules(CommandInvocationIntrinsics invokeCommand, bool listAvailable = false, params string[] moduleNames) { var command = "Get-Module"; command += moduleNames != null && moduleNames.Any() ? $" -Name {CommaSeparatedQuotedList(moduleNames)}" : String.Empty; command += listAvailable ? " -ListAvailable" : String.Empty; var modules = listAvailable ? Pwsh.Create().AddScript(command).Invoke <PSObject>() : invokeCommand.NewScriptBlock(command).Invoke(); return(modules != null?modules.Select(m => m?.BaseObject as PSModuleInfo).Where(m => m != null).ToArray() : new PSModuleInfo[] { }); }
public static PSModuleInfo[] GetModules(CommandInvocationIntrinsics invokeCommand, bool listAvailable = false, params string[] moduleNames) { string nameParameter = $"-Name { (moduleNames != null && moduleNames.Any() ? GetCommaSeparatedQuotedList(moduleNames) : "Microsoft.Graph*" )}"; string listAvailableParameter = listAvailable ? " -ListAvailable" : String.Empty; string command = $"Get-Module {nameParameter}{listAvailableParameter}"; Collection <PSObject> modules = listAvailable ? PowerShell.Create().AddScript(command).Invoke <PSObject>() : invokeCommand.NewScriptBlock(command).Invoke(); return(modules != null?modules.Select(m => m?.BaseObject as PSModuleInfo).Where(m => m != null).ToArray() : new PSModuleInfo[] { }); }
public static IEnumerable <T> RunScript <T>(CommandInvocationIntrinsics cii, string script) where T : class => cii.NewScriptBlock(script).Invoke().Select(o => o?.BaseObject as T).Where(m => m != null);