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