1

In my script, I'm parsing emails and regex-ing into variables. When i write-host or query the variable it shows, but when I try to run it in the script below it acts lick its empty.

I'm saving the variable as $Branch $Branch = $Matches.BRANCH

Get-ADOrganizationalUnit -Filter "Name -like '$($Branch)'"

I've tried:(when I run this, it runs with no errors, but also nothing shows.) Get-ADOrganizationalUnit -Filter 'Name -like $Branch'

when I try this I get an error that actually shows the contents of the variable in the error. Get-ADOrganizationalUnit -Filter "Name -like "$Branch""

Get-ADOrganizationalUnit : A positional parameter cannot be found that accepts argument 'WA AZ1'.
At line:1 char:1
+ Get-ADOrganizationalUnit -Filter "Name -like "$Branch""
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADOrganizationalUnit], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADOrganizationalUnit 

If I manually create the variable and run it, it works and finds the correct OU.

any help would be great, thanks

Hweb
  • 13
  • 3
  • 1
    the usual format depends on the type of OUTER quotes. [*grin*] so this ... >>> `-Filter 'Name -like $Branch'` <<< otta be this >>> `-Filter "Name -like $Branch"` <<< ///// also `-like` is a wildcard operator, so if you are not including wildcards in that $Var, it just does `-eq` for an exact match. – Lee_Dailey Aug 28 '20 at 14:21
  • "I'm saving the variable as `$Branch`" - _how_? Please show us the code that assigns/saves the branch name to that variable – Mathias R. Jessen Aug 28 '20 at 14:26
  • I get a parsing error when running with "" ```Get-ADOrganizationalUnit : Error parsing query: 'Name -like WA AZ1' Error Message: 'syntax error' at position: '12'. At line:1 char:1 + Get-ADOrganizationalUnit -Filter "Name -like $Branch" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ParserError: (:) [Get-ADOrganizationalUnit], ADFilterParsingException + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADOrganizationalUnit``` – Hweb Aug 28 '20 at 14:27
  • @Hweb Your first attempt (`Get-ADOrganizationalUnit -Filter "Name -like '$($Branch)'"`) is correct (the `$()` is unnecessary, but the result is the same). If the command silently just doesn't return anything, then that's because no OUs can be found matching the search criteria. – Mathias R. Jessen Aug 28 '20 at 14:31
  • but when i manually change the `$Branch` to "WA AZ1" -(this is the $VAR) it works and finds the OU – Hweb Aug 28 '20 at 14:33
  • 1
    So that means that the value of `$Branch` _isn't_ the string "WA AZ1" as you expect. Show us your email parsing and regex-ing code if you want qualified help – Mathias R. Jessen Aug 28 '20 at 14:34
  • ` $Branch = $Matches.BRANCH Write-Host "Branch =" " "$Branch -BackgroundColor DarkGreen` – Hweb Aug 28 '20 at 14:36
  • `Branch = WA AZ1` - this is my write-host response – Hweb Aug 28 '20 at 14:38
  • 1
    Thats _not_ a complete example - what was the input string and pattern used in the previous regex match? – Mathias R. Jessen Aug 28 '20 at 14:45
  • ```$RE = [RegEx]'(?sm)Name:\s+(?.*?)$.*?Goes by:\s+(?.*?)$.*?Email:\s+(?.*?)$.*?Phone:\s+(?.*?)$.*?Mobile:\s+(?.*?)$.*?SubBranch:\s+(?.*?)$.*?Branch:\s+(?.*?)$.*?Branch Address:\s+(?
    .*?)$.*?Title:\s+(?.*?)$.*?EncompassID:\s+(?<encompassid>.*?)$.*?Supervisor Email:\s+(?<supervisor>.*?)$.*'```</supervisor></encompassid>
    – Hweb Aug 28 '20 at 14:47
  • What type is it? (`$Branch.GetType()`) is it null? (`[string]::IsNullOrEmpty($Branch)`). Best to add those two in your code just before the line where it seems empty. – T-Me Aug 28 '20 at 14:57
  • ```IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String System.Object ``` – Hweb Aug 28 '20 at 15:01

1 Answers1

0

Get-ADOrganizationalUnit -Filter 'Name -like $Branch' is the most robust syntax to use - it lets the AD provider interpret the $Branch variable, so you need no embedded quoting.

  • Caveat: This syntax only works with simple variable references, not also with expressions (e.g., $Branch.Name) and doesn't work with implicitly remoting AD modules - see this answer.

If this produces no output, the implication is that there are no OUs that match the wildcard expression contained in $Branch. (As Lee Dailey points out, use of -like is only needed if your comparison value contains wildcard metacharacter *; use -eq otherwise).

If printing the value of $Branch looks like it contains the right value, but the filter doesn't work, there may be hidden characters, such as trailing spaces or a trailing CR character, if your input string has Windows-style CRLF newlines[1]. You can eliminate both potential problems with a call to .Trim():

$Branch = $Matches.BRANCH.Trim()
Get-ADOrganizationalUnit -Filter 'Name -like $Branch'

[1] To demonstrate a potential problem:
$null = "foo`r`n" -match '(?m)^.+$'; $Matches.0 -match "`r" returns $true, because the $ only matches a LF character ("`n"), not also the CR character before it, so .+ captures the CR as well.

mklement0
  • 382,024
  • 64
  • 607
  • 775