0

I have created a script in PowerShell (v. 5.1.17763.592) and it doesn't work properly.

$NUMBER = Read-Host 'Enter the number of Organizational Unit (without parentheses)'
$TMPFILENAME = Get-ADOrganizationalUnit -filter 'Name -like "*($NUMBER)*"' | select -ExpandProperty Name

$FILENAME = $TMPFILENAME | Out-String 
$FILENAME = $FILENAME + ".csv"
$FILENAME = $FILENAME -replace '\s',''
$FILENAME = $FILENAME -replace '&',''

$OUNAME = Get-ADOrganizationalUnit -Filter 'Name -like "*($NUMBER)*"' |
          select -ExpandProperty DistinguishedName

New-Item -Path "c:\Raports" -ItemType Directory -ErrorAction SilentlyContinue

Get-ADUser -Filter * -SearchBase $OUNAME | Export-Csv "C:\Raports\$FILENAME"

After run this I'm receiving an error:

Get-ADUser : Cannot validate argument on parameter 'SearchBase'. The argument is
null. Provide a valid value for the argument, and then try running the command
again.
At line:13 char:34
+ Get-ADUser -Filter * -SearchBase $OUNAME | Export-Csv

When I put a $OUNAME content in the Get-ADUser I got error:

Get-ADUser : Cannot bind parameter because parameter 'Filter' is specified more
than once. To provide multiple values to parameters that can accept multiple
values, use the array syntax. For example, "-parameter value1,value2,value3"

If I put a number directly into script (instead of using variable $NUMBER) it is working perfectly, but I want to allow user to enter this number by her/himself.


Edit (after few hours :) )

I've just realized that problem belong to the variable transmission.

Get-ADOrganizationalUnit -filter 'Name -like "*(202)*"' | select -ExpandProperty DistinguishedName

Get-ADOrganizationalUnit -filter 'Name -like "*($abc)*"' | select -ExpandProperty DistinguishedName

Namely, if our variable $abc would be equal to 202 (no matter if we just define it as:

$abc=202           or
$abc="202"         or 
$abc=Read-Host 

)
it wont give any result, but if I put 202 directly to the script, the result is acceptable. Can anyone tell me how to solvthat problem ?

3
  • Does $OUNAME contain sensible value? Commented Nov 19, 2019 at 9:03
  • Well, I'll be honest, this is my first 'real' script. I need to find OU with specific name and every OU has unique Number. The reason of this script is to get every information GET-ADUser can give me about members of specific OU Commented Nov 19, 2019 at 9:20
  • Unless your OUs do have parenthesis in the name, get-adou cmdlet is likely to return zero items. That's why you need to print ouname variable and see if it contains sensible data, nonsense or is empty. Commented Nov 19, 2019 at 9:42

3 Answers 3

0

This type of filter works for me, without the parentheses, and the double-quotes on the outside. The double-quotes need to be on the outside for the $abc variable to be interpreted. Also, parentheses are meaningless inside double-quotes unless preceded by a dollar sign. $( )

-filter "Name -like '*$abc*'" 
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer is correct but probably requires some explanation about quoting rules
0

Alright. I've found the issue. The apostrophe and quotation marks should be reversed.

    **WRONG**
Get-ADOrganizationalUnit -filter 'Name -like "*($abc)*"' | select -ExpandProperty DistinguishedName**strong text**

.

 **CORRECT*
Get-ADOrganizationalUnit -filter "Name -like '*($abc)*'" | select -ExpandProperty DistinguishedName

Comments

-1

Define the variable type for $NUMBER as [String], It may work.

[String]$NUMBER = Read-Host 'Enter the number of Organizational Unit (without parentheses)'

1 Comment

thank you for effort, but it doesn't work. GET-ADUser still doesn't recognize $OUNAME as proper argument. The most annoying fact is - like i said before - that script works with directly entered number.

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.