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

回報:PowerShell的子方法

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

請幫幫忙!

+1

方法調用,不支持作爲Active Directory過濾器內部的比較參數,造成PowerShell來忽略'(5,4)'一部分,並尋找一個叫做'屬性子串「 - 它不存在。在'Get-ADOrganizationalUnit'之前調用'Substring()'# –

+2

試試:'Get-ADOrganizationalUnit -Filter {Name -like「* $($ computername.Substring(5,4))*」}' – arco444

+0

感謝近作品 – Chester1111

回答

0

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 

Filter參數轉換的PowerShell樣表達式的LDAP過濾器,但不支持只是任意PowerShell的說法,只有一組特定的比較操作與屬性名稱爲左邊的操作數和右邊的比較值。

做你Substring()事先電話:

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

Get-ADOrganizationalUnit -Filter {Name -like "$substr"} 
+0

非常感謝,想要管道computername但無論如何 – Chester1111