0
$computername = 'RSERV1234'    
$computername.Substring(5,4) returns '1234' as expected    
Get-ADOrganizationalUnit -Filter {Name -like $computername.Substring(5,4)}

returns:

Property 'Substring' not found in object of type: 'System.String'

Please help!

2
  • 1
    Method calls are not supported as comparison arguments inside the Active Directory filter, causing PowerShell to ignore the (5,4) part and look for a property called Substring - which doesn't exist. Call Substring() before Get-ADOrganizationalUnit Commented May 27, 2015 at 13:08
  • 2
    Try: Get-ADOrganizationalUnit -Filter {Name -like "*$($computername.Substring(5,4))*"} Commented May 27, 2015 at 13:11

1 Answer 1

1

From about_ActiveDirecory_Filter:

Filter Syntax
  The following syntax descriptions use Backus-Naur form to show the
  PowerShell Expression Language for the Filter parameter.

    <filter>  ::= "{" <FilterComponentList> "}"

    <FilterComponentList> ::= <FilterComponent> |
        <FilterComponent> <JoinOperator> <FilterComponent> |
        <NotOperator>  <FilterComponent>

    <FilterComponent> ::= <attr> <FilterOperator> <value> |
        "(" <FilterComponent> ")"

    <FilterOperator> ::= "-eq" | "-le" | "-ge" | "-ne" | "-lt" | "-gt" |
        "-approx" | "-bor" | "-band" | "-recursivematch" | "-like" |
        "-notlike"

    <JoinOperator> ::= "-and" | "-or"

    <NotOperator> ::= "-not"

    <attr> ::= <PropertyName> | <LDAPDisplayName of the attribute>

    <value>::= < this value will be compared to the object data for
        attribute <ATTR> using the specified filter operator

The Filter parameter translates PowerShell-like expressions to an LDAP filter, but doesn't support just any arbitrary PowerShell statement, only a specific set of comparison operations with attribute names as the left-hand operand and the comparison value on the right hand side.

Do your Substring() call beforehand:

$substr = $computername.Substring(5,4)

Get-ADOrganizationalUnit -Filter {Name -like "$substr"}
Sign up to request clarification or add additional context in comments.

1 Comment

Great thanks, wanted to pipeline the computername in but thanks anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.