我希望通过自动化运行一些 powershell 脚本。就像是:
IList errors;
Collection<PSObject> res = null;
using (RunspaceInvoke rsi = new RunspaceInvoke())
{
try
{
res = rsi.Invoke(commandline, null, out errors);
}
catch (Exception ex)
{
LastErrorMessage = ex.ToString();
Debug.WriteLine(LastErrorMessage);
return 1;
}
}
我面临的问题是,如果我的脚本使用write-host
上面的 cmdlet,则会抛出System.Management.Automation.CmdletInvocationException
-
无法调用此函数,因为当前主机没有实现它。
有什么好的选择可以解决这个问题?