ADS PowerShell Module - Variables in a Filter

Sorry for the simple question to get started, but I will answer in a dead end.

My code is simple ... I want to take a variable from the command line into my script and use this variable as a filter string in the AD command. I have the following:

PARAM($myOU) $FoundOUs = Get-ADOrganizationalUnit -Filter 'Name -like "*"' -SearchBase ="OU=Offices,DC=dc1,DC=domain,DC=com" 

So, I want to replace "*" with $ myOU ... I am lost on how to do this. I tried things like -Filter Name $ myOU etc., but no luck. Any suggestions would be great.

0
source share
1 answer

Use string interpolation like this:

 $FoundOUs = Get-ADOrganizationalUnit -Filter "Name -like '$myOU'" -SearchBase="OU=Offices,DC=dc1,DC=domain,DC=com" 

Note that string interpolation only happens with double quotes, so change the order of single and double quotes so that the variable is interpolated. In this case, the use of $($myOU) not required. You usually use a subexpression when you need to access a property, for example. $($myOU.Length) or generally evaluate the expression inside the string.

+1
source

Source: https://habr.com/ru/post/1202974/


All Articles