Solved

Where, Variables and Functions in Powershell

Posted on 2016-09-16
3
43 Views
Last Modified: 2016-09-19
Hi
I have this function, that should reveal all user accounts with some given letter combinations in the Description og Info field in AD.
Function RevealClearTextPassword([Array]$search){


    $words = $search -split ","


    foreach($word in $words){
        
        $list = $list += "(`$_.Description -like `"*$word*`" -OR `$_.info -like `"*$word*`") -OR "
    }

    $list = $list.substring(0,$list.Length-5)
    Get-ADUser -filter * -Properties enabled, Description, Info | Where{$list} | ft samaccountname,enabled,description,info -AutoSize
    
}

Open in new window

When I run the Function like this:
RevealClearTextPassword pwd,pw,kode,pass

Open in new window

it returns ALL users in the AD no matter what is written in the two fields.
Why is that?

If I write out the Get-ADUser line it looks ok:
Get-ADUser -filter * -Properties enabled, Description, Info | Where{($_.Description -like "*pwd*" -OR $_.info -like "*pwd*") -OR ($
_.Description -like "*pw*" -OR $_.info -like "*pw*") -OR ($_.Description -like "*kode*" -OR $_.info -like "*kode*") -OR ($_.Descrip
tion -like "*pass*" -OR $_.info -like "*pass*")} | ft samaccountname,enabled,description,info -AutoSize

Open in new window

As far as I can see it should work, but it doesn't.
0
Comment
Question by:Kasper Katzmann
3 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 250 total points
ID: 41801439
When you mention Where{$list}, PowerShell consider it as a variable not a expression/condition. Until $list is not equal to $null the where condition is always considered as true.  That why the result has all users.

Change Where{$list} to Where{Invoke-Expression $list} and see if it works for you

You can also construct a pattern and use -Match to achieve the same..
Try..
Function RevealClearTextPassword([Array]$search){
  $words = ($search -Split ",") -join "|"
    Get-ADUser -filter * -Properties enabled, Description, Info | 
      ? {$_.Description -Match $words -OR $_.info -match $words} | 
    FT samaccountname,enabled,description,info -AutoSize
}

Open in new window

0
 
LVL 39

Assisted Solution

by:footech
footech earned 250 total points
ID: 41801836
As an alternative to Subsun's, if you change lines 12 and 13 as below it should work.
$list = [scriptblock]::Create( ($list.substring(0,$list.Length-5)) )
    Get-ADUser -filter * -Properties enabled, Description, Info | Where $list | ft samaccountname,enabled,description,info -AutoSize

Open in new window

0
 

Author Closing Comment

by:Kasper Katzmann
ID: 41804345
There is one word for people like you... geniuses
Both worked - thanks
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question