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 ?
$OUNAMEcontain sensible value?