Solved

Add a variable to property in Powershell

Posted on 2016-09-21
3
38 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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

19 Experts available now in Live!

Get 1:1 Help Now