Azri Zakaria
Azri Zakaria

Reputation: 1364

How to write powershell script with Where-Object

Given script like this :

Get-ADPermission -Identity "Mark Adam" | where {$._ExtendedRights -like "*Send-As*"} -and -not {$_.Users -like "NT AUTHORITY\SELF"}

Here I tried so far, still no luck.

var CommandGetAdPermission = new Command("Get-ADPermission");

CommandGetAdPermission.Parameters.Add("Identity", Identity);       

var CommandWhere = new Command("Where-Object");

ScriptBlock filter = ScriptBlock.Create("{$_.ExtendedRights -like \"*Send-As*\"} -and -not {$_.User -like \"NT AUTHORITY\\SELF\"}");

CommandWhere.Parameters.Add("FilterScript", filter);

pipeline.Commands.Add(CommandGetAdPermission);
pipeline.Commands.Add(CommandWhere);                     

psh = pipeline.Invoke();
errors = ps.Streams.Error.ReadAll();

Return error as below :

The term 'Where-Object' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Do I do something wrong?

Upvotes: 2

Views: 1253

Answers (1)

Kirill Pashkov
Kirill Pashkov

Reputation: 3236

Your syntax is wrong. In case of complex filtering (i.e. more than one criteria) you want to put whole filter criteria in script block like this:

Get-ADPermission -Identity "Mark Adam" | Where-Object {$_.ExtendedRights -like "*Send-As*" -and $_.Users -notlike "NT AUTHORITY\SELF"}

There is also a typo with

$._ExtendedRights 

while it has to be

$_.ExtendedRights

Upvotes: 1

Related Questions