I am trying to use InitialSessionState.ImportPSModule to import a Powershell module.
I am interested to know if the module could not be imported for any reason (for example, the file was not found, etc.). Including such code in the try block does not throw an exception in the event of a failure, and the function seems to fail and continues to work if it cannot import the module.
Is there a way to get a warning in the code if the import fails?
I am trying to do something like the following. The module "TestModule1234" does not exist in the code below. The catch block does not throw an exception.
Note. This is just a prototype test code, so please ignore errors related to the production code.
try { //Initializing the PowerShell runspace InitialSessionState psSessionInitialState = InitialSessionState.CreateDefault(); LogFile.Log("Importing Module TestModule1234"); psSessionInitialState.ImportPSModule(new[] { "TestModule1234" }); LogFile.Log("Creating Powershell Runspace"); m_PoshRunspace = RunspaceFactory.CreateRunspace(psSessionInitialState); } catch (System.Exception ex) { LogFile.Log("Failed to create a Powershell Runspace"); LogFile.Log(ex.ToString()); throw; }
source share