2

This is a continuation of my previous question Invoking powershell cmdlets from C#

I want to invoke the active directory cmdlet get-aduser from within C#, and the first parameter it takes is a filter. The full cmdlet I'm trying to execute is:

get-aduser -filter {name -eq "joe bloggs"} -Properties * | select employeeID

Following from my previous question (See link above), I created a scriptblock to do that:

ps2.Commands.AddCommand("get-aduser");
string script = string.Format("name -eq \"{0}\"", fullname); //fullname constuction now shown
ScriptBlock filter2 = ScriptBlock.Create(script);
ps2.AddParameter("FilterScript", filter2);
ps2.AddParameter("Properties").AddArgument("*");
ps2.AddCommand("select").AddArgument("employeeID");
ps2.Invoke();

However, when I execute this, I get an exception at ps2.Invoke():

A parameter cannot be found that matches parameter name 'FilterScript'.

Any suggestions on this one?

Thanks in advance

Community
  • 1
  • 1
NullPointer
  • 2,084
  • 7
  • 24
  • 38

1 Answers1

1

Get-ADUser doesn't have a 'FilterScript' parameter (Where-Object does), use 'Filter' :)

Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Great, thanks, that got me past the FilterScript problem, but now there's a new one: A positional parameter cannot be found that accepts argument '*'. It seems the "Properties" parameter is not being accepted either :(. Sorry for being thick about this, I'm learning how to use PS through C# – NullPointer Jun 13 '13 at 15:39
  • Yikes, stupid mistake, fixed it now.. had to use .AddCommand("Properties","*") and not use AddArgument() – NullPointer Jun 13 '13 at 15:46