Solved

Add a variable to property in Powershell

Posted on 2016-09-21
3
30 Views
Last Modified: 2016-09-21
Good Day,

This should be very quick and easy.

I have a powershell script and we have multiple child domains.  I would like to include a new property for which domain the ADUser object is in as part of the output.

Below is my script:
__________________________________________________________
$z='y'
$domains = (Get-ADForest).Domains

While ($z -eq 'y')
    {
    $userx = @()
    $nameregex = Read-Host "put in part or al of a user's name"
    $userd = '*'+$nameregex+'*'
        foreach ($domain in $domains) {
            if ($domain -ne "cst.is.local" ) {
                Write-Output "Searching Domain " $domain
               $userx += Get-ADUser -Filter {name -like $userd} -server $domain | Select-Object name,samaccountname,enabled,DistinguishedName
             }
        }
        Write-output $userx | ft -AutoSize
    $z = Read-Host 'do you want to find another user?'
    $z = $z.ToLower()
    }

________________________________________________________________
The script works great but I would like to add the $domain variable to the Select-object output.

Thanks in advance.
0
Comment
Question by:mrfite
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 41809023
Like this:
$z='y'
$domains = (Get-ADForest).Domains

While ($z -eq 'y')
    {
    $userx = @()
    $nameregex = Read-Host "put in part or al of a user's name"
    $userd = '*'+$nameregex+'*'
        foreach ($domain in $domains) {
            if ($domain -ne "cst.is.local" ) {
                Write-Output "Searching Domain " $domain
               $userx += Get-ADUser -Filter {name -like $userd} -server $domain | Select-Object name,samaccountname,enabled,DistinguishedName,@{l="Domain";e={$domain}}
             }
        }
        Write-output $userx | ft -AutoSize
    $z = Read-Host 'do you want to find another user?'
    $z = $z.ToLower()
    }

Open in new window


-saige-
0
 

Author Comment

by:mrfite
ID: 41809047
Thanks,

I knew it was something simple.
0
 

Author Closing Comment

by:mrfite
ID: 41809051
Thanks again
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now