How to run PowerShell scripts via automation without running into Host issues

Posted by Scott Weinstein on Stack Overflow See other posts from Stack Overflow or by Scott Weinstein
Published on 2010-04-16T23:31:03Z Indexed on 2010/04/16 23:33 UTC
Read the original article Hit count: 408

I'm looking to run some powershell scripts via automation. Something like:

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;
    }
}

the problem I'm facing is that if my script uses cmdlets such as write-host the above throws an System.Management.Automation.CmdletInvocationException -

Cannot invoke this function because the current host does not implement it.

What are some good options for getting around this problem?

© Stack Overflow or respective owner

Related posts about powershell

Related posts about powershell-v2.0