使用自定义 cmdlet 托管受限制的 Powershell
我在我的应用程序中托管 powershell,并设置了一个受限的运行空间池,该池基本上是空的(据我所知)。
public class MyPowerShell : IDisposable
{
private RunspacePool _runspacePool;
private PowerShell _shell;
public MyPowerShell()
{
try
{
var initialSessionState = InitialSessionState.CreateRestricted(SessionCapabilities.RemoteServer);
_runspacePool = RunspaceFactory.CreateRunspacePool(initialSessionState);
_shell = PowerShell.Create();
_shell.RunspacePool = _runspacePool;
_shell.RunspacePool.Open();
_shell.AddCommand("Import-Module").AddParameter("Assembly", Assembly.GetExecutingAssembly());
_shell.Invoke();
_shell.Commands.Clear();
}
catch (Exception ex)
{
throw;
}
}
public void Dispose()
{
_shell.RunspacePool.Close();
_shell.Dispose();
}
public string[] Exec(string commandText)
{
var results = new List<string>();
try
{
_shell.AddScript(commandText);
foreach (var str in _shell.AddCommand("Out-String").Invoke<string>())
{
results.Add(str);
}
}
catch (Exception ex)
{
results.Add(ex.Message);
}
return results.ToArray();
}
}
显然,当我运行此代码时...
_shell.AddCommand("Import-Module").AddParameter("Assembly", Assembly.GetExecutingAssembly());
_shell.Invoke();
_shell.Commands.Clear();
它失败,因为没有可用的“Import-Module”cmdlet。所以,我的问题是如何在“Import-Module”cmdlet 不可用的情况下导入模块?
这是我收到的错误消息...
术语“Import-Module”未被识别为 cmdlet 的名称, 函数、脚本文件或可运行程序。检查拼写 名称,或者如果包含路径,请验证路径是否正确并且 再试一次。
I am hosting powershell within my app and have set up a restricted runspacepool, which is basically empty (to the best of my knowledge).
public class MyPowerShell : IDisposable
{
private RunspacePool _runspacePool;
private PowerShell _shell;
public MyPowerShell()
{
try
{
var initialSessionState = InitialSessionState.CreateRestricted(SessionCapabilities.RemoteServer);
_runspacePool = RunspaceFactory.CreateRunspacePool(initialSessionState);
_shell = PowerShell.Create();
_shell.RunspacePool = _runspacePool;
_shell.RunspacePool.Open();
_shell.AddCommand("Import-Module").AddParameter("Assembly", Assembly.GetExecutingAssembly());
_shell.Invoke();
_shell.Commands.Clear();
}
catch (Exception ex)
{
throw;
}
}
public void Dispose()
{
_shell.RunspacePool.Close();
_shell.Dispose();
}
public string[] Exec(string commandText)
{
var results = new List<string>();
try
{
_shell.AddScript(commandText);
foreach (var str in _shell.AddCommand("Out-String").Invoke<string>())
{
results.Add(str);
}
}
catch (Exception ex)
{
results.Add(ex.Message);
}
return results.ToArray();
}
}
obviously, when I run this code ...
_shell.AddCommand("Import-Module").AddParameter("Assembly", Assembly.GetExecutingAssembly());
_shell.Invoke();
_shell.Commands.Clear();
it fails because there is no "Import-Module" cmdlet available. So, my question is how can I import a module without the "Import-Module" cmdlet being available?
This is the error message I am getting ...
The term 'Import-Module' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and
try again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方案,不记得我现在在哪里做了,而且我实际上忘记了我问过这个问题!无论如何,这是为了有同样问题的人的利益。
当我问这个问题时,我实际上无法看到我正在尝试导入哪个模块,但我已成功使用上述代码来使用 AppFabric 管理 powershell cmdlet。
I have found a solution, can't remember where I did now and I'd actually forgotten I'd asked this question! Anyway, here it is for the benefit of anyone with the same problem.
I can't actually see which module I was trying to import when I asked this question, but I have successfully used the above code for using AppFabric administration powershell cmdlets.