Tag: runspace

C#Runspace Powershell(交互式)

我正在尝试使用C#在运行空间中执行带有参数的Powershell文件。 不幸的是我得到以下输出: A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as […]

ImportPSModule故障检测

我试图使用InitialSessionState.ImportPSModule来导入Powershell模块。 我有兴趣知道模块的导入是否由于任何原因(例如找不到文件等)而失败。 将这些代码放在try块中不会在出现故障时引发exception,并且该函数似乎无声地失败并且如果无法导入模块则继续。 如果导入失败,有没有办法在代码中发出警报? 我正在尝试做类似以下的事情。 在下面的代码中,模块“TestModule1234”不存在。 catch块没有捕获exception。 注意:这只是原型测试代码,因此请忽略任何与生产代码相关的违规行为。 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; }