Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Where, Variables and Functions in Powershell

Posted on 2016-09-16
3
Medium Priority
?
54 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 1000 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 41

Assisted Solution

by:footech
footech earned 1000 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

Introducing the WatchGuard 420 Access Point

WatchGuard's newest access point includes an 802.11ac Wave 2 chipset, providing the fastest speeds for VoIP, video and music streaming, and large data file transfers. Additionally, enjoy the benefits of strong security as the 3rd radio delivers dedicated WIPS protection!

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

715 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