Filtering Exchange PowerShell Locally

As an Exchange developer, it’s quite possible you’ve run across this error message:

Assignment statements are not allowed in restricted language mode or a Data section.

 

Or you’ve possibly seen this one:

Script block literals are not allowed in restricted language mode or a Data section.

 

While it’s quite easy to change the PSLanguageMode to FullLanguage instead of RestrictedLanguage it may be that you can’t do that for one reason or another.  Here’s a small method I put together for filtering the results locally.  Use your discretion about the size of your resultsets to bring back.

public IEnumerable<PSObject> ApplyPSFilter(IEnumerable psData, ScriptBlock scriptBlock)
{
    using (var ps = System.Management.Automation.PowerShell.Create())
    {
	    ps.AddCommand("Where-Object");
	    ps.AddParameter("FilterScript", scriptBlock);

	    return ps.Invoke(psData);
    }
}

 

Tuesday, March 25th, 2014 C#, Code, Exchange

Leave a Reply

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

WordPress SEO fine-tune by Meta SEO Pack from Poradnik Webmastera